Skip to content

Commit

Permalink
Merge pull request #14 from andela/ft-login-167573212
Browse files Browse the repository at this point in the history
#167573212 Implement Login Feature
  • Loading branch information
LostLite authored and Adekoreday committed Aug 5, 2019
2 parents c3dd0ed + b3e037e commit ef869be
Show file tree
Hide file tree
Showing 43 changed files with 1,006 additions and 139 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ DB_URL_PRODUCTION=postgres://authors:haven@localhost:2800/haven
NODE_ENV=development
BASE_URL=/api/v5
JWT_KEY=passkey
REDIRECT_URL=http://localhost:5000/api/v1/auth/facebook/callback
FACEBOOK_APP_ID=4342d2e47a9265845478yuy343y
FACEBOOK_APP_SECRET=5y4384y545510
106 changes: 98 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"scripts": {
"lint": "eslint '**/*.js'",
"lint:fix": "prettier-eslint '**/*.js' --write",
"test:staged": "nyc mocha --timeout 1000 -r esm --exit",
"build": "babel server --out-dir dist && npm run copy:docs",
"copy:docs": "cp -r server/docs/ dist/docs/",
"coverage": "nyc report --reporter=text-lcov | coveralls",
Expand All @@ -20,7 +19,7 @@
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm run test:staged"
"pre-commit": "lint-staged && npm test"
}
},
"author": "Andela Simulations Programme",
Expand All @@ -33,6 +32,8 @@
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.9.1",
"passport": "^0.4.0",
"passport-facebook": "^3.0.0",
"pg": "^7.11.0",
"pg-hstore": "^2.3.3",
"sequelize": "^5.10.2",
Expand Down Expand Up @@ -61,6 +62,7 @@
"lint-staged": "^9.2.1",
"mocha": "^6.1.4",
"mocha-lcov-reporter": "^1.3.0",
"nock": "^10.0.6",
"nodemon": "^1.19.1",
"nyc": "^14.1.1",
"prettier-eslint-cli": "^5.0.0",
Expand Down
41 changes: 41 additions & 0 deletions server/controllers/Auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
getUserAgent,
createSocialUsers,
serverResponse,
getSocialUserData
} from '../helpers';
/**
* @export
* @class Auth
*/
class Auth {
/**
* @name facebookSocialLogin
* @async
* @static
* @memberof Auth
* @param {Object} request express request object
* @param {Object} response express response object
* @returns {JSON} JSON object with details of new user
*/
static async facebookSocialLogin(request, response) {
const { devicePlatform, userAgent } = getUserAgent(request);
const { ip } = request;
const { givenName, familyName, email } = getSocialUserData(
request,
'facebook'
);
const data = {
firstName: givenName,
lastName: familyName,
email,
devicePlatform,
userAgent,
ipAddress: ip
};
const User = await createSocialUsers(data);
delete User.password;
serverResponse(response, 200, User);
}
}
export default Auth;
66 changes: 66 additions & 0 deletions server/controllers/Sessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import bcrypt from 'bcryptjs';
import {
findUser,
generateToken,
serverResponse,
serverError,
expiryDate,
getUserAgent
} from '../helpers';
import models from '../database/models';

const { Session } = models;

/**
*
*
* @class Session
*/
class Sessions {
/**
*
*
* @static
* @param {object} req - request object
* @param {object} res - response object
* @memberof User
* @returns {json} object
*/
static async create(req, res) {
try {
const { userLogin, password } = req.body;
if (Object.keys(req.body).length < 1 || !userLogin || !password) {
return serverResponse(res, 400, {
message: 'invalid authentication details'
});
}
const user = await findUser(userLogin);
let verifyPassword;
if (user) verifyPassword = bcrypt.compareSync(password, user.password);
if (!user || !verifyPassword) {
return serverResponse(res, 401, {
message: 'incorrect username or password'
});
}
const { devicePlatform, userAgent } = getUserAgent(req);
const { id, dataValues } = user;
const expiresAt = expiryDate(devicePlatform);
const token = generateToken(id);
await Session.create({
userId: id,
token,
expiresAt,
userAgent,
ipAddress: req.ip,
devicePlatform
});
res.set('Authorization', token);
delete dataValues.password;
return serverResponse(res, 200, { user: { ...dataValues }, token });
} catch (error) {
serverError(res);
}
}
}

export default Sessions;
7 changes: 5 additions & 2 deletions server/controllers/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
serverResponse,
serverError,
checkEmail,
checkUserName
checkUserName,
generateToken
} from '../helpers';

const { User } = models;
Expand Down Expand Up @@ -40,8 +41,10 @@ class Users {
...req.body,
password: hashedPassword
});
const token = generateToken(user.id);
res.set('Authorization', token);
delete user.dataValues.password;
return serverResponse(res, 201, { user });
return serverResponse(res, 201, { user, token });
} catch (error) {
return serverError(res);
}
Expand Down
2 changes: 1 addition & 1 deletion server/database/models/session.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default (sequelize, DataTypes) => {
const Session = sequelize.define(
'Sessions',
'Session',
{
userId: DataTypes.INTEGER,
active: DataTypes.BOOLEAN,
Expand Down
Loading

0 comments on commit ef869be

Please sign in to comment.