Add renew token

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-05-12 13:21:26 +02:00
parent ceb11379fc
commit 298dcac271
3 changed files with 57 additions and 20 deletions

View File

@ -59,6 +59,11 @@
"path": "/login",
"handler": "authentication.login"
},
{
"method": "POST",
"path": "/renew-token",
"handler": "authentication.renewToken"
},
{
"method": "POST",
"path": "/auth/local/register",

View File

@ -3,31 +3,51 @@
const passport = require('koa-passport');
const compose = require('koa-compose');
const login = compose([
(ctx, next) => {
return passport.authenticate('local', { session: false }, (err, user, info) => {
if (err) {
ctx.body = { error: 'Internal server error' };
} else if (!user) {
ctx.body = { error: info.error };
} else {
module.exports = {
login: compose([
(ctx, next) => {
return passport.authenticate('local', { session: false }, (err, user, info) => {
if (err) {
return ctx.badImplementation();
}
if (!user) {
return ctx.badRequest(info.error);
}
ctx.state.user = user;
return next();
}
})(ctx, next);
},
ctx => {
const { user } = ctx.state;
})(ctx, next);
},
ctx => {
const { user } = ctx.state;
ctx.body = {
data: {
token: strapi.admin.services.auth.createJwtToken(user),
user: strapi.admin.services.auth.sanitizeUser(ctx.state.user), // TODO: fetch more detailed info
},
};
},
]),
renewToken(ctx) {
const { token } = ctx.request.body;
if (token === undefined) {
return ctx.badRequest('Token is required.');
}
const { isValid, payload } = strapi.admin.services.auth.decodeToken(token);
if (!isValid) {
return ctx.badRequest('Invalid token.');
}
ctx.body = {
data: {
token: strapi.admin.services.auth.createJwtToken(user),
user: strapi.admin.services.auth.sanitizeUser(ctx.state.user), // TODO: fetch more detailed info
token: strapi.admin.services.auth.createJwtToken(payload.id),
},
};
},
]);
module.exports = {
login,
};

View File

@ -8,7 +8,7 @@ const sanitizeUser = user => {
return _.omit(user, ['password', 'resetPasswordToken']);
};
const defaultOptions = { expiresIn: '30d' };
const defaultOptions = { expiresIn: '1s' };
const getJWTOptions = () => {
const { options, secret } = strapi.config.get('server.admin.jwt', {});
@ -78,6 +78,17 @@ const checkCredentials = async ({ email, password }) => {
return [null, user];
};
const decodeToken = token => {
const { secret } = getJWTOptions();
try {
const payload = jwt.verify(token, secret);
return { payload, isValid: true };
} catch (err) {
return { payloda: null, isValid: false };
}
};
module.exports = {
checkCredentials,
createJwtToken,
@ -85,4 +96,5 @@ module.exports = {
validatePassword,
hashPassword,
getJWTOptions,
decodeToken,
};