Skip to content

Instantly share code, notes, and snippets.

@alexanmtz
Created January 13, 2019 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanmtz/b638f67488e1e4c01715746f24212f2f to your computer and use it in GitHub Desktop.
Save alexanmtz/b638f67488e1e4c01715746f24212f2f to your computer and use it in GitHub Desktop.
Routes to authenticate
const express = require('express')
const router = express.Router()
const passport = require('passport')
const authenticationHelpers = require('../../authenticationHelpers')
require('../../../loading/loading')
const secure = require('./secure')
router.get('/authenticated', authenticationHelpers.isAuth)
router.get('/authorize/github', passport.authenticate('github', { scope: ['user:email'], accessType: 'offline' }))
router.get('/callback/github',
passport.authenticate('github', { failureRedirect: '/' }),
(req, res) => {
res.redirect(`${process.env.FRONTEND_HOST}/#/token/${req.user.token}`)
})
})
router.post('/authorize/local', (req, res, next) => {
passport.authenticate('local', (err, user, info) => {
if (err) {
res.status(401)
res.send({ 'reason': 'Invalid credentials' })
}
if (!user) {
// res.status(401)
// res.send({ 'reason': 'Invalid credentials' })
res.redirect(`${process.env.FRONTEND_HOST}/#/login/invalid`)
}
else {
req.logIn(user, { session: false }, (err) => {
if (err) {
res.status(500)
res.send({ 'error': 'Server error' })
}
// set authorization header for tests
res.set('Authorization', 'Bearer ' + user.token)
res.redirect(`${process.env.FRONTEND_HOST}/#/token/${req.user.token}`)
})
}
})(req, res, next)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment