如何在 NEXT-AUTH 中向 signIn Promise return 添加额外的数据?
How to add additional data to signIn Promise return in NEXT-AUTH?
这就是我们在我们网站上授权用户的方式
signIn('credentials', {
phoneNumber: verifiedPhone,
code: otp.data,
type: 'phone',
redirect: false,
}).then((res) => {
console.log(res) // default response {error,status,ok,url}
// How can i add additional data to this response, in my case user session
// if (!res.user.name) openModal('add_name')
// else toast.success('You are all set!')
});
默认情况下,登录将 return 一个承诺,解决:
{
error: string | undefined
status: number
ok: boolean
url: string | null
}
我们想为这个承诺添加自定义数据 return。
实际上我们想要做的是让用户登录,如果用户是新用户,he/she 应该没有用户名,所以会打开一个模式,输入 his/her 用户名,然后我们更新下一个授权会话。
[...nextauth].js
:
...
async authorize(credentials, req) {
// check the code here
const res = await requests.auth.signInEnterOtp(
credentials.phoneNumber,
credentials.code,
credentials.type
);
if (!res.ok) return null
return {
user: {
access_token: res.data?.access_token,
token_type: res.data?.token_type,
expires_at: res.data?.expires_at,
user_info: {
id: res.data?.user.id,
name: res.data?.user.name,
phone: res.data?.user.phone,
user_type: res.data?.user.user_type,
},
},
};
},
...
我最终是这样想的:
...
.then(async (res) => {
const session = await getSession()
...
})
...
但是我还有另一个问题,就是用新的用户名更新会话(
编辑
我找到了登录后更改会话的方法
[...nextauth].js
:
...
async authorize(credentials, req){
...
if(credentials.type === 'update_name'){
const session = await getSession({ req })
return session.user.name = credentails.name
}
...
}
在客户端上:
signIn('credentials', {
name: newName,
type: 'name_update',
redirect: false
)
这就是我们在我们网站上授权用户的方式
signIn('credentials', {
phoneNumber: verifiedPhone,
code: otp.data,
type: 'phone',
redirect: false,
}).then((res) => {
console.log(res) // default response {error,status,ok,url}
// How can i add additional data to this response, in my case user session
// if (!res.user.name) openModal('add_name')
// else toast.success('You are all set!')
});
默认情况下,登录将 return 一个承诺,解决:
{
error: string | undefined
status: number
ok: boolean
url: string | null
}
我们想为这个承诺添加自定义数据 return。
实际上我们想要做的是让用户登录,如果用户是新用户,he/she 应该没有用户名,所以会打开一个模式,输入 his/her 用户名,然后我们更新下一个授权会话。
[...nextauth].js
:
...
async authorize(credentials, req) {
// check the code here
const res = await requests.auth.signInEnterOtp(
credentials.phoneNumber,
credentials.code,
credentials.type
);
if (!res.ok) return null
return {
user: {
access_token: res.data?.access_token,
token_type: res.data?.token_type,
expires_at: res.data?.expires_at,
user_info: {
id: res.data?.user.id,
name: res.data?.user.name,
phone: res.data?.user.phone,
user_type: res.data?.user.user_type,
},
},
};
},
...
我最终是这样想的:
...
.then(async (res) => {
const session = await getSession()
...
})
...
但是我还有另一个问题,就是用新的用户名更新会话(
编辑
我找到了登录后更改会话的方法
[...nextauth].js
:
...
async authorize(credentials, req){
...
if(credentials.type === 'update_name'){
const session = await getSession({ req })
return session.user.name = credentails.name
}
...
}
在客户端上:
signIn('credentials', {
name: newName,
type: 'name_update',
redirect: false
)