Speech as a package #416
7 changed files with 72 additions and 54 deletions
|
@ -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'});
|
||||
}
|
||||
});
|
||||
};
|
11
server/routes/auth/index.js
Normal file
11
server/routes/auth/index.js
Normal 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,
|
||||
};
|
28
server/routes/auth/login.js
Normal file
28
server/routes/auth/login.js
Normal 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;
|
8
server/routes/auth/logout.js
Normal file
8
server/routes/auth/logout.js
Normal 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;
|
12
server/routes/auth/signup.js
Normal file
12
server/routes/auth/signup.js
Normal 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;
|
11
server/routes/auth/user.js
Normal file
11
server/routes/auth/user.js
Normal 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;
|
|
@ -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,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue