From fefb3535dc3a05fb390ea6f7466fdb53a47fac6d Mon Sep 17 00:00:00 2001 From: Dimitrie Stefanescu Date: Mon, 3 May 2021 11:04:28 +0100 Subject: [PATCH] fix(server): invites: updates local strategy to use invite during pre-finalisation cb --- packages/server/modules/auth/strategies/local.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/server/modules/auth/strategies/local.js b/packages/server/modules/auth/strategies/local.js index 21be7bf6f..50a0ade2c 100644 --- a/packages/server/modules/auth/strategies/local.js +++ b/packages/server/modules/auth/strategies/local.js @@ -5,7 +5,7 @@ const appRoot = require( 'app-root-path' ) const debug = require( 'debug' ) const { createUser, updateUser, findOrCreateUser, validatePasssword, getUserByEmail } = require( `${appRoot}/modules/core/services/users` ) const { getServerInfo } = require( `${appRoot}/modules/core/services/generic` ) -const { validateInvite } = require( `${appRoot}/modules/serverinvites/services` ) +const { validateInvite, useInvite } = require( `${appRoot}/modules/serverinvites/services` ) module.exports = async ( app, session, sessionAppId, finalizeAuth ) => { const strategy = { @@ -16,8 +16,6 @@ module.exports = async ( app, session, sessionAppId, finalizeAuth ) => { url: '/auth/local' } - const serverInfo = await getServerInfo() - app.post( '/auth/local/login', session, sessionAppId, async ( req, res, next ) => { try { let valid = await validatePasssword( { email: req.body.email, password: req.body.password } ) @@ -40,6 +38,7 @@ module.exports = async ( app, session, sessionAppId, finalizeAuth ) => { }, finalizeAuth ) app.post( '/auth/local/register', session, sessionAppId, async ( req, res, next ) => { + const serverInfo = await getServerInfo() try { if ( !req.body.password ) @@ -47,15 +46,16 @@ module.exports = async ( app, session, sessionAppId, finalizeAuth ) => { let user = req.body - if ( serverInfo.inviteOnly && !req.session.inviteId ){ + if ( serverInfo.inviteOnly && !req.session.inviteId ) { throw new Error( 'This server is invite only. Please provide an invite id.' ) - } if ( req.session.inviteId ) { - const valid = await validateInvite( { id:req.session.inviteId, email: user.email } ) + const valid = await validateInvite( { id: req.session.inviteId, email: user.email } ) if ( !valid ) throw new Error( 'Invite email mismatch. Please use the original email the invite was sent to register.' ) + + await useInvite( { id: req.session.inviteId, email: user.email } ) } let userId = await createUser( user )