gpt4 book ai didi

javascript - 自定义 session next js next-auth

转载 作者:行者123 更新时间:2023-12-04 14:07:01 25 4
gpt4 key购买 nike

迁移我的 js 文件 jo tsx 时遇到问题,我正在做的是使用凭据登录并将 session 用户自定义到我的用户数据

// api/auth/[...nextauth].js

import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import { ConnectDatabase } from "../../../lib/db";
import { VertifyPassword } from "../../../lib/password";
import { getSelectedUser } from "../../../helpers/database";
import { MongoClient } from "mongodb";
import { NextApiRequest } from "next";

interface credentialsData {
data: string | number;
password: string;
}
export default NextAuth({
session: {
jwt: true,
},
callbacks: {
async session(session) {
const data = await getSelectedUser(session.user.email);
session.user = data.userData;

// inside data.userdata is a object
// {
// _id: '60a92f328dc04f58207388d1',
// email: 'user@user.com',
// phone: '087864810221',
// point: 0,
// role: 'user',
// accountstatus: 'false'
// }
return Promise.resolve(session);
},
},
providers: [
Providers.Credentials({
async authorize(credentials: credentialsData, req: NextApiRequest) {
let client;
try {
client = await ConnectDatabase();
} catch (error) {
throw new Error("Failed connet to database.");
}

const checkEmail = await client
.db()
.collection("users")
.findOne({ email: credentials.data });
const checkPhone = await client
.db()
.collection("users")
.findOne({ phone: credentials.data });

let validData = {
password: "",
email: "",
};

if (!checkEmail && !checkPhone) {
client.close();
throw new Error("Email atau No HP tidak terdaftar.");
} else if (checkEmail) {
validData = checkEmail;
} else if (checkPhone) {
validData = checkPhone;
}

const checkPassword = await VertifyPassword(
credentials.password,
validData.password
);
if (!checkPassword) {
client.close();
throw new Error("Password Salah.");
}
client.close();

// inside validData is a object
// {
// _id: '60a92f328dc04f58207388d1',
// email: 'user@user.com',
// phone: '087864810221',
// point: 0,
// role: 'user',
// accountstatus: 'false'
// }

return validData;
},
}),
],
});
// as default provider just return session.user just return email,name, and image, but I want custom the session.user to user data what I got from dababase
这在客户端
// index.tsx

export const getServerSideProps: GetServerSideProps<{
session: Session | null;
}> = async (context) => {
const session = await getSession({ req: context.req });

if (session) {
if (session.user?.role === "admin") {
return {
redirect: {
destination: "/admin/home",
permanent: false,
},
};
}
}
return {
props: {
session,
},
};
};
但在客户端我收到警告
Property 'role' does not exist on type '{ name?: string; email?: string; image?: string; 
实际上我的文件仍然可以正常工作,但是当我的文件在 js格式,它不是那样的警告
有人可以帮我解决吗?

最佳答案

不确定您是否找到了解决方法,但您还需要配置 jwt 回调!这是我的一个项目的一个例子:

callbacks: {
async session(session, token) {
session.accessToken = token.accessToken;
session.user = token.user;
return session;
},
async jwt(token, user, account, profile, isNewUser) {
if (user) {
token.accessToken = user._id;
token.user = user;
}
return token;
},
},

解释事情。 jwt 函数始终在 session 之前运行,因此您传递给 jwt token 的任何数据都将在 session 函数上可用,您可以使用它做任何您想做的事情。在 jwt 函数中,我检查是否有用户,因为这仅在您登录时才返回数据。

关于javascript - 自定义 session next js next-auth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67749917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com