modifies all auth request handlers to rely on express error handling, updates /me req handler to operation format

This commit is contained in:
James
2020-07-07 14:44:37 -04:00
parent 94bbee7f74
commit 1f832a373a
15 changed files with 64 additions and 58 deletions

View File

@@ -1,6 +1,7 @@
/* eslint-disable no-param-reassign */
const meResolver = async (_, args, context) => {
return context.user;
const { me } = require('../../operations');
const meResolver = config => async (_, __, context) => {
return me({ req: context, config });
};
module.exports = meResolver;

View File

@@ -7,6 +7,7 @@ const resetPassword = require('./resetPassword');
const registerFirstUser = require('./registerFirstUser');
const update = require('./update');
const policies = require('./policies');
const me = require('./me');
module.exports = {
login,
@@ -18,4 +19,5 @@ module.exports = {
resetPassword,
registerFirstUser,
policies,
me,
};

29
src/auth/operations/me.js Normal file
View File

@@ -0,0 +1,29 @@
const jwt = require('jsonwebtoken');
const getExtractJWT = require('../getExtractJWT');
const me = async ({ req, config }) => {
try {
const extractJWT = getExtractJWT(config);
if (req.user) {
const response = req.user;
const token = extractJWT(req);
if (token) {
const decoded = jwt.decode(token);
if (decoded) {
response.exp = decoded.exp;
}
}
return response;
}
return null;
} catch (error) {
throw error;
}
};
module.exports = me;

View File

@@ -1,8 +1,7 @@
const httpStatus = require('http-status');
const formatErrorResponse = require('../../express/responses/formatError');
const { forgotPassword } = require('../operations');
const forgotPasswordHandler = (config, email) => async (req, res) => {
const forgotPasswordHandler = (config, email) => async (req, res, next) => {
try {
await forgotPassword({
req,
@@ -17,7 +16,7 @@ const forgotPasswordHandler = (config, email) => async (req, res) => {
message: 'Success',
});
} catch (error) {
return res.status(error.status || httpStatus.INTERNAL_SERVER_ERROR).json(formatErrorResponse(error));
return next(error);
}
};

View File

@@ -1,13 +1,11 @@
const httpStatus = require('http-status');
const { init } = require('../operations');
const formatError = require('../../express/responses/formatError');
const initHandler = async (req, res) => {
const initHandler = async (req, res, next) => {
try {
const initialized = await init({ Model: req.collection.Model });
return res.status(200).json({ initialized });
} catch (error) {
return res.status(error.status || httpStatus.INTERNAL_SERVER_ERROR).json(formatError(error));
return next(error);
}
};

View File

@@ -1,8 +1,7 @@
const httpStatus = require('http-status');
const formatErrorResponse = require('../../express/responses/formatError');
const { login } = require('../operations');
const loginHandler = config => async (req, res) => {
const loginHandler = config => async (req, res, next) => {
try {
const token = await login({
req,
@@ -18,7 +17,7 @@ const loginHandler = config => async (req, res) => {
token,
});
} catch (error) {
return res.status(error.status || httpStatus.INTERNAL_SERVER_ERROR).json(formatErrorResponse(error));
return next(error);
}
};

View File

@@ -1,29 +1,12 @@
const jwt = require('jsonwebtoken');
const { me } = require('../operations');
const meHandler = async (req, res, next) => {
const meHandler = config => async (req, res, next) => {
try {
if (req.user) {
const response = req.user;
if (req.headers.authorization && req.headers.authorization.indexOf('JWT') === 0) {
const token = req.headers.authorization.replace('JWT ', '');
if (token) {
const decoded = jwt.decode(token);
if (decoded.exp) {
response.exp = decoded.exp;
}
}
}
return res.status(200).json(response);
}
return res.status(200).json(null);
const response = await me({ req, config });
return res.status(200).json(response);
} catch (err) {
next(err);
return next(err);
}
return next();
};
module.exports = meHandler;

View File

@@ -1,8 +1,7 @@
const httpStatus = require('http-status');
const formatErrorResponse = require('../../express/responses/formatError');
const { policies } = require('../operations');
const policiesHandler = config => async (req, res) => {
const policiesHandler = config => async (req, res, next) => {
try {
const policyResults = await policies({
req,
@@ -12,7 +11,7 @@ const policiesHandler = config => async (req, res) => {
return res.status(httpStatus.OK)
.json(policyResults);
} catch (error) {
return res.status(error.status || httpStatus.INTERNAL_SERVER_ERROR).json(formatErrorResponse(error));
return next(error);
}
};

View File

@@ -1,9 +1,7 @@
const httpStatus = require('http-status');
const formatErrorResponse = require('../../express/responses/formatError');
const { refresh } = require('../operations');
const getExtractJWT = require('../getExtractJWT');
const refreshHandler = config => async (req, res) => {
const refreshHandler = config => async (req, res, next) => {
try {
const extractJWT = getExtractJWT(config);
const token = extractJWT(req);
@@ -21,7 +19,7 @@ const refreshHandler = config => async (req, res) => {
...result,
});
} catch (error) {
return res.status(error.status || httpStatus.INTERNAL_SERVER_ERROR).json(formatErrorResponse(error));
return next(error);
}
};

View File

@@ -1,9 +1,8 @@
const httpStatus = require('http-status');
const formatErrorResponse = require('../../express/responses/formatError');
const formatSuccessResponse = require('../../express/responses/formatSuccess');
const { register } = require('../operations');
const registerHandler = config => async (req, res) => {
const registerHandler = config => async (req, res, next) => {
try {
const user = await register({
config,
@@ -17,7 +16,7 @@ const registerHandler = config => async (req, res) => {
doc: user,
});
} catch (error) {
return res.status(error.status || httpStatus.UNAUTHORIZED).json(formatErrorResponse(error));
return next(error);
}
};

View File

@@ -1,8 +1,6 @@
const httpStatus = require('http-status');
const formatErrorResponse = require('../../express/responses/formatError');
const { registerFirstUser } = require('../operations');
const registerFirstUserHandler = config => async (req, res) => {
const registerFirstUserHandler = config => async (req, res, next) => {
try {
const firstUser = await registerFirstUser({
req,
@@ -13,7 +11,7 @@ const registerFirstUserHandler = config => async (req, res) => {
return res.status(201).json(firstUser);
} catch (error) {
return res.status(error.status || httpStatus.INTERNAL_SERVER_ERROR).json(formatErrorResponse(error));
return next(error);
}
};

View File

@@ -1,8 +1,7 @@
const httpStatus = require('http-status');
const formatErrorResponse = require('../../express/responses/formatError');
const { resetPassword } = require('../operations');
const resetPasswordHandler = config => async (req, res) => {
const resetPasswordHandler = config => async (req, res, next) => {
try {
const token = await resetPassword({
req,
@@ -17,8 +16,7 @@ const resetPasswordHandler = config => async (req, res) => {
token,
});
} catch (error) {
return res.status(error.status || httpStatus.INTERNAL_SERVER_ERROR)
.json(formatErrorResponse(error));
return next(error);
}
};

View File

@@ -1,9 +1,8 @@
const httpStatus = require('http-status');
const formatErrorResponse = require('../../express/responses/formatError');
const formatSuccessResponse = require('../../express/responses/formatSuccess');
const { update } = require('../operations');
const updateHandler = async (req, res) => {
const updateHandler = async (req, res, next) => {
try {
const user = await update({
req,
@@ -18,7 +17,7 @@ const updateHandler = async (req, res) => {
doc: user,
});
} catch (error) {
return res.status(httpStatus.UNAUTHORIZED).json(formatErrorResponse(error));
return next(error);
}
};

View File

@@ -46,7 +46,7 @@ const authRoutes = (collection, config, sendEmail) => {
router
.route(`/${slug}/me`)
.get(me);
.get(me(config));
router
.route(`/${slug}/first-register`)

View File

@@ -141,12 +141,16 @@ function registerCollections() {
type: 'text',
required: true,
},
{
name: 'exp',
type: 'number',
},
]),
);
this.Query.fields[`me${singularLabel}`] = {
type: collection.graphQL.jwt,
resolve: me,
resolve: me(this.config),
};
this.Query.fields[`initialized${singularLabel}`] = {