早上好,
我是NextJs的新手,我正在与Google提供商进行身份验证,问题是它只返回我一个名字,我试图确保我可以拆分它,以便我的名字和姓氏在我的数据库中正确填写
我提供给你的代码我目前有
我的prisma模式:
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}
model Session {
id String @id @default(cuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model User {
id String @id @default(cuid())
firstname String?
lastname String?
email String? @unique
password String?
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}
字符串
我的下一个目标
declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user: DefaultSession["user"] & {
/** The user's id address. */
id?: string;
firstname?: string;
lastname?: string;
};
}
interface User {
firstname?: string;
lastname?: string;
}
}
型
和[. nextauth].ts文件
import NextAuth, { AuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "@/src/lib/db";
export const authOptions: AuthOptions = {
adapter: PrismaAdapter(prisma),
providers: [
GoogleProvider({
clientId: env.GOOGLE_ID,
clientSecret: env.GOOGLE_SECRET,
}),
],
callbacks: {
session({ session, user }) {
session.user.id = user.id;
session.user.image = user.image;
return session;
},
},
pages: {
signIn: "/login",
},
};
export default NextAuth(authOptions);
型
希望你能帮我做到这一点提前感谢!
1条答案
按热度按时间bfhwhh0e1#
嘿,所以如果你想从谷歌提供商那里访问用户的名字和姓氏,你应该看看下面的NextAuth文档。
下面是一个例子,可能有助于展示我是如何做类似的事情的。
第一个月
字符串
auth.ts
个型
PSA:我会删除用户的界面,并使用
import { Account, Profile, Session, User } from "next-auth";
的用户界面,因为它已经有id,name,email和image作为默认值。