nextauth session null
Unanswered
North Pacific hake posted this in #help-forum
North Pacific hakeOP
hello, so I am trying to make the users create their username on the first signin, but when I redirect them to the page, the session is null.
however if i remove this snippet
the session is not null and correctly contains user information
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import { NextAuthConfig } from "next-auth";
import { getUser } from "@/lib/actions/auth";
import { db } from "@/db";
const authConfig: NextAuthConfig = {
providers: [
Google({
clientId: process.env.AUTH_GOOGLE_ID as string,
clientSecret: process.env.AUTH_GOOGLE_SECRET as string,
}),
],
callbacks: {
authorized({ auth, request: { nextUrl } }) {
return !!auth?.user;
},
async signIn({ user, account, profile }) {
if (!user.email) return false;
try {
const existingGuest = await getUser(user.email);
if (!existingGuest) {
return "/create-username";
}
return true;
} catch {
return false;
}
},
async session({ session, user }) {
if (session.user?.email) {
const dbUser = await db.user.findUnique({
where: { email: session.user.email },
});
if (dbUser) {
session.user.id = dbUser.id;
}
}
return session;
},
},
pages: {
signIn: "/login",
},
};
export const {
auth,
signIn,
signOut,
handlers: { GET, POST },
} = NextAuth(authConfig);
export { authConfig };
however if i remove this snippet
if (!existingGuest) {
return "/create-username";
}
the session is not null and correctly contains user information
10 Replies
Basically, removing that condition, it'll take the sign in even if they werent a user before and tells the app that they were. In turn, it'll populate the session with the sign in from Google.
What you can do is basically set a flag, so when the user signs in with google, it'll setup the session, take them to the page you've setup after the auth.
Run a check for that flag on that page and if it exists push them to the create-username page so they have the session to then create their username. Otherwise the session hasn't been created so, so it won't persist into the create-username page.
What you can do is basically set a flag, so when the user signs in with google, it'll setup the session, take them to the page you've setup after the auth.
Run a check for that flag on that page and if it exists push them to the create-username page so they have the session to then create their username. Otherwise the session hasn't been created so, so it won't persist into the create-username page.
North Pacific hakeOP
Ah so basically handle it in the front end? And only show the component if they don’t already exist? And if they do redirect somewhere right?
That's correct 👌 otherwise you won't have a valid session. Hopefully that explanation makes sense.
North Pacific hakeOP
Thank you, I tried that approach after posting here but wasn’t sure if that was optimal or not
But that also means that everytime they sign in from Google they get directed to that page
Wouldn’t that be annoying?
const { data: session } = useSession();
const router = useRouter();
useEffect(() => {
if (session && !session.user.hasCompletedSetup) {
router.push("/create-username");
}
}, [session, router]);
if (!session) return null;
on the page where a verified user would go hasCompletedSetup is the flag, they'll only be redirected out on first account creation
const router = useRouter();
useEffect(() => {
if (session && !session.user.hasCompletedSetup) {
router.push("/create-username");
}
}, [session, router]);
if (!session) return null;
on the page where a verified user would go hasCompletedSetup is the flag, they'll only be redirected out on first account creation
so it won't really affect them once they've created it
North Pacific hakeOP
Oh I wasn’t aware that it had .hasCompletedSetup
Thank you I’ll try that out