Files
speckle-server/packages/server/modules/auth/services/passportService.js
T
Kristaps Fabians Geikins da9224a069 feat: server & stream invites rework
feat: server & stream invites rework

Co-authored-by: Dimitrie Stefanescu <didimitrie@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-07-19 13:01:19 +03:00

28 lines
889 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:errors')(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
}