Currently, I am in the process of developing a customized credentialProvider using NextAuth. Below is my logical login implementation:
credentials: {
email: { label: 'email', type: 'email', placeholder: 'Your email' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials) {
try {
const { email, password } = credentials;
const login: Awaited<Promise<LoginResponse>> = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}${process.env.NEXT_PLUBIC_API_LOGIN}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
},
)
.then((res) => res.json() as Promise<LoginResponse>)
.then((json) => json);
const {
token,
user: { _id, name },
} = login;
if (!token || !name || !_id) return null;
return {
token,
name,
email,
id: _id,
};
} catch (error) {
return null;
}
},
}),
The structure of my user object is as follows:
{
token: string;
name: string,
email: string,
id: string
}
However, I have encountered an issue where the USER object does not contain the TOKEN key in my jwt's callbacks function:
callbacks: {
jwt: async ({ token, user }) => {
const isSignIn = !!user;
const currentDateInSeconds = Math.floor(Date.now() / 1000);
const expirateDateInSeconds = Math.floor(7 * 24 * 60 * 60);
if (isSignIn) {
token.email = user.email;
token.name = user.name;
token.token = user.token; //<<<-- error on this line
token.id = user.id;
token.expiration = Math.floor(
currentDateInSeconds + expirateDateInSeconds,
);
}
return token;
},
},
I am considering extending the type User | AdapterUser. What steps should I take to achieve this?