Files
talk/routes/api/v1/auth.js
T
2018-02-13 12:12:03 -07:00

48 lines
1.1 KiB
JavaScript

const express = require('express');
const {
passport,
HandleGenerateCredentials,
HandleLogout,
} = require('../../../services/passport');
const router = express.Router();
/**
* This returns the user if they are logged in.
*/
router.get('/', (req, res, next) => {
if (!req.user) {
res.status(204).end();
return;
}
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
// Send back the user object.
res.json({ user: req.user });
});
/**
* This blacklists the token used to authenticate.
*/
router.delete('/', HandleLogout);
//==============================================================================
// PASSPORT ROUTES
//==============================================================================
/**
* Local auth endpoint, will receive a email and password
*/
router.post('/local', (req, res, next) => {
// Perform the local authentication.
passport.authenticate(
'local',
{ session: false },
HandleGenerateCredentials(req, res, next)
)(req, res, next);
});
module.exports = router;