-
As suggested in e.g. https://developers.google.com/identity/sign-in/web/backend-auth#create-an-account-or-session after login, I can use the ID token from the Auth provider to create a new user/establish sessions on my own API backend.
I have known that I can save the ID token by jwt({ token, trigger, session, account }) {
token.idToken = account?.id_token;
return token
}
async session({ session, token }) {
session.idToken = token.idToken
return session
} So I can retrieve it by const session = await auth();
...
await fetch(... `${session?.idToken}` ... in my API call. However, it is not clear to me what's the best approach to inject this user creation API call. I have tried to
Any suggestion? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I found out a solution here. So I redirect import { signIn } from "@/auth";
await signIn(... { redirectTo: "/welcome" }); I define an idempotent Then in the welcome page, I do a import { redirect } from "next/navigation";
export default async function Welcome() {
await createUserFromIdToken();
redirect("/dashboard");
} to the real login landing page ( This solves the problem. |
Beta Was this translation helpful? Give feedback.
I found out a solution here.
So I redirect
signIn
to a new/welcome
page.I define an idempotent
createUserFromIdToken()
which call the backend API to create the user, and do nothing if the user already exists.Then in the welcome page, I do a
to the real login landing page (
/dashboard
in this case).This solves the problem.