Skip to content

Commit 88a8191

Browse files
feat(users): rework tokens ✨
1 parent 03a5466 commit 88a8191

4 files changed

Lines changed: 29 additions & 33 deletions

File tree

config/defaults/development.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ module.exports = {
104104
},
105105
jwt: {
106106
secret: 'test',
107+
expiresIn: 7 * 24 * 60 * 60, // sec
107108
},
108109
mailer: {
109110
from: 'WAOS_NODE_mailer_from',

modules/users/controllers/users/users.authentication.controller.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ const noReturnUrls = [
2626
exports.signup = async (req, res) => {
2727
try {
2828
const user = await UserService.create(req.body);
29-
const token = jwt.sign({ userId: user.id }, config.jwt.secret);
29+
const token = jwt.sign({ userId: user.id }, config.jwt.secret, { expiresIn: config.jwt.expiresIn });
3030
return res.status(200)
3131
.cookie('TOKEN', token, { httpOnly: true })
32-
.json({ user, tokenExpiresIn: Date.now() + (3600 * 24 * 1000) });
32+
.json({ user, tokenExpiresIn: Date.now() + (config.jwt.expiresIn * 1000) });
3333
} catch (err) {
3434
responses.error(res, 422, errors.getMessage(err))(err);
3535
}
@@ -43,35 +43,36 @@ exports.signup = async (req, res) => {
4343
*/
4444
exports.signin = async (req, res) => {
4545
const user = req.user;
46-
const token = jwt.sign({ userId: user.id }, configuration.jwt.secret);
46+
const token = jwt.sign({ userId: user.id }, configuration.jwt.secret, { expiresIn: config.jwt.expiresIn });
4747
return res.status(200)
4848
.cookie('TOKEN', token, { httpOnly: true })
49-
.json({ user, tokenExpiresIn: Date.now() + (3600 * 24 * 1000) });
49+
.json({ user, tokenExpiresIn: Date.now() + (config.jwt.expiresIn * 1000) });
5050
};
5151

5252
/**
53-
* @desc Endpoint to generate a token
53+
* @desc Endpoint to get a new token if old is ok
5454
* @param {Object} req - Express request object
5555
* @param {Object} res - Express response object
5656
*/
5757
exports.token = async (req, res) => {
58-
try {
59-
// Authenticate the user based on credentials
60-
// @TODO be consistent with whether the login field for user identification
61-
// is a username or an email
62-
const user = await UserService.authenticate(req.body.email, req.body.password);
63-
// Create the token and send
64-
// @TODO properly create the token with all of its metadata
65-
const payload = {
66-
id: user.id,
58+
let user = null;
59+
if (req.user) {
60+
user = {
61+
id: req.user.id,
62+
provider: escape(req.user.provider),
63+
username: escape(req.user.username),
64+
roles: req.user.roles,
65+
profileImageURL: req.user.profileImageURL,
66+
email: escape(req.user.email),
67+
lastName: escape(req.user.lastName),
68+
firstName: escape(req.user.firstName),
69+
additionalProvidersData: req.user.additionalProvidersData,
6770
};
68-
// @TODO properly sign the token, not with a shared secret (use pubkey instead),
69-
// and specify proper expiration, issuer, algorithm, etc.
70-
const token = jwt.sign(payload, config.jwt.secret);
71-
return res.status(200).cookies('TOKEN', token);
72-
} catch (err) {
73-
responses.error(res, 422, errors.getMessage(err))(err);
7471
}
72+
const token = jwt.sign({ userId: user.id }, configuration.jwt.secret, { expiresIn: config.jwt.expiresIn });
73+
return res.status(200)
74+
.cookie('TOKEN', token, { httpOnly: true })
75+
.json({ user, tokenExpiresIn: Date.now() + (config.jwt.expiresIn * 1000) });
7576
};
7677

7778
/**

modules/users/controllers/users/users.profile.controller.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ exports.changeProfilePicture = async (req, res) => {
5858
exports.me = (req, res) => {
5959
// Sanitize the user - short term solution. Copied from core.controller.js
6060
// TODO create proper passport mock: See https://gist.github.com/mweibel/5219403
61-
let safeUserObject = null;
61+
let user = null;
6262
if (req.user) {
63-
safeUserObject = {
63+
user = {
6464
id: req.user.id,
6565
provider: escape(req.user.provider),
6666
username: escape(req.user.username),
@@ -72,7 +72,7 @@ exports.me = (req, res) => {
7272
additionalProvidersData: req.user.additionalProvidersData,
7373
};
7474
}
75-
return responses.success(res, 'user get')(safeUserObject);
75+
return responses.success(res, 'user get')(user);
7676
};
7777

7878
/**
@@ -89,9 +89,9 @@ exports.addOAuthProviderUserProfile = async (req, res) => {
8989
}
9090
if (!user) return responses.error(res, 404, 'No Oauth found')();
9191

92-
const token = jwt.sign({ userId: user.id }, config.jwt.secret);
92+
const token = jwt.sign({ userId: user.id }, config.jwt.secret, { expiresIn: config.jwt.expiresIn });
9393

9494
res.status(200)
9595
.cookie('TOKEN', token, { httpOnly: true })
96-
.json({ user, tokenExpiresIn: Date.now() + (3600 * 24 * 1000) });
96+
.json({ user, tokenExpiresIn: Date.now() + (config.jwt.expiresIn * 1000) });
9797
};

modules/users/routes/auth.routes.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@ module.exports = (app) => {
1919
app.route('/api/auth/signup').post(model.isValid(usersSchema.User), users.signup);
2020
app.route('/api/auth/signin').post(passport.authenticate('local'), users.signin);
2121

22-
// Jwt token
23-
app.route('/api/auth/token').post(model.isValid(usersSchema.User), users.token);
24-
// Jwt protected route example:
25-
// app.route('/api/auth/secretPlace').get(passport.authenticate('jwt'), (req, res) => {
26-
// console.log(req.user)
27-
// console.log(req.isAuthenticated())
28-
// res.status(200).send()
29-
// })
22+
// Jwt reset token
23+
app.route('/api/auth/token').get(passport.authenticate('jwt'), users.token);
3024

3125
// Setting the oauth routes
3226
app.route('/api/auth/:strategy').get(users.oauthCall);

0 commit comments

Comments
 (0)