Reputation: 721
I am trying to store the jwt token returned from passport.js in browser localStorage, I am having some issues with communicating from the server to the client since the token is being generated from the server.
I will appreciate any help on how to set the token generated by the server on the client browser localStorage.
Routes
// Setting the github oauth routes
app.get('/auth/github', passport.authenticate('github', {
failureRedirect: '/signin'
}), authCallback);
app.get('/auth/github/callback', passport.authenticate('github', {
failureRedirect: '/signin'
}), authCallback);
passport.js
passport.use(new GitHubStrategy(
{
clientID: process.env.GITHUB_CLIENT_ID || config.github.clientID,
clientSecret: process.env.GITHUB_CLIENT_SECRET || config.github.clientSecret,
callbackURL: config.github.callbackURL
},
((accessToken, refreshToken, profile, done) => {
User.findOne({
'github.id': profile.id
}, (err, user) => {
if (err) {
return done(err);
}
if (!user) {
user = new User({
name: profile.displayName,
username: profile.username,
provider: 'github',
github: profile._json
});
user.save(() => done(err, user));
} else {
return done(err, user);
}
});
})
));
AuthCallBack
export const authCallback = (req, res) => {
const { TOKEN_SECRET } = process.env;
if (!req.user) {
res.redirect('/#!/signin?error=emailRequired');
} else {
const token = jwt.sign(
{ user: req.user.id, name: req.user.name },
TOKEN_SECRET,
{ expiresIn: 72 * 60 * 60 }
);
// window.localStorage.setItem('token', token);
res.redirect('/#!/app');
}
};
I will appreciate any help to store the token from authCallBack in my browser localStorage.
Upvotes: 1
Views: 2680
Reputation: 721
I had to fix this by setting the data in the browser cookie and then retrieving the stored data in the client side then storing the data in the browser localStorage.
Upvotes: 2