Skip to content

Commit

Permalink
Merge branch 'develop' into ft-login-signup-with-email-164489784
Browse files Browse the repository at this point in the history
  • Loading branch information
mwibutsa committed Apr 10, 2019
2 parents f7eae09 + c72ba96 commit 770abea
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
repo_token: EC2Rj8qko27GeraaipSpFO2eOHCHQNDfZ
repo_token: uaTjvZzIlc7gXmcqdiDOEEZZ1e0bpGHGW
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ node_js:
install:
- npm i
script:
- npm run migrate
- npm run test
after_success:
- npm run coverage
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Authors Haven - A Social platform for the creative at heart.
[![Build Status](https://travis-ci.com/andela/strikers-ah-backend.svg?branch=develop)](https://travis-ci.com/andela/strikers-ah-backend)
[![Reviewed by Hound](https://img.shields.io/badge/Reviewed%20by-Hound-%239069ad.svg)](https://houndci.com)

[![Coverage Status](https://coveralls.io/repos/github/andela/strikers-ah-backend/badge.svg)](https://coveralls.io/github/andela/strikers-ah-backend)
## Vision

Create a community of like minded authors to foster inspiration and innovation
Expand Down
13 changes: 9 additions & 4 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class User {
const data = req.body;
const newUser = {
...data,
password: await helper.hashPassword(data.password),
// password: await helper.hashPassword(data.password),
};
// check if the user does not already exist
const emailUsed = await UserModel.findOne({ where: { email: newUser.email } });
Expand All @@ -38,7 +38,7 @@ class User {
}
return res.status(400).json({ error: uniqueEmailUsername });
} catch (error) {
return res.status(500).json({ error: `${error}` });
return res.status(400).send(error);
}
}

Expand All @@ -50,7 +50,11 @@ class User {
static async loginWithEmail(req, res) {
const { email, password } = req.body;
try {
const user = await UserModel.findOne({ where: { [Op.or]: [{ email }, { username: email }] } });
const user = await UserModel.findOne({
where: {
[Op.or]: [{ email }, { username: email }]
}
});
// verify password
if (user && helper.comparePassword(password, user.password)) {
// return user and token
Expand All @@ -60,7 +64,8 @@ class User {
}
return res.status(401).json({ error: 'Invalid username or password' });
} catch (error) {
return res.status(500).json({ error: `Server Error: ${error}` });
const status = (error.name === 'SequelizeValidationError') ? 400 : 500;
return res.status(status).json({ error: `${error.message}` });
}
}

Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import bodyParser from 'body-parser';
import passport from 'passport';
import swaggerUi from 'swagger-ui-express';
import YAML from 'yamljs';
// import routes from './routes/routes';
import routes from './routes/routes';
import user from './routes/user';
// import Strategy from './middlewares/auth';
import Strategy from './middlewares/auth';

const swaggerDocument = YAML.load('./swagger.yaml');

Expand All @@ -23,12 +23,12 @@ const port = process.env.PORT || 3000;

app.listen(port);
app.use(passport.initialize());
// const strategy = new Strategy();
const strategy = new Strategy();
app.use('/api/v1/login', user);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

app.use('/', (req, res) => {
res.send("Welcome");
res.send('Welcome');
});


Expand Down
24 changes: 0 additions & 24 deletions middlewares/validations.js

This file was deleted.

40 changes: 38 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import helper from '../helpers/helper';

const UserModel = (Sequelize, DataTypes) => {
const User = Sequelize.define('user', {
firstname: { type: DataTypes.STRING, allowNull: true },
Expand All @@ -6,13 +8,47 @@ const UserModel = (Sequelize, DataTypes) => {

username: { type: DataTypes.STRING, allowNull: false },

email: { type: DataTypes.STRING, allowNull: true },
email: {
type: DataTypes.STRING,
allowNull: true,
validate: {
len: {
args: [6, 128],
msg: 'an email address should be between 6 and 128 characters long',
},
isEmail: {
args: true,
msg: 'This is not a valid email address'
},
}
},

bio: { type: DataTypes.STRING, allowNull: true, },

image: { type: DataTypes.TEXT, allowNull: true },

password: { type: DataTypes.TEXT, allowNull: true, },
password: {
type: DataTypes.TEXT,
allowNull: false,
validate: {
len: {
args: [8, 128],
msg: 'Password should be 8 characters minimum',
},
validate() {
const hasCharacter = ['@', '*', '%', '^', '!', '~', '`', '"', '\''].some(r => this.password.includes(r));
if (!hasCharacter) throw new Error('The password should have at least one special character');
if (!/(.*\d.*)/.test(this.password)) throw new Error('The password Must contain at least one number');
if (!/(.*[a-z].*)/.test(this.password)) {
throw new Error('The password must contain at least one lower case character');
}
if (!/(.*[A-Z].*)/.test(this.password)) {
throw new Error('the password should contain at least one uppercase character');
}
this.password = helper.hashPassword(this.password);
}
}
},

provider: { type: DataTypes.STRING, allowNull: true, },

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"test": "NODE_ENV=test nyc --reporter=html --reporter=text mocha --require @babel/polyfill --require @babel/register ./test/*.js --exit",
"start": "babel-node ./index.js",
"dev": "nodemon --exec babel-node ./index.js ",
"coverage": "nyc --reporter=lcov --reporter=text-lcov | coveralls",
"migrate": "node_modules/.bin/sequelize db:migrate:undo:all && node_modules/.bin/sequelize db:migrate",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint ./",
"lint-fix": "eslint ./ --fix",
"fix-code": "prettier-eslint --write './**/*.js' "
Expand Down Expand Up @@ -61,6 +62,7 @@
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.12.4",
"istanbul": "^0.4.5",
"mocha": "^6.0.2",
"mocha-lcov-reporter": "^1.3.0",
"nodemon": "^1.18.10",
Expand Down
6 changes: 2 additions & 4 deletions routes/user.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import express from 'express';
import passport from 'passport';
import user from '../controllers/user';
import validations from '../middlewares/validations';
import schema from '../helpers/schema';

const router = express.Router();

router.post('/login', validations.validate(schema.loginSchema, schema.options), user.loginWithEmail);
router.post('/login', user.loginWithEmail);

router.post('/', validations.validate(schema.signUpSchema, schema.options), user.signUpWithEmail);
router.post('/', user.signUpWithEmail);

router.get('/auth/google', passport.authenticate('google', { scope: ['email', 'profile'] }));

Expand Down
2 changes: 1 addition & 1 deletion test/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chai.should();
/**
* test username generator class
*/
describe('/ GET all meetups', () => {
describe('/ TEST Middleware', () => {
it('it should generate random username', (done) => {
const strings = new usernameGenerator('myusername');
const result = strings.getUsername();
Expand Down

0 comments on commit 770abea

Please sign in to comment.