Skip to content

Commit

Permalink
Merge pull request #24 from hnginternship5/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
IDTitanium committed May 17, 2019
2 parents 844d75d + 72bc46f commit 1046262
Show file tree
Hide file tree
Showing 34 changed files with 825 additions and 733 deletions.
36 changes: 20 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
var mongoose = require("mongoose");
var createError = require("http-errors");
var express = require("express");
var exphbs = require("express-handlebars");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var session = require("express-session");
var flash = require("connect-flash");
var MongoStore = require("connect-mongo")(session);
require("dotenv").config();
require("./schedule");
const passport = require("passport");
const fx = require("money");
var mongoose = require('mongoose');
var createError = require('http-errors');
var express = require('express');
var exphbs = require('express-handlebars');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require('express-session');
var flash = require('connect-flash');
var MongoStore = require('connect-mongo')(session);
require('dotenv').config();
require('./schedule');
const passport = require('passport');
const fx = require('money');
var bodyParser = require('body-parser')


// The database setup
// we should probable store the url in .env for security reasons.
Expand All @@ -31,7 +33,9 @@ db.once("open", () => console.log("Connected to database"));
// checks if connection to db is a success
db.on("error", console.error.bind(console, "Database connection error:"));

var indexRouter = require("./routes/index");
var indexRouter = require('./routes/index');
var authRouter = require('./routes/auth');

// var usersRouter = require('./routes/users');

var app = express();
Expand Down Expand Up @@ -80,7 +84,7 @@ app.use(function(req, res, next) {
});

app.use("/", indexRouter);
// app.use("/auth.js", authRouter);
app.use("/auth", authRouter);
// app.use('/users', usersRouter);

// catch 404 and forward to error handler
Expand Down
32 changes: 18 additions & 14 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ module.exports = function(passport) {

// asynchronous
process.nextTick(function() {

// find the user in the database based on their facebook id
User.findOne({ 'facebook.id' : profile.id }, function(err, user) {
User.findOne({ 'account._id' : `${profile.id}` }, function(err, user) {

// if there is an error, stop everything and return that
// ie an error connecting to the database
Expand All @@ -64,18 +64,19 @@ module.exports = function(passport) {
let newUser = new User();

// set all of the facebook information in our user model
newUser.facebook.id = profile.id; // set the users facebook id
newUser.facebook.token = token; // we will save the token that facebook provides to the user
newUser.facebook.name = profile.name.givenName + ' ' + profile.name.familyName; // look at the passport user profile to see how names are returned
newUser.facebook.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first
newUser.account._id = profile.id; // set the users facebook id
newUser.account.token = token; // we will save the token that facebook provides to the user
newUser.account.name = profile.name.givenName + ' ' + profile.name.familyName; // look at the passport user profile to see how names are returned
newUser.account.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first
newUser.account.type = "facebook";

// save our user to the database
newUser.save(function(err) {
if (err)
throw err;

// if successful, return the new user
return done(null, newUser);
else
req.session.user = newUser.account;
res.redirect('/job-preference');
});
}

Expand All @@ -101,7 +102,7 @@ module.exports = function(passport) {
process.nextTick(function() {

// try to find the user based on their google id
User.findOne({ 'google.id' : profile.id}, function(err, user){
User.findOne({ 'account._id': `${profile.id}`}, function(err, user){
if (err)
return done(err);

Expand All @@ -113,11 +114,12 @@ module.exports = function(passport) {
var newUser =new User();

//set all of the relevant information
newUser.google = {
newUser.account = {
id:profile.id,
token:token,
name:profile.displayName,
email:profile.emails[0].value
email:profile.emails[0].value,
type: "google"
}
// newUser.google.id = profile.id;
// newUser.google.token = token;
Expand All @@ -128,8 +130,10 @@ module.exports = function(passport) {
//save the user
newUser.save(function(err){
if (err)
throw err;
return done(null, newUser);
throw err;
else
req.session.user = newUser.account;
res.redirect('/job-preference');
});
}
});
Expand Down
84 changes: 84 additions & 0 deletions controllers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Web application which authenticates to github
var http = require('http')
, url = require('url')
, qs = require('querystring')
, github = require('octonode')
, session = require('express-session');

let User = require('../models/login');

// Build the authorization config and url
var auth_url = github.auth.config({
id: process.env.GITHUB_CLIENTID,
secret: process.env.GITHUB_SECRET,
}).login(['user']);

// Store info to verify against CSRF
var state = auth_url.match(/&state=([0-9a-z]{32})/i);

// Handle github authentication
exports.authenticate = function (req, res) {
res.writeHead(302, { 'Content-Type': 'text/plain', 'Location': auth_url })
res.end('Redirecting to ' + auth_url);
};

// Handle callback
exports.callback = function (req, res) {
var sessData = req.session;
uri = url.parse(req.url);
var values = qs.parse(uri.query);
// Check against CSRF attacks
if (!state || state[1] != values.state) {

res.redirect('/login');
} else {
github.auth.login(values.code, function (err, token, headers) {

var client = github.client(token);

sessData.token = token;

client.get('/user', {}, function (err, status, body, headers) {

// find the user in the database based on their github account id
User.findOne
({
'account._id': `${body.id}`
}, function (err, user) {

// if there is an error, stop everything and return that
// ie an error connecting to the database
if (err)
res.redirect("/login");

// if the user is found, then log them in
if (user) {
req.session.user = user.account;
res.redirect("/jobs"); // user found, return that user
} else {
// if there is no user found with that github id, create them
let newUser = new User();

// set all of the github information in our user model
newUser.account._id = `${body.id}`;
newUser.account.token = token;
newUser.account.name = (body.name == null)? body.login : body.name;
newUser.account.email = body.email;
newUser.account.type = "github";

// save our user to the database
newUser.save(function (err) {
if (err)
throw err;
else
req.session.user = newUser.account;
res.redirect('/job-preference');
});
}

});

});
});
}
};
Loading

0 comments on commit 1046262

Please sign in to comment.