-
Hello, I would like to know if it is possible for me to write the session cookie with new data because when logging in with facebook I need to consult the user's email in the database to bring the data regarding his account. I used the session to do this but as I call it several times in the application, I end up doing a lot of pull in the api in the backend, and login is only necessary as soon as you log into the application, I would like to know how to create a custom session with the new ones data if it is possible My code bellow: providers: [
Providers.Facebook({
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET
}),
]
database: process.env.DATABASE_URL,
callbacks: {
async session(session: Session) {
try {
const data = {
email: session.user.email,
password: process.env.SOCIAL_PASSWORD,
};
const user: any = await axios
.post(sessionUser, data)
.catch((e) => console.log(e));
const user_info = await showUser(user.data);
const token = user.data.token;
return {
...user_info.data,
token,
};
} catch (error) {
console.log(error);
return {
...session,
};
}
},
async signIn(user, account) {
// Criar o usuário
const data = { ...user, auth_type: account.provider };
try {
await axios.post(createSocialUser, data).catch((e) => console.log(e));
return true;
} catch {
return false;
}
},
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I solved this problem code updated bellow: // Return all data to access them in jwt with profile function
providers: [
Providers.Facebook({
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
profile(profile) {
return {
id: profile.id,
name: profile.name,
email: profile.email,
image: profile.picture.data.url,
};
},
}),
]
// Consume api request in jwt
async jwt(token, profile) {
if (profile) {
const data = {
email: profile.email,
password: process.env.SOCIAL_PASSWORD,
};
const user: any = await axios
.post(sessionUser, data)
.catch((e) => console.log(e));
const user_info = await showUser(user.data);
user_info.data.token = user.data.token;
return { ...user_info.data };
}
return token;
},
// Return user data to session object
async session(session: Session, user: any) {
try {
return { ...user };
} catch (error) {
console.log(error);
return {
...session,
};
}
}, |
Beta Was this translation helpful? Give feedback.
I solved this problem code updated bellow: