Skip to content

Commit

Permalink
Merge pull request #46 from andela/develop
Browse files Browse the repository at this point in the history
Merge develop to master
  • Loading branch information
9jaswag committed Apr 1, 2019
2 parents cfb7be1 + 5e0a973 commit 73ee94b
Show file tree
Hide file tree
Showing 95 changed files with 17,073 additions and 1,016 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["transform-es2015-destructuring", "transform-object-rest-spread"],
"presets": [ "es2015" ]
}
9 changes: 9 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DB_HOST=
DB_USERNAME=
DB_PASSWORD=
DB_DATABASE=
PORT=
JWT_SECRET=
EMAIL_ADDRESS=
EMAIL_PASSWORD=
HOST_URL=
9 changes: 9 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/dist/**
/mochawesome-report/**
/node_modules/**
/.env/**
/coverage/**
.eslintrc.json
package.json
package-lock.json
doc.json
37 changes: 37 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"root": true,
"extends": "airbnb-base",
"env": {
"node": true,
"es6": true,
"mocha": true
},
"rules": {
"one-var": 0,
"one-var-declaration-per-line": 0,
"new-cap": 0,
"consistent-return": 0,
"no-param-reassign": 0,
"comma-dangle": 0,
"curly": ["error", "multi-line"],
"import/no-unresolved": [2, { "commonjs": true }],
"no-shadow": ["error", { "allow": ["req", "res", "err"] }],
"valid-jsdoc": ["error", {
"requireReturn": true,
"requireReturnType": true,
"requireParamDescription": false,
"requireReturnDescription": true
}],
"require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
}
}]
}
}
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,17 @@ node_modules
# Optional REPL history
.node_repl_history

# Report files for Mochawesome HTML report
mochawesome-report

# Ignore environmental variables
.env

# Ignore yarn generated files
yarn.lock
dist/

# Ignore vscode files
.vscode/
#coveralls code coverage reports
.nyc_output/
3 changes: 3 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eslint:
enabled: true
config_file: .eslintrc.json
9 changes: 9 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

const path = require('path');

module.exports = {
"config": path.resolve('./config', 'config.js'),
"models-path": path.resolve('./models'),
"seeders-path": path.resolve('./seeders'),
"migrations-path": path.resolve('./migrations')
};
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
node_js:
- "10"
services:
- postgresql
before_script:
- psql -c 'create database vidarapp;' -U postgres
script:
- npm install && npm install sequelize-cli -g
- npm run test
after_success:
- npm run coverage
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Authors Haven - A Social platform for the creative at heart.

[![Build Status](https://travis-ci.com/andela/vidar-ah-backend.svg?branch=develop)](https://travis-ci.com/andela/vidar-ah-backend) [![Coverage Status](https://coveralls.io/repos/github/andela/vidar-ah-backend/badge.svg?branch=develop)](https://coveralls.io/github/andela/vidar-ah-backend?branch=develop) [![](https://img.shields.io/badge/Protected_by-Hound-a873d1.svg)](https://houndci.com)
=======

## Vision
Expand Down
103 changes: 103 additions & 0 deletions auth/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import passport from 'passport';
import { OAuth2Strategy as GoogleStrategy } from 'passport-google-oauth';
import { Strategy as FacebookStrategy } from 'passport-facebook';
import { User } from '../models';

passport.use(new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: `${process.env.HOST_URL}/api/v1/auth/google/callback`,
},
/**
* callback function for google strategy
* @param {object} accessToken authorization token
* @param {object} refreshToken authorization token
* @param {object} profile a user profile
* @param {function} done end of function
* @returns {function} callback
*/
async (accessToken, refreshToken, profile, done) => {
const email = profile.emails[0].value;
/**
* @description - finds an existing user or create a new user
* @param {object} user a user
* @param {function} done end of function
* @returns {object} createOrFindUser
*/
const {
displayName,
name: { givenName }
} = profile;
const user = await User.findOrCreate({
where: { email },
defaults: {
name: displayName,
username: givenName,
email
}
});
return done(null, user[0].dataValues);
}
));

/**
* callback function for facebook strategy
* @param {object} accessToken authorization token
* @param {object} refreshToken authorization token
* @param {object} profile a user profile
* @param {function} done end of function
* @returns {function} callback
*/
passport.use(new FacebookStrategy(
{
clientID: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
callbackURL: `${process.env.HOST_URL}/api/v1/auth/facebook/callback`,
profileFields: ['id', 'displayName', 'email']
},
async (accessToken, refreshToken, profile, done) => {
const email = profile.emails[0].value;
const splitName = profile.displayName.split(' ');
const username = splitName[0];
/**
* @description - finds an existing user or create a new user
* @param {object} user a user
* @param {function} done end of function
* @returns {object} createOrFindUser
*/
const { displayName } = profile;
const user = await User.findOrCreate({
where: { email },
defaults: {
name: displayName,
username,
email
}
});
return done(null, user[0]);
}
));

/**
* @description - set the user id
* @param {object} user a user
* @param {function} done end of function
* @returns {object} user id
*/
passport.serializeUser((user, done) => {
done(null, user.id);
});

/**
* @description - finds the user by id
* @param {object} user a user
* @param {function} done end of function
* @returns {object} user profile
*/
passport.deserializeUser(async (id, done) => {
const user = await User.findByPk(id);
return done(null, user);
});

export default passport;
27 changes: 27 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require('dotenv').config();

const config = {
development: {
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
dialect: 'postgres',
},
test: {
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_TEST,
host: process.env.DB_HOST,
dialect: 'postgres',
},
production: {
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_TEST,
host: process.env.DB_HOST,
dialect: 'postgres',
},
};

module.exports = config;
4 changes: 2 additions & 2 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
secret:
process.env.NODE_ENV === "production" ? process.env.SECRET : "secret"
secret:
process.env.NODE_ENV === 'production' ? process.env.SECRET : 'secret',
};
45 changes: 23 additions & 22 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const mongoose = require("mongoose");
const User = mongoose.model("User");
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');

const User = mongoose.model('User');

passport.use(
new LocalStrategy(
{
usernameField: "user[email]",
passwordField: "user[password]"
},
function(email, password, done) {
User.findOne({ email: email })
.then(function(user) {
if (!user || !user.validPassword(password)) {
return done(null, false, {
errors: { "email or password": "is invalid" }
});
}
new LocalStrategy(
{
usernameField: 'user[email]',
passwordField: 'user[password]',
},
((email, password, done) => {
User.findOne({ email })
.then((user) => {
if (!user || !user.validPassword(password)) {
return done(null, false, {
errors: { 'email or password': 'is invalid' },
});
}

return done(null, user);
})
.catch(done);
}
)
return done(null, user);
})
.catch(done);
}),
),
);
Loading

0 comments on commit 73ee94b

Please sign in to comment.