Extending the user model in NextAuth with required unique fields
Unanswered
Brewer's Blackbird posted this in #help-forum
Brewer's BlackbirdOP
auth.ts
prisma.schema
Does anyone know how to implement it?
After user signed in via provider (new account):
-create user with name, email, image, etc and username
-username should take its value from name, if prisma will return an error (username is already taken), then try to set username to "name+random_number_from_1000_to_9000"
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
username: string;
} & DefaultSession["user"];
}
interface User {
username: string;
}
}
export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
username: user.username,
},
}),
},
adapter: PrismaAdapter(prisma),
providers: [
GithubProvider({
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
}),
],
}; prisma.schema
model User {
id String @id @default(cuid())
name String? @db.VarChar(20)
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
username String @unique @db.VarChar(20)
}Does anyone know how to implement it?
After user signed in via provider (new account):
-create user with name, email, image, etc and username
-username should take its value from name, if prisma will return an error (username is already taken), then try to set username to "name+random_number_from_1000_to_9000"
13 Replies
try searching using the
Augmentation keyword in Next-Auth docs@alfon you have to augment your types using `declare` in a `d.ts` file to extend User model
Brewer's BlackbirdOP
It's not what i'm talking about, i already augmented the type
After user signed in via provider (new account):
-create user with name, email, image, etc and username
-username should take its value from name, if prisma will return an error (username is already taken), then try to set username to "name+random_number_from_1000_to_9000"
@Brewer's Blackbird It's not what i'm talking about, i already augmented the type
Brewer's BlackbirdOP
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
username: string;
} & DefaultSession["user"];
}
interface User {
username: string;
}
}This is from the code above
Could you please check this question: https://nextjs-forum.com/post/1117929294879019130
I'm trying to do almost the same thing.
User signs in with github, if they are new user, then before creating user add
User signs in with github, if they are new user, then before creating user add
username: "..."I foung this inside of a GithubProvider:
So is adding additional fields to
export default function Github<P extends GithubProfile>(
options: OAuthUserConfig<P>
): OAuthConfig<P> {
[...]
profile(profile) {
return {
id: profile.id.toString(),
name: profile.name ?? profile.login,
email: profile.email,
image: profile.avatar_url,
}
},
[...]
}So is adding additional fields to
profile funcition is the best approach?For example:
profile(profile) {
return {
id: profile.id.toString(),
name: profile.name ?? profile.login,
email: profile.email,
image: profile.avatar_url,
username: `${profile}${Math.random(1000, 9999)}`
}
},Something like this
Or is there a way better approach?
@Brewer's Blackbird *auth.ts*
ts
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
username: string;
} & DefaultSession["user"];
}
interface User {
username: string;
}
}
export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
username: user.username,
},
}),
},
adapter: PrismaAdapter(prisma),
providers: [
GithubProvider({
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
}),
],
};
*prisma.schema*
model User {
id String @id @default(cuid())
name String? @db.VarChar(20)
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
username String @unique @db.VarChar(20)
}
Does anyone know how to implement it?
After user signed in via provider (new account):
-create user with name, email, image, etc and username
-username should take its value from name, if prisma will return an error (username is already taken), then try to set username to "name+random_number_from_1000_to_9000"
you can do that in the session callback, its almost correct. However that means you have to combine the mechanism with the adapter.
The point of adapter is that it already take care of db stuff for you so
The point of adapter is that it already take care of db stuff for you so
might worth checking out if in the adapter you can modify its behavior