diff --git a/src/auth/graphql/resolvers/me.js b/src/auth/graphql/resolvers/me.js index 729d738346..74a4380ecb 100644 --- a/src/auth/graphql/resolvers/me.js +++ b/src/auth/graphql/resolvers/me.js @@ -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; diff --git a/src/auth/operations/index.js b/src/auth/operations/index.js index 418e341503..7d7fafd05b 100644 --- a/src/auth/operations/index.js +++ b/src/auth/operations/index.js @@ -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, }; diff --git a/src/auth/operations/me.js b/src/auth/operations/me.js new file mode 100644 index 0000000000..7c816da118 --- /dev/null +++ b/src/auth/operations/me.js @@ -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; diff --git a/src/auth/requestHandlers/forgotPassword.js b/src/auth/requestHandlers/forgotPassword.js index 96eda790fb..742c7f6140 100644 --- a/src/auth/requestHandlers/forgotPassword.js +++ b/src/auth/requestHandlers/forgotPassword.js @@ -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); } }; diff --git a/src/auth/requestHandlers/init.js b/src/auth/requestHandlers/init.js index 27e86d50ad..a7891ffb0e 100644 --- a/src/auth/requestHandlers/init.js +++ b/src/auth/requestHandlers/init.js @@ -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); } }; diff --git a/src/auth/requestHandlers/login.js b/src/auth/requestHandlers/login.js index 8d8bb8ba27..d72dfa305e 100644 --- a/src/auth/requestHandlers/login.js +++ b/src/auth/requestHandlers/login.js @@ -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); } }; diff --git a/src/auth/requestHandlers/me.js b/src/auth/requestHandlers/me.js index 223ac84da2..b3847a31d0 100644 --- a/src/auth/requestHandlers/me.js +++ b/src/auth/requestHandlers/me.js @@ -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; diff --git a/src/auth/requestHandlers/policies.js b/src/auth/requestHandlers/policies.js index 783f62fda4..22dbdd38a5 100644 --- a/src/auth/requestHandlers/policies.js +++ b/src/auth/requestHandlers/policies.js @@ -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); } }; diff --git a/src/auth/requestHandlers/refresh.js b/src/auth/requestHandlers/refresh.js index 03291e8ecd..ec3558b3d0 100644 --- a/src/auth/requestHandlers/refresh.js +++ b/src/auth/requestHandlers/refresh.js @@ -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); } }; diff --git a/src/auth/requestHandlers/register.js b/src/auth/requestHandlers/register.js index 03342c2be2..bf7e15fa1c 100644 --- a/src/auth/requestHandlers/register.js +++ b/src/auth/requestHandlers/register.js @@ -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); } }; diff --git a/src/auth/requestHandlers/registerFirstUser.js b/src/auth/requestHandlers/registerFirstUser.js index a53c214537..7b89179d79 100644 --- a/src/auth/requestHandlers/registerFirstUser.js +++ b/src/auth/requestHandlers/registerFirstUser.js @@ -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); } }; diff --git a/src/auth/requestHandlers/resetPassword.js b/src/auth/requestHandlers/resetPassword.js index 5406f3a334..73665abca7 100644 --- a/src/auth/requestHandlers/resetPassword.js +++ b/src/auth/requestHandlers/resetPassword.js @@ -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); } }; diff --git a/src/auth/requestHandlers/update.js b/src/auth/requestHandlers/update.js index 2447ee0e00..2bdd1e7023 100644 --- a/src/auth/requestHandlers/update.js +++ b/src/auth/requestHandlers/update.js @@ -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); } }; diff --git a/src/auth/routes.js b/src/auth/routes.js index e051a8f25d..fc326968ab 100644 --- a/src/auth/routes.js +++ b/src/auth/routes.js @@ -46,7 +46,7 @@ const authRoutes = (collection, config, sendEmail) => { router .route(`/${slug}/me`) - .get(me); + .get(me(config)); router .route(`/${slug}/first-register`) diff --git a/src/collections/graphql/init.js b/src/collections/graphql/init.js index a77f9be512..1bf6872ece 100644 --- a/src/collections/graphql/init.js +++ b/src/collections/graphql/init.js @@ -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}`] = {