user onboarding

Hi, what do you think is the best way to make user onboarding mandatory? I'd like users to fill out their username before they can start using the app.
3 Replies
lonelyplanet
lonelyplanet3mo ago
@aris_22 You could extend the user schema to have a field for completedOnboarding and make it either a Non Nullable Boolean or use a Nullable Date (allows you to track when users finish onboarding), then you can just check if it is set to True (Boolean Method) or Not Null (Date Method). For typescript safety check out the docs on inference For setting onboarding is complete either just use your ORM directly (Like drizzle or prisma) or just hit raw SQL
TypeScript | Better Auth
Better Auth TypeScript integration.
Database | Better Auth
Learn how to use a database with Better Auth.
aris_22
aris_22OP3mo ago
@lonelyplanet Great, thanks. Where do you think it's best to verify this field? One option would be to obtain the session in the middleware, but I'm not sure if this is optimal since a DB query will be performed on each request.
const { data: session } = await betterFetch<Session>("/api/auth/get-session", {
baseURL: request.nextUrl.origin,
headers: {
cookie: request.headers.get("cookie") || "", // Forward the cookies from the request
},
});
const { data: session } = await betterFetch<Session>("/api/auth/get-session", {
baseURL: request.nextUrl.origin,
headers: {
cookie: request.headers.get("cookie") || "", // Forward the cookies from the request
},
});
lonelyplanet
lonelyplanet3mo ago
https://www.better-auth.com/docs/concepts/hooks Only implement the check where needed: - For better auth specific functions use a hook (For example a user cannot accept a org invite until onboarding is complete). - For other actions/routes/procedures/logic etc... just do a simple check if user not onboarded then reject else continue Also dont know if you've done this but: User input: false to prevent it from updated from the client for example
additionalFields: {
onboarding_completed: {
type: "date", // using a date type to store the onboarding completed date as a timestamp useful for analytics
required: false, // not required to pass in the onboarding completed date allowing null values
defaultValue: null, // default value is null
input: false, // not allowing the user to set the onboarding completed date at all, only read access.
},
},
additionalFields: {
onboarding_completed: {
type: "date", // using a date type to store the onboarding completed date as a timestamp useful for analytics
required: false, // not required to pass in the onboarding completed date allowing null values
defaultValue: null, // default value is null
input: false, // not allowing the user to set the onboarding completed date at all, only read access.
},
},
-# Don't use middleware for auth checks when using Fullstack frameworks like Next.JS as you will block ALL requests and add latency and may even dupe requests, Its recommended to run all auth checks per actions/routes/procedures/logic basis. You can abstract it to a function like checkAuth() @aris_22

Did you find this page helpful?