|
@@ -7,50 +7,61 @@ var r = require('../db');
|
|
|
|
|
|
var bcrypt = require('bcryptjs');
|
|
var bcrypt = require('bcryptjs');
|
|
|
|
|
|
-// GitHub
|
|
|
|
|
|
+//GitHub authentication routes
|
|
|
|
+//GitHub authentication callback route
|
|
authRouter.use('/login/callback/github', auth.authenticate('github'), function (req, res) {
|
|
authRouter.use('/login/callback/github', auth.authenticate('github'), function (req, res) {
|
|
res.redirect('/');
|
|
res.redirect('/');
|
|
});
|
|
});
|
|
-authRouter.get('/login/github', auth.authenticate('github', { scope: [ 'user:email' ] }));
|
|
|
|
|
|
+//GitHub authentication route
|
|
|
|
+authRouter.get('/login/github', auth.authenticate('github'));
|
|
|
|
|
|
-// Local
|
|
|
|
|
|
+//Local authentication routes
|
|
|
|
+//Local login route
|
|
authRouter.get('/login', auth.authenticate('local', {successRedirect: '/auth/user', failureRedirect: '/login'}), function(req, res) {
|
|
authRouter.get('/login', auth.authenticate('local', {successRedirect: '/auth/user', failureRedirect: '/login'}), function(req, res) {
|
|
// If this function gets called, authentication was successful.
|
|
// If this function gets called, authentication was successful.
|
|
// `req.user` contains the authenticated user.
|
|
// `req.user` contains the authenticated user.
|
|
res.redirect("/auth/user");
|
|
res.redirect("/auth/user");
|
|
});
|
|
});
|
|
|
|
|
|
-// Local
|
|
|
|
|
|
+//Local register route
|
|
authRouter.get('/register', function(req, res) {
|
|
authRouter.get('/register', function(req, res) {
|
|
|
|
+ //Checks if the email, username and password are valid
|
|
req.checkQuery('email', 'Invalid email').isEmail();
|
|
req.checkQuery('email', 'Invalid email').isEmail();
|
|
req.checkQuery('username', 'Invalid getparam').notEmpty();
|
|
req.checkQuery('username', 'Invalid getparam').notEmpty();
|
|
req.checkQuery('password', 'Invalid getparam').notEmpty();
|
|
req.checkQuery('password', 'Invalid getparam').notEmpty();
|
|
|
|
+
|
|
var query = req.query;
|
|
var query = req.query;
|
|
|
|
|
|
|
|
+ //Check to see if there are any errors, and throw them if so
|
|
var errors = req.validationErrors();
|
|
var errors = req.validationErrors();
|
|
if (errors) {
|
|
if (errors) {
|
|
res.send('There have been validation errors: ', 400);
|
|
res.send('There have been validation errors: ', 400);
|
|
return;
|
|
return;
|
|
} else {
|
|
} else {
|
|
//TODO Check if username/email already exists
|
|
//TODO Check if username/email already exists
|
|
|
|
+ //Check to see if a user with that username already exists
|
|
r.table("users").getAll(query.username.toLowerCase(), {index: "usernameL"}).isEmpty().run(r.conn, function(err, result) {
|
|
r.table("users").getAll(query.username.toLowerCase(), {index: "usernameL"}).isEmpty().run(r.conn, function(err, result) {
|
|
if (err) throw err;
|
|
if (err) throw err;
|
|
if (result) {
|
|
if (result) {
|
|
|
|
+ //Check to see if a user with that email already exists
|
|
r.table("users").getAll(query.email.toLowerCase(), {index: "email"}).isEmpty().run(r.conn, function(err, result) {
|
|
r.table("users").getAll(query.email.toLowerCase(), {index: "email"}).isEmpty().run(r.conn, function(err, result) {
|
|
if (err) throw err;
|
|
if (err) throw err;
|
|
if (result) {
|
|
if (result) {
|
|
//TODO Hash password
|
|
//TODO Hash password
|
|
var hash;
|
|
var hash;
|
|
|
|
+ //Generating a salt
|
|
bcrypt.genSalt(10, function (err, salt) {
|
|
bcrypt.genSalt(10, function (err, salt) {
|
|
if (err) {
|
|
if (err) {
|
|
//TODO Throw error
|
|
//TODO Throw error
|
|
} else {
|
|
} else {
|
|
|
|
+ //Hashing the password with the salt
|
|
bcrypt.hash(query.password, salt, function (err, hash) {
|
|
bcrypt.hash(query.password, salt, function (err, hash) {
|
|
if (err) {
|
|
if (err) {
|
|
//TODO Throw error
|
|
//TODO Throw error
|
|
} else {
|
|
} else {
|
|
var email = query.email.toLowerCase();
|
|
var email = query.email.toLowerCase();
|
|
var usernameL = query.username.toLowerCase();
|
|
var usernameL = query.username.toLowerCase();
|
|
|
|
+ //Inserting the user object into the database
|
|
r.table('users')
|
|
r.table('users')
|
|
.insert({
|
|
.insert({
|
|
username: query.username,
|
|
username: query.username,
|
|
@@ -63,10 +74,12 @@ authRouter.get('/register', function(req, res) {
|
|
.then(function (response) {
|
|
.then(function (response) {
|
|
|
|
|
|
return r.table('users')
|
|
return r.table('users')
|
|
|
|
+ //Getting the newly created user
|
|
.get(response.generated_keys[0])
|
|
.get(response.generated_keys[0])
|
|
.run(r.conn);
|
|
.run(r.conn);
|
|
})
|
|
})
|
|
.then(function (newUser) {
|
|
.then(function (newUser) {
|
|
|
|
+ //Logging in
|
|
//TODO Log in
|
|
//TODO Log in
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -84,8 +97,9 @@ authRouter.get('/register', function(req, res) {
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
-// All
|
|
|
|
|
|
+//Route to get user info
|
|
authRouter.use('/user', authControllers.getUser);
|
|
authRouter.use('/user', authControllers.getUser);
|
|
|
|
+//Route to logout
|
|
authRouter.use('/logout', authControllers.logout);
|
|
authRouter.use('/logout', authControllers.logout);
|
|
|
|
|
|
module.exports = authRouter;
|
|
module.exports = authRouter;
|