Speech as a package #416

Merged
bones7242 merged 89 commits from speech-as-a-package into master 2018-04-18 21:47:34 +02:00
7 changed files with 72 additions and 54 deletions
Showing only changes of commit 86ede21445 - Show all commits

View file

@ -1,54 +0,0 @@
const logger = require('winston');
const passport = require('passport');
module.exports = (app) => {
// route for sign up
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,
});
});
// route for log in
app.post('/login', (req, res, next) => {
passport.authenticate('local-login', (err, user, info) => {
if (err) {
return next(err);
}
if (!user) {
return res.status(400).json({
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);
});
// route to log out
app.get('/logout', (req, res) => {
req.logout();
res.status(200).json({success: true, message: 'you successfully logged out'});
});
// see if user is authenticated, and return credentials if so
app.get('/user', (req, res) => {
if (req.user) {
res.status(200).json({success: true, data: req.user});
} else {
res.status(401).json({success: false, message: 'user is not logged in'});
}
});
};

View file

@ -0,0 +1,11 @@
const signup = require('./signup');
const login = require('./login');
const logout = require('./logout');
const user = require('./user');
module.exports = {
signup,
login,
logout,
user,
};

View file

@ -0,0 +1,28 @@
const login = (passport) => {
return (req, res, next) => {
passport.authenticate('local-login', (err, user, info) => {
if (err) {
return next(err);
}
if (!user) {
return res.status(400).json({
success: false,
message: info.message,
});
}
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);
};
};
module.exports = login;

View file

@ -0,0 +1,8 @@
const logout = () => {
return (req, res) => {
req.logout();
res.status(200).json({success: true, message: 'you successfully logged out'});
};
};
module.exports = logout;

View file

@ -0,0 +1,12 @@
const signup = () => {
return (req, res) => {
res.status(200).json({
success : true,
channelName : req.user.channelName,
channelClaimId: req.user.channelClaimId,
shortChannelId: req.user.shortChannelId,
});
};
};
module.exports = signup;

View file

@ -0,0 +1,11 @@
const user = () => {
return (req, res) => {
if (req.user) {
res.status(200).json({success: true, data: req.user});
} else {
res.status(401).json({success: false, message: 'user is not logged in'});
}
};
};
module.exports = user;

View file

@ -1,5 +1,6 @@
const api = require('./server/routes/api/');
const asset = require('./server/routes/asset/');
const auth = require('./server/routes/auth/');
const logger = require('./config/loggerConfig.js');
const mysql = require('./config/mysqlConfig');
const site = require('./config/siteConfig');
@ -18,6 +19,7 @@ const exports = {
routes: {
api,
asset,
auth,
},
// Components,
};