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
|
||||
"version": "0.2.0",
|
||||
"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",
|
||||
"request": "launch",
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
"cov": "npm run core:build && node ./node_modules/jest/bin/jest.js src/tests --coverage",
|
||||
"dev": "nodemon 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": {
|
||||
"payload": "./src/bin/index.js"
|
||||
|
||||
@@ -37,6 +37,7 @@ const UserProvider = ({ children }) => {
|
||||
|
||||
if (request.status === 200) {
|
||||
const json = await request.json();
|
||||
console.log('refreshedToken', json.refreshedToken);
|
||||
setToken(json.refreshedToken);
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
@@ -14,10 +14,10 @@ const refresh = async (args) => {
|
||||
// 1. Execute before refresh hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
const beforeRefreshHook = args.config.hooks && args.config.hooks.beforeRefresh;
|
||||
const { beforeRefresh } = args.config.hooks;
|
||||
|
||||
if (typeof beforeRefreshHook === 'function') {
|
||||
options = await beforeRefreshHook(options);
|
||||
if (typeof beforeRefresh === 'function') {
|
||||
options = await beforeRefresh(options);
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -29,17 +29,19 @@ const refresh = async (args) => {
|
||||
opts.expiresIn = options.config.auth.tokenExpiration;
|
||||
|
||||
const token = options.authorization.replace('JWT ', '');
|
||||
jwt.verify(token, secret, {});
|
||||
const refreshedToken = jwt.sign(token, secret);
|
||||
const payload = jwt.verify(token, secret, {});
|
||||
delete payload.iat;
|
||||
delete payload.exp;
|
||||
const refreshedToken = jwt.sign(payload, secret, opts);
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 3. Execute after login hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
const afterRefreshHook = args.config.hooks && args.config.hooks.afterRefresh;
|
||||
const { afterRefresh } = args.config.hooks;
|
||||
|
||||
if (typeof afterRefreshHook === 'function') {
|
||||
await afterRefreshHook(options, refreshedToken);
|
||||
if (typeof afterRefresh === 'function') {
|
||||
await afterRefresh(options, refreshedToken);
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -8,6 +8,7 @@ const refreshHandler = async (req, res) => {
|
||||
config: req.collection,
|
||||
api: 'REST',
|
||||
authorization: req.headers.authorization,
|
||||
user: req.user,
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
|
||||
@@ -13,6 +13,7 @@ const updateHandler = async (req, res) => {
|
||||
api: 'REST',
|
||||
locale: req.locale,
|
||||
fallbackLocale: req.fallbackLocale,
|
||||
user: req.user,
|
||||
});
|
||||
|
||||
return res.status(httpStatus.OK).json({
|
||||
|
||||
@@ -8,12 +8,12 @@ const { email, password } = require('../tests/credentials');
|
||||
|
||||
const config = require('../../demo/payload.config');
|
||||
|
||||
const url = config.serverURL;
|
||||
const usernameField = config.user.auth.useAsUsername;
|
||||
|
||||
let token = null;
|
||||
|
||||
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 () => {
|
||||
const response = await fetch(`${url}/api/first-register`, {
|
||||
body: JSON.stringify({
|
||||
@@ -76,7 +76,7 @@ describe('Users REST API', () => {
|
||||
expect(response.status).toBe(200);
|
||||
expect(data.token).not.toBeNull();
|
||||
|
||||
({ token } = data);
|
||||
// token = data.refreshedToken;
|
||||
});
|
||||
|
||||
it('should allow a user to be created', async () => {
|
||||
|
||||
Reference in New Issue
Block a user