我创建了一个
api路由
来处理
获取所有地址
c
当前登录的用户
已创建。但我只得到一个空数组。我想象req无法获得当前登录用户的ID。我检查了下一个配置
我拿到了身份证
。更确切地说,我在nextauth上console.log会话配置,并获得如下ID:
ð ~ session ~ session: {
user: {
name: 'Nhung Nguyen',
email: '[email protected]',
image: 'https://lh3.googleusercontent.com/a/ACg8ocLg8TfKh72d8RDiUO9xIxR7CTgR4e6hU8WpwTtlXHEDEIg=s96-c',
id: '65ff0b9c80353678f60e337a'
},
expires: '2024-04-27T13:26:30.889Z'
}
但在api路由句柄getAllAddresses上,我收到的会话只有名称、电子邮件、图像,没有ID。这是我在api路由手柄getAllAddresses:
ð ~ GET ~ session: {
user: {
name: 'Nhung Nguyen',
email: '[email protected]',
image: 'https://lh3.googleusercontent.com/a/ACg8ocLg8TfKh72d8RDiUO9xIxR7CTgR4e6hU8WpwTtlXHEDEIg=s96-c'
}
}
这是我在api/auth/[…nexttauth]/route.js中的代码
const handler = NextAuth({
providers: [
CredentialsProvider({
name: 'credentials',
credentials: {},
async authorize(credentials, req) {
await connect()
const user = await User.findOne({ email: credentials.email })
if (!user) {
throw new Error('ÄÄng nháºp không hợp lá»!')
}
const comparePassword = await bcrypt.compare(credentials.password, user.password)
if (!comparePassword) {
throw new Error('ÄÄng nháºp không hợp lá»!')
}
return user
}
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET
}),
],
session: {
strategy: 'jwt'
},
pages: {
signIn: '/login'
},
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async signIn({ profile, account }) {
if (account.provider === 'google') {
try {
await connect()
let user = await User.findOne({ email: profile.email })
if (!user) {
user = await User.create({
email: profile.email,
name: profile.name,
avatar: profile.image,
wishlist: [],
cart: [],
orders: [],
products: []
})
}
return user
} catch (error) {
console.log(error)
}
}
return true
},
async jwt({ token, user }) {
user && (token.user = user)
return token
},
async session({ session }) {
const sessionUser = await User.findOne({ email: session.user.email })
// console.log('ð ~ session ~ sessionUser:', sessionUser)
session.user.id = sessionUser._id.toString()
console.log('ð ~ session ~ session.user.id:', session.user.id)
console.log('ð ~ session ~ session:', session)
return session
}
}
})
export { handler as GET, handler as POST }
这是我在api路由句柄getAllAddresses处的代码
export async function GET(req) {
try {
const session = await getServerSession({ req })
console.log('ð ~ GET ~ session:', session)
if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const getAllAddresses = await Address.find({ userID: session.user.id })
if (getAllAddresses) {
return NextResponse.json({
success: true,
data: getAllAddresses,
});
} else {
return NextResponse.json({
success: false,
message: "failed to get addresses ! Please try again",
});
}
} catch (error) {
console.error('Error fetching user addresses:', error)
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
}
}