Issue is after successful authorize and return user + jwt token it’s not getting in any of the callbacks
options for NextAuth are as follows:
import GoogleProvider from 'next-auth/providers/google';
import CredentialsProvider from 'next-auth/providers/credentials';
import base_url from '@/app/_util/StrapiConfig';
// Write options for NextAuth with providers, authorize and callbacks for Credentials provider here
export const options = {
providers: [
CredentialsProvider({
name: 'Credentials',
credentials: {
email: { label: "Email", type: "text", placeholder: "Please enter your email" },
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
const { email, password } = credentials;
try {
fetch(base_url + `/auth/local`, {
headers: {
'Authorization': `Bearer ${process.env.NEXT_PUBLIC_STRAPI_TOKEN}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
identifier: email,
password: password,
})
}).then(res => res.json()).then(response => {
const cond = response?.jwt !== null;
if(cond !== null) {
return response;
} else if(response.status === 403) {
throw new Error('User with these email is not registered!');
// redirect signup page
} else if(response.status === 401) {
throw new Error('Invalid credentials!');
// redirect login page
} else {
throw new Error('Something went wrong!');
}
})
.catch(error => {
});
} catch (error) {
}
}
})
],
session: {
strategy: "jwt",
},
secret: process.env.AUTH_SECRET,
debug: process.env.NODE_ENV === 'development',
callbacks: {
async session({session, user, token}) {
console.log("session", session);
session.user.email = user.email;
return session;
},
async jwt({token, user}) {
console.log("token", token);
if(user) {
token.user = user;
token.jwt = user.jwt;
}
return token;
},
}
}
I’m getting user in authorize
also getting 200 log in backend but not getting in any of callback niether session
nor jwt
next.js app logs are as follows:
GET /api/auth/signin 200 in 654ms
POST /api/auth/callback/credentials 302 in 16ms
GET /api/auth/error?error=CredentialsSignin&provider=credentials 302 in 7ms
GET /api/auth/signin?error=CredentialsSignin 200 in 7ms
Backend log is as follows:
[2024-07-27 14:47:45.855] http: POST /api/auth/local (1283 ms) 200
I have watched n number of tutorials and everyone is doing the same, I have been stuck at this point for hours now!