Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Fueled/Rover
Browse files Browse the repository at this point in the history
* 'master' of github.com:Fueled/Rover:
  chore(*): Formatting the js per eslint
  feat(ROV-3): Empty state with instructions for My Projects
  feat(ROV-19): Added events sidebar to filter those events by function
  feat(Events): Format events to readable params
  chore(Rollbar): Configured Rollbar for Error Handling
  chore(Admin): Forest Admin Panel configured
  chore(*): Updated packages
  • Loading branch information
ravidsrk committed Nov 23, 2018
2 parents c93749f + 37956d2 commit 751aed7
Show file tree
Hide file tree
Showing 19 changed files with 2,541 additions and 1,449 deletions.
23 changes: 23 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ const mongoose = require('mongoose');
const passport = require('passport');
const expressValidator = require('express-validator');
const sass = require('node-sass-middleware');
const Rollbar = require("rollbar");
const _ = require("lodash");

/**
* Load environment variables from .env file, where API keys and passwords are configured.
*/
dotenv.load({ path: '.env' });

const rollbar = new Rollbar({
accessToken: process.env.ROLLBAR_ACCESS_TOKEN,
captureUncaught: true,
captureUnhandledRejections: true
});

/**
* Controllers (route handlers).
*/
Expand Down Expand Up @@ -79,6 +87,12 @@ app.use(session({
autoReconnect: true,
})
}));
app.use(require('forest-express-mongoose').init({
modelsDir: __dirname + '/models',
envSecret: process.env.FOREST_ENV_SECRET,
authSecret: process.env.FOREST_AUTH_SECRET,
mongoose: require('mongoose')
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
Expand Down Expand Up @@ -106,10 +120,19 @@ app.use((req, res, next) => {
}
next();
});
app.use(rollbar.errorHandler());

app.locals.moment = require('moment');
app.locals.web3 = require('web3');
app.locals.jsonminify = require('jsonminify');
app.locals.contractEvents = function(contractAbi, event) {
let eventDefinitions = {};
let abi = _.find(JSON.parse(contractAbi), function(a) { return a.name === event.event})
abi.inputs.forEach(({ name }) => {
eventDefinitions[name] = event.returnValues[name]
});
return eventDefinitions;
}

app.use('/', express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));
app.use('/js/lib', express.static(path.join(__dirname, 'node_modules/popper.js/dist/umd'), { maxAge: 31557600000 }));
Expand Down
172 changes: 92 additions & 80 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,127 @@
const passport = require('passport');
const { Strategy: LocalStrategy } = require('passport-local');
const { OAuth2Strategy: GoogleStrategy } = require('passport-google-oauth');
const {Strategy: LocalStrategy} = require('passport-local');
const {OAuth2Strategy: GoogleStrategy} = require('passport-google-oauth');

const User = require('../models/User');

passport.serializeUser((user, done) => {
done(null, user.id);
done(null, user.id);
});

passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
User.findById(id, (err, user) => {
done(err, user);
});
});

/**
* Sign in using Email and Password.
*/
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
User.findOne({ email: email.toLowerCase() }, (err, user) => {
if (err) { return done(err); }
if (!user) {
return done(null, false, { msg: `Email ${email} not found.` });
}
user.comparePassword(password, (err, isMatch) => {
if (err) { return done(err); }
if (isMatch) {
return done(null, user);
}
return done(null, false, { msg: 'Invalid email or password.' });
passport.use(new LocalStrategy({usernameField: 'email'}, (email, password, done) => {
User.findOne({email: email.toLowerCase()}, (err, user) => {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, {msg: `Email ${email} not found.`});
}
user.comparePassword(password, (err, isMatch) => {
if (err) {
return done(err);
}
if (isMatch) {
return done(null, user);
}
return done(null, false, {msg: 'Invalid email or password.'});
});
});
});
}));

/**
* Sign in with Google.
*/
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: '/auth/google/callback',
passReqToCallback: true
clientID: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: '/auth/google/callback',
passReqToCallback: true
}, (req, accessToken, refreshToken, profile, done) => {
if (req.user) {
User.findOne({ google: profile.id }, (err, existingUser) => {
if (err) { return done(err); }
if (existingUser) {
req.flash('errors', { msg: 'There is already a Google account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
done(err);
} else {
User.findById(req.user.id, (err, user) => {
if (err) { return done(err); }
user.google = profile.id;
user.tokens.push({ kind: 'google', accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || profile._json.image.url;
user.save((err) => {
req.flash('info', { msg: 'Google account has been linked.' });
done(err, user);
});
if (req.user) {
User.findOne({google: profile.id}, (err, existingUser) => {
if (err) {
return done(err);
}
if (existingUser) {
req.flash('errors', {msg: 'There is already a Google account that belongs to you. Sign in with that account or delete it, then link it with your current account.'});
done(err);
} else {
User.findById(req.user.id, (err, user) => {
if (err) {
return done(err);
}
user.google = profile.id;
user.tokens.push({kind: 'google', accessToken});
user.profile.name = user.profile.name || profile.displayName;
user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || profile._json.image.url;
user.save((err) => {
req.flash('info', {msg: 'Google account has been linked.'});
done(err, user);
});
});
}
});
}
});
} else {
User.findOne({ google: profile.id }, (err, existingUser) => {
if (err) { return done(err); }
if (existingUser) {
return done(null, existingUser);
}
User.findOne({ email: profile.emails[0].value }, (err, existingEmailUser) => {
if (err) { return done(err); }
if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with Google manually from Account Settings.' });
done(err);
} else {
const user = new User();
user.email = profile.emails[0].value;
user.google = profile.id;
user.tokens.push({ kind: 'google', accessToken });
user.profile.name = profile.displayName;
user.profile.gender = profile._json.gender;
user.profile.picture = profile._json.image.url;
user.save((err) => {
done(err, user);
});
}
});
});
}
} else {
User.findOne({google: profile.id}, (err, existingUser) => {
if (err) {
return done(err);
}
if (existingUser) {
return done(null, existingUser);
}
User.findOne({email: profile.emails[0].value}, (err, existingEmailUser) => {
if (err) {
return done(err);
}
if (existingEmailUser) {
req.flash('errors', {msg: 'There is already an account using this email address. Sign in to that account and link it with Google manually from Account Settings.'});
done(err);
} else {
const user = new User();
user.email = profile.emails[0].value;
user.google = profile.id;
user.tokens.push({kind: 'google', accessToken});
user.profile.name = profile.displayName;
user.profile.gender = profile._json.gender;
user.profile.picture = profile._json.image.url;
user.save((err) => {
done(err, user);
});
}
});
});
}
}));

/**
* Login Required middleware.
*/
exports.isAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
};

/**
* Authorization Required middleware.
*/
exports.isAuthorized = (req, res, next) => {
const provider = req.path.split('/').slice(-1)[0];
const token = req.user.tokens.find(token => token.kind === provider);
if (token) {
next();
} else {
res.redirect(`/auth/${provider}`);
}
const provider = req.path.split('/').slice(-1)[0];
const token = req.user.tokens.find(token => token.kind === provider);
if (token) {
next();
} else {
res.redirect(`/auth/${provider}`);
}
};
Loading

0 comments on commit 751aed7

Please sign in to comment.