908bbfb937
* fix(server): fix 'speckle:error(s)' mismatching naming * fix(frontend): add mtl as documented file import format, fix missing computed prop * fix(fileimports): specklepy operations.send shouldn't use local cache for sending fixes #1017 * obj import skip using default cache
28 lines
888 B
JavaScript
28 lines
888 B
JavaScript
const passport = require('passport')
|
|
const debug = require('debug')
|
|
|
|
/**
|
|
* Wrapper for passport.authenticate that handles success & failure scenarios correctly
|
|
* (passport.authenticate() by default doesn't, so don't use it)
|
|
* @param {import('passport').Strategy | string} strategy
|
|
* @param {import('passport').AuthenticateOptions | undefined} [options]
|
|
* @returns {import('express').Handler}
|
|
*/
|
|
function passportAuthenticate(strategy, options = undefined) {
|
|
return (req, res, next) =>
|
|
passport.authenticate(strategy, options, (err, user, info) => {
|
|
if (err) debug('speckle:error')(err)
|
|
if (!user) {
|
|
const errMsg = info?.message || 'Failed to authenticate, contact server admins'
|
|
return res.redirect(`/error?message=${errMsg}`)
|
|
}
|
|
|
|
req.user = user
|
|
return next()
|
|
})(req, res, next)
|
|
}
|
|
|
|
module.exports = {
|
|
passportAuthenticate
|
|
}
|