Wil*_*lyo 4 javascript authentication json next.js next-auth
接下来,当我使用带有身份验证凭据的 SignIn 或 SignOut 时,出现未处理的运行时错误 SyntaxError: Unexpected end of JSON input。
该错误很奇怪,因为显示弹出下一个错误,但注册了令牌和会话
这是我的授权码和凭据:
CredentialsProvider({
id: 'credentials',
name: 'Credenciales',
async authorize(credentials, req) {
if (!(credentials.email.length > 0) || !(credentials.password.length > 0)) {
throw new Error('Email or password was not provided');
}
const formData = new FormData();
formData.append('username', credentials.email);
formData.append('password', credentials.password);
formData.append('client_id', credentials.rif);
const url = `${process.env.BACK_ENDPOINT}/login/`
const response = await fetch(url,{
method: 'POST',
body: formData
});
if (response.status === 401) {
return {
'details': 'error',
'message': 'The username or password is not valid'
}
}
if (response.status === 500) {
throw new Error('Have been an error');
}
const data = await response.json();
if (response.status > 400 && response.status < 500) {
console.error(data)
if (data.detail) {
throw data.message;
}
throw data;
}
if(data.details === 'success'){
return data
}
return null
}
}),
Run Code Online (Sandbox Code Playgroud)
这是我的登录回调:
signIn: async({ user, account, profile, email, credentials }) => {
// console.log('signIn', {user, account, profile, email, credentials})
if (account.type === 'credentials'){
if(user.details === 'success') return true
if(user.details === 'error') return false
}
},
Run Code Online (Sandbox Code Playgroud)
这是我在前端的 onsubmit 句柄:
const objectValues= {
email: values.email,
password: values.password,
rif: values.rif,
callbackUrl: '/',
}
signIn('credentials', objectValues)
Run Code Online (Sandbox Code Playgroud)
我发现“错误”:在 [...nextauth].js 的回调中,我进行了重定向回调,那里没有任何内容,它总是执行而不返回:
Callbacks:{
jwt: ({token, user, account, isNewUser}) =>{
some code...
},
redirect: async ({url, baseUrl}) => {
// NOTHING OF CODE
},
session: ({session, token}) => {
some code...
},
signIn: async({ user, account, profile, email, credentials }) => {
some code...
}
}
Run Code Online (Sandbox Code Playgroud)
下次我将提供有关我的代码和问题的更多详细信息,抱歉。感谢@Bravo抽出时间。
| 归档时间: |
|
| 查看次数: |
2242 次 |
| 最近记录: |