Created
January 13, 2019 20:50
-
-
Save alexanmtz/b638f67488e1e4c01715746f24212f2f to your computer and use it in GitHub Desktop.
Routes to authenticate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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