2017-09-20 23:39:20 +02:00
|
|
|
const logger = require('winston');
|
|
|
|
const passport = require('passport');
|
|
|
|
|
|
|
|
module.exports = (app) => {
|
|
|
|
// route for sign up
|
2018-03-16 01:19:16 +01:00
|
|
|
app.post('/signup', passport.authenticate('local-signup'), (req, res) => {
|
|
|
|
logger.verbose(`successful signup for ${req.user.channelName}`);
|
|
|
|
res.status(200).json({
|
|
|
|
success : true,
|
|
|
|
channelName : req.user.channelName,
|
|
|
|
channelClaimId: req.user.channelClaimId,
|
|
|
|
shortChannelId: req.user.shortChannelId,
|
|
|
|
});
|
2017-09-20 23:39:20 +02:00
|
|
|
});
|
|
|
|
// route for log in
|
2018-01-25 03:08:21 +01:00
|
|
|
app.post('/login', (req, res, next) => {
|
|
|
|
passport.authenticate('local-login', (err, user, info) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
if (!user) {
|
2018-02-13 21:19:57 +01:00
|
|
|
return res.status(400).json({
|
2018-01-25 03:08:21 +01:00
|
|
|
success: false,
|
|
|
|
message: info.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
logger.debug('successful login');
|
|
|
|
req.logIn(user, (err) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
return res.status(200).json({
|
|
|
|
success : true,
|
|
|
|
channelName : req.user.channelName,
|
|
|
|
channelClaimId: req.user.channelClaimId,
|
|
|
|
shortChannelId: req.user.shortChannelId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})(req, res, next);
|
2017-09-20 23:39:20 +02:00
|
|
|
});
|
2018-02-03 00:14:57 +01:00
|
|
|
// route to log out
|
|
|
|
app.get('/logout', (req, res) => {
|
|
|
|
req.logout();
|
|
|
|
res.status(200).json({success: true, message: 'you successfully logged out'});
|
|
|
|
});
|
2018-01-24 04:36:09 +01:00
|
|
|
// see if user is authenticated, and return credentials if so
|
|
|
|
app.get('/user', (req, res) => {
|
|
|
|
if (req.user) {
|
2018-02-03 00:14:57 +01:00
|
|
|
res.status(200).json({success: true, data: req.user});
|
2018-01-24 04:36:09 +01:00
|
|
|
} else {
|
2018-02-13 21:19:57 +01:00
|
|
|
res.status(401).json({success: false, message: 'user is not logged in'});
|
2018-01-24 04:36:09 +01:00
|
|
|
}
|
|
|
|
});
|
2017-09-20 23:39:20 +02:00
|
|
|
};
|