I tried to add a localStorage of my token when the process is done so i can get it in my react component (let tok = localStorage.getItem('jwtGoogleToken');)
like this :
if (account.enable === true) {
//send token
try {
jwt.sign(
{
account,
},
"fatboardauthentication",
// config.secretKey,
(err, token) => {
if (err) {
done(err);
} else {
localStorage.setItem("jwtGoogleToken", token); // this wont work
done(null, token);
}
// localStorage.setItem("jwtGoogleToken", token); // this wont work too
}
);
} catch (e) {
done(err);
}
}
La ligne localStorage.setItem("jwtGoogleToken", token); inside the else will not work, and when i click on my email to connect, i have this screen below and it still like this, charging infinitely :
if i move the localstorage outside the else it doesn't work too, i can't see the token in the browser.
this is the complete code :
passport.use(
new GoogleStrategy(
{
// options for google strategy
clientID:config.oauth.google.clientID,
clientSecret: config.oauth.google.clientSecret,
callbackURL: config.oauth.google.redirect,
},
(accessToken, refreshToken, profile, done) => {
// passport callback function
console.log("passport callback function fired:");
if (profile !== null) {
console.log("Contenu de profile :", profile);
var email = profile.emails[0].value;
var nom = profile.name.familyName;
var prenom = profile.name.givenName;
var googleId = profile.id;
Account.findOne({ email: email }, async (err, account) => {
if (err) {
console.log("ERRORS 1 ???? ", err);
done(err);
}
if (account) {
//exist
if (err) {
console.log("ERRORS 2 ???? ", err);
done(err);
} else {
//get account
console.log("currentAccount");
//check if enable = true
if (account.enable === true) {
//send token
try {
jwt.sign(
{
account,
},
config.secretKey,
(err, token) => {
if (err) {
done(err);
} else {
localStorage.setItem("jwtGoogleToken", token);
done(null, token);
}
// localStorage.setItem("jwtGoogleToken", token);
}
);
} catch (e) {
done(err);
}
} else {
done(
"This account is disabled, please contact support for more information",
null
);
}
}
} else {
//new account
if (err) {
done(err);
}
const accountA = new Account({
email: email,
nom: nom,
prenom: prenom,
acceptCGU: true,
google: {
id: googleId,
email: email,
name: nom + " " + prenom,
},
});
//save data
await accountA.save((err) => {
if (err) {
done(err);
} else {
console.log("newaccount");
//send email
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: config.supportemail,
pass: config.gmailPSW,
},
});
var mailOptions = {
to: email,
from: config.supportemail,
subject: "Bienvenue chez FATBOAR !",
text:
"Bienvenue chez FATBOAR ! \n \n Votre compte : " +
email +
" a bien été créé. \n \n Nos dernières offres exclusives n'attendent plus que vous sur notre site Fatboar.\n A bientôt ! \n \n L'équipe FATBOAR",
};
smtpTransport.sendMail(mailOptions, function (err) {
if (err) {
res.status(400);
resultats = {
success: false,
message: "Error : " + err,
result: "",
};
res.json(resultats);
} else {
//send token
try {
jwt.sign(
{
accountA,
},
config.secretKey,
(err, token) => {
if (err) {
done(err);
} else {
done(null, token);
}
}
);
} catch (e) {
done(err);
}
}
});
}
});
}
});
}
}
)
);