auth-router.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var express = require('express');
  2. var authControllers = require('./auth-controller');
  3. var auth = require('./index');
  4. var authRouter = express.Router();
  5. var r = require('../db');
  6. var bcrypt = require('bcryptjs');
  7. //GitHub authentication routes
  8. //GitHub authentication callback route
  9. authRouter.use('/login/callback/github', auth.authenticate('github'), function (req, res) {
  10. res.redirect('/');
  11. });
  12. //GitHub authentication route
  13. authRouter.get('/login/github', auth.authenticate('github'));
  14. //Local authentication routes
  15. //Local login route
  16. authRouter.get('/login', auth.authenticate('local', {successRedirect: '/auth/user', failureRedirect: '/login'}), function(req, res) {
  17. // If this function gets called, authentication was successful.
  18. // `req.user` contains the authenticated user.
  19. res.redirect("/auth/user");
  20. });
  21. //Local register route
  22. authRouter.get('/register', function(req, res) {
  23. //Checks if the email, username and password are valid
  24. req.checkQuery('email', 'Invalid email').isEmail();
  25. req.checkQuery('username', 'Invalid getparam').notEmpty();
  26. req.checkQuery('password', 'Invalid getparam').notEmpty();
  27. var query = req.query;
  28. //Check to see if there are any errors, and throw them if so
  29. var errors = req.validationErrors();
  30. if (errors) {
  31. res.send('There have been validation errors: ', 400);
  32. return;
  33. } else {
  34. //TODO Check if username/email already exists
  35. //Check to see if a user with that username already exists
  36. r.table("users").getAll(query.username.toLowerCase(), {index: "usernameL"}).isEmpty().run(r.conn, function(err, result) {
  37. if (err) throw err;
  38. if (result) {
  39. //Check to see if a user with that email already exists
  40. r.table("users").getAll(query.email.toLowerCase(), {index: "email"}).isEmpty().run(r.conn, function(err, result) {
  41. if (err) throw err;
  42. if (result) {
  43. //TODO Hash password
  44. var hash;
  45. //Generating a salt
  46. bcrypt.genSalt(10, function (err, salt) {
  47. if (err) {
  48. //TODO Throw error
  49. } else {
  50. //Hashing the password with the salt
  51. bcrypt.hash(query.password, salt, function (err, hash) {
  52. if (err) {
  53. //TODO Throw error
  54. } else {
  55. var email = query.email.toLowerCase();
  56. var usernameL = query.username.toLowerCase();
  57. //Inserting the user object into the database
  58. r.table('users')
  59. .insert({
  60. username: query.username,
  61. usernameL: usernameL,
  62. email: email,
  63. type: 'local',
  64. password: hash
  65. })
  66. .run(r.conn)
  67. .then(function (response) {
  68. return r.table('users')
  69. //Getting the newly created user
  70. .get(response.generated_keys[0])
  71. .run(r.conn);
  72. })
  73. .then(function (newUser) {
  74. //Logging in
  75. //TODO Log in
  76. });
  77. }
  78. });
  79. }
  80. });
  81. } else {
  82. //TODO Throw error
  83. }
  84. });
  85. } else {
  86. //TODO Throw error
  87. }
  88. });
  89. }
  90. });
  91. //Route to get user info
  92. authRouter.use('/user', authControllers.getUser);
  93. //Route to logout
  94. authRouter.use('/logout', authControllers.logout);
  95. module.exports = authRouter;