Multi-step authentication with github, google or any other OAuth provider. #10957
Unanswered
yashpd6634
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi All, I want to extend user object. I mean if user logged in with github or google then we will redirect it to another page where he needs to provide its username to register his account. If he doesn't provide then we will not create its account. how can I implement it?
`
import NextAuth from "next-auth";
import authConfig from "./auth.config";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { db } from "./lib/db";
import { getUserById } from "./lib/user-service";
import { redirect } from "next/navigation";
const createRandomUsername = async () => {
let username =
User#${Math.floor(Math.random() * 10000)}
;let user = await db.user.findUnique({
where: {
username: username,
},
});
while (user) {
username =
user#${Math.floor(Math.random() * 10000)}
;user = await db.user.findUnique({
where: {
username: username,
},
});
}
return username;
};
const adapter = PrismaAdapter(db);
export const { handlers, signIn, signOut, auth } = NextAuth({
pages: {
signIn: "/sign-in",
error: "/authError",
newUser: "/auth/username?isNewUser=true",
},
events: {
async linkAccount({ user }) {
await db.user.update({
where: {
id: user.id,
},
data: {
emailVerified: new Date(),
},
});
},
},
callbacks: {
async signIn({ user, account }) {
if (account?.provider !== "credentials") {
user.username = await createRandomUsername();
return true;
}
},
adapter: PrismaAdapter(db),
session: { strategy: "jwt" },
...authConfig,
});
`
Beta Was this translation helpful? Give feedback.
All reactions