next-auth prisma adapter add username to provider
Unanswered
Chinese Egret posted this in #help-forum
Chinese EgretOP
I want the user who logs in to additionally create a username, in case of not creating it or canceling the transaction the account is not saved in the mongodb database.
I have a problem that does not recognize username in session and in redirect it does not find user.
Here is the code:
import NextAuth from "next-auth";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { PrismaClient } from "@prisma/client";
import Google from "next-auth/providers/google";
import GithHub from "next-auth/providers/github";
const prisma = new PrismaClient();
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [Google, GithHub],
callbacks: {
async signIn({ user, account, profile }) {
const existingUser = await prisma.user.findUnique({
where: { email: user.email as string },
});
if (existingUser && existingUser.username) {
return true;
}
if (!existingUser) {
return true;
}
return false;
},
async session({ session, token, user }) {
session.user.username = user.username;
return session;
},
async redirect({ url, baseUrl }) {
if (!user.username) {
return ${baseUrl}/create-username;
}
return url;
},
},
session: {
strategy: "jwt",
},
});
Errors: Property 'username' does not exist on type 'AdapterUser & User'.
Cannot find name 'user'.
then i show the prisma scheme
I have a problem that does not recognize username in session and in redirect it does not find user.
Here is the code:
import NextAuth from "next-auth";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { PrismaClient } from "@prisma/client";
import Google from "next-auth/providers/google";
import GithHub from "next-auth/providers/github";
const prisma = new PrismaClient();
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [Google, GithHub],
callbacks: {
async signIn({ user, account, profile }) {
const existingUser = await prisma.user.findUnique({
where: { email: user.email as string },
});
if (existingUser && existingUser.username) {
return true;
}
if (!existingUser) {
return true;
}
return false;
},
async session({ session, token, user }) {
session.user.username = user.username;
return session;
},
async redirect({ url, baseUrl }) {
if (!user.username) {
return ${baseUrl}/create-username;
}
return url;
},
},
session: {
strategy: "jwt",
},
});
Errors: Property 'username' does not exist on type 'AdapterUser & User'.
Cannot find name 'user'.
then i show the prisma scheme
1 Reply
Chinese EgretOP
prisma scheme: generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model Account {
id String @id @default(cuid()) @map("_id")
userId String
type String
provider String
providerAccountId String
refresh_token String?
access_token String?
expires_at Int?
token_type String?
scope String?
id_token String?
session_state String?
oauth_token_secret String?
oauth_token String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}
model Session {
id String @id @default(cuid()) @map("_id")
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model User {
id String @id @default(cuid()) @map("_id")
name String?
username String @unique
email String? @unique
emailVerified DateTime?
image String?
Account Account[]
Session Session[]
}
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model Account {
id String @id @default(cuid()) @map("_id")
userId String
type String
provider String
providerAccountId String
refresh_token String?
access_token String?
expires_at Int?
token_type String?
scope String?
id_token String?
session_state String?
oauth_token_secret String?
oauth_token String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}
model Session {
id String @id @default(cuid()) @map("_id")
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model User {
id String @id @default(cuid()) @map("_id")
name String?
username String @unique
email String? @unique
emailVerified DateTime?
image String?
Account Account[]
Session Session[]
}