darc
darc
BABetter Auth
Created by darc on 3/20/2025 in #help
Trigger Session update ( to update user data)
Hey im updating user data after signin with google basically I assign a username and a checkbox to agree TOS. User is always redirected this onboarding page as long as he has 'pending' role. basically, after he submit his data i update him to a user role and he can browse the app now. How can I trigger/refresh session data to get the current state of the user from db? middleware : import { NextRequest, NextResponse } from "next/server"; import { getSessionCookie } from "better-auth/cookies"; import { betterFetch } from "@better-fetch/fetch"; import { auth } from "./lib/auth"; const authRoutes = ["/sign-in"]; const onBoarding = ["/on-boarding"]; type Session = typeof auth.$Infer.Session; export async function middleware(request: NextRequest) { const sessionCookie = getSessionCookie(request); const { data: session } = await betterFetch<Session>( "/api/auth/get-session", { baseURL: request.nextUrl.origin, headers: { // Get the cookie from the request headers cookie: request.headers.get("cookie") || "", }, } ); const pathName = request.nextUrl.pathname; const isAuthRoute = authRoutes.includes(pathName); const isOnBoardingRoute = onBoarding.includes(pathName); if (pathName === "/") { return NextResponse.next(); } if (!sessionCookie) { if (isAuthRoute) { return NextResponse.next(); } return NextResponse.redirect(new URL("/sign-in", request.url)); } // check if user role is pending redirect them to onboarding to set username if (session?.user.role === "pending" && !isOnBoardingRoute) { return NextResponse.redirect(new URL("/on-boarding", request.url)); } if (isAuthRoute) { return NextResponse.redirect(new URL("/", request.url)); } return NextResponse.next(); } export const config = { matcher: [ "/((?!api|_next/static|_next/image|.\.png$).)", "/dashboard", "/library", ], // Specify the routes the middleware applies to };
5 replies
BABetter Auth
Created by darc on 3/19/2025 in #help
On-boarding step after social login
Hey! I m using google social signin and i give new users the 'pedning' role attribute in my database, I want to redirect these type of users to the onboarding page whenever they try to access anything in my app. where i collect additional infos about them such as to setup a displayname. , so basically not allowed to skip this step, then i change their status to something like 'member'. i want to know if there is a way to get the authenticated user data in my middleware so i can read their role status and redirect accordinly isntead of spamming all my pages with this check ? or any other method. thank you !
5 replies