fixes bugs within refresh handler
This commit is contained in:
13
.vscode/launch.json
vendored
13
.vscode/launch.json
vendored
@@ -4,6 +4,19 @@
|
|||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Debug Jest Tests",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"runtimeArgs": [
|
||||||
|
"--inspect-brk",
|
||||||
|
"${workspaceRoot}/node_modules/.bin/jest",
|
||||||
|
"--runInBand"
|
||||||
|
],
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"internalConsoleOptions": "neverOpen",
|
||||||
|
"port": 9229
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "node",
|
"type": "node",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
|
|||||||
@@ -9,7 +9,8 @@
|
|||||||
"cov": "npm run core:build && node ./node_modules/jest/bin/jest.js src/tests --coverage",
|
"cov": "npm run core:build && node ./node_modules/jest/bin/jest.js src/tests --coverage",
|
||||||
"dev": "nodemon demo/server.js",
|
"dev": "nodemon demo/server.js",
|
||||||
"server": "node demo/server.js",
|
"server": "node demo/server.js",
|
||||||
"lint": "eslint **/*.js"
|
"lint": "eslint **/*.js",
|
||||||
|
"debug:test:int": "node --inspect-brk node_modules/.bin/jest --runInBand"
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"payload": "./src/bin/index.js"
|
"payload": "./src/bin/index.js"
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ const UserProvider = ({ children }) => {
|
|||||||
|
|
||||||
if (request.status === 200) {
|
if (request.status === 200) {
|
||||||
const json = await request.json();
|
const json = await request.json();
|
||||||
|
console.log('refreshedToken', json.refreshedToken);
|
||||||
setToken(json.refreshedToken);
|
setToken(json.refreshedToken);
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ const refresh = async (args) => {
|
|||||||
// 1. Execute before refresh hook
|
// 1. Execute before refresh hook
|
||||||
// /////////////////////////////////////
|
// /////////////////////////////////////
|
||||||
|
|
||||||
const beforeRefreshHook = args.config.hooks && args.config.hooks.beforeRefresh;
|
const { beforeRefresh } = args.config.hooks;
|
||||||
|
|
||||||
if (typeof beforeRefreshHook === 'function') {
|
if (typeof beforeRefresh === 'function') {
|
||||||
options = await beforeRefreshHook(options);
|
options = await beforeRefresh(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// /////////////////////////////////////
|
// /////////////////////////////////////
|
||||||
@@ -29,17 +29,19 @@ const refresh = async (args) => {
|
|||||||
opts.expiresIn = options.config.auth.tokenExpiration;
|
opts.expiresIn = options.config.auth.tokenExpiration;
|
||||||
|
|
||||||
const token = options.authorization.replace('JWT ', '');
|
const token = options.authorization.replace('JWT ', '');
|
||||||
jwt.verify(token, secret, {});
|
const payload = jwt.verify(token, secret, {});
|
||||||
const refreshedToken = jwt.sign(token, secret);
|
delete payload.iat;
|
||||||
|
delete payload.exp;
|
||||||
|
const refreshedToken = jwt.sign(payload, secret, opts);
|
||||||
|
|
||||||
// /////////////////////////////////////
|
// /////////////////////////////////////
|
||||||
// 3. Execute after login hook
|
// 3. Execute after login hook
|
||||||
// /////////////////////////////////////
|
// /////////////////////////////////////
|
||||||
|
|
||||||
const afterRefreshHook = args.config.hooks && args.config.hooks.afterRefresh;
|
const { afterRefresh } = args.config.hooks;
|
||||||
|
|
||||||
if (typeof afterRefreshHook === 'function') {
|
if (typeof afterRefresh === 'function') {
|
||||||
await afterRefreshHook(options, refreshedToken);
|
await afterRefresh(options, refreshedToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
// /////////////////////////////////////
|
// /////////////////////////////////////
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ const refreshHandler = async (req, res) => {
|
|||||||
config: req.collection,
|
config: req.collection,
|
||||||
api: 'REST',
|
api: 'REST',
|
||||||
authorization: req.headers.authorization,
|
authorization: req.headers.authorization,
|
||||||
|
user: req.user,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const updateHandler = async (req, res) => {
|
|||||||
api: 'REST',
|
api: 'REST',
|
||||||
locale: req.locale,
|
locale: req.locale,
|
||||||
fallbackLocale: req.fallbackLocale,
|
fallbackLocale: req.fallbackLocale,
|
||||||
|
user: req.user,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(httpStatus.OK).json({
|
return res.status(httpStatus.OK).json({
|
||||||
|
|||||||
@@ -8,12 +8,12 @@ const { email, password } = require('../tests/credentials');
|
|||||||
|
|
||||||
const config = require('../../demo/payload.config');
|
const config = require('../../demo/payload.config');
|
||||||
|
|
||||||
|
const url = config.serverURL;
|
||||||
|
const usernameField = config.user.auth.useAsUsername;
|
||||||
|
|
||||||
|
let token = null;
|
||||||
|
|
||||||
describe('Users REST API', () => {
|
describe('Users REST API', () => {
|
||||||
const url = config.serverURL;
|
|
||||||
const usernameField = config.user.auth.useAsUsername;
|
|
||||||
|
|
||||||
let token = null;
|
|
||||||
|
|
||||||
it('should prevent registering a first user', async () => {
|
it('should prevent registering a first user', async () => {
|
||||||
const response = await fetch(`${url}/api/first-register`, {
|
const response = await fetch(`${url}/api/first-register`, {
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -76,7 +76,7 @@ describe('Users REST API', () => {
|
|||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(data.token).not.toBeNull();
|
expect(data.token).not.toBeNull();
|
||||||
|
|
||||||
({ token } = data);
|
// token = data.refreshedToken;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow a user to be created', async () => {
|
it('should allow a user to be created', async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user