-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- creates endpoint for user to login - gives user a token upon signin [Finishes#168781679]
- Loading branch information
1 parent
2f278ba
commit 4725039
Showing
16 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ package-lock.json | |
# Seeders | ||
src/database/seeders | ||
|
||
# Migrations | ||
/src/database/migrations |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import models from '../database/models'; | ||
import responseHelper from '../helpers/responseHelper'; | ||
import messages from '../helpers/responseMessages'; | ||
|
||
class UserController { | ||
static async signIn(req, res) { | ||
const { email, password } = req.body; | ||
const user = await models.users.findOne({ where: { email, password } }); | ||
if (!user) { | ||
return responseHelper(res, 400, messages.user.error.LOGIN_FAILURE); | ||
} | ||
if (user.verified === false) { | ||
return responseHelper(res, 400, messages.user.error.VERIFY_FIRST); | ||
} | ||
const userToken = jwt.sign({ email }, process.env.SECRET_KEY, { expiresIn: '1h' }); | ||
const userInfo = { | ||
userID: user.id, | ||
username: user.username, | ||
email: user.email, | ||
token: userToken | ||
}; | ||
return responseHelper(res, 200, messages.user.success.SUCCESSFUL_LOGIN, userInfo); | ||
} | ||
} | ||
export default UserController; |
13 changes: 13 additions & 0 deletions
13
src/database/migrations/20191008073714-addPasswordColumn.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.addColumn( | ||
'users', | ||
'password', | ||
Sequelize.STRING | ||
), | ||
|
||
down: (queryInterface, Sequelize) => queryInterface.removeColumn( | ||
'users', | ||
'password' | ||
) | ||
}; |
13 changes: 13 additions & 0 deletions
13
src/database/migrations/20191008112757-addVerifiedColumn.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.addColumn( | ||
'users', | ||
'verified', | ||
Sequelize.BOOLEAN | ||
), | ||
|
||
down: (queryInterface, Sequelize) => queryInterface.removeColumn( | ||
'users', | ||
'verified' | ||
) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.bulkInsert('users', [{ | ||
username: 'caretUser', | ||
email: 'user@caretbn.com', | ||
password: 'default', | ||
verified: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}], {}); | ||
}, | ||
|
||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.bulkDelete('users', null, {}); | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default (res, status, message, data) => res.status(status).json({ status, message, data }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const messages = { | ||
user: { | ||
success: { | ||
SUCCESSFUL_LOGIN: 'User logged in successfully!' | ||
}, | ||
error: { | ||
LOGIN_FAILURE: 'Incorrect email or password!', | ||
VERIFY_FIRST: 'Please verify your email first!', | ||
} | ||
} | ||
}; | ||
|
||
export default messages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import express from 'express'; | ||
import UserController from '../../controllers/userController'; | ||
|
||
const { signIn } = UserController; | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/auth/login', signIn); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe } from 'mocha'; | ||
import test from './test'; | ||
import userTest from './userTest'; | ||
|
||
describe('Testing Barefoot Nomad...', () => { | ||
describe('Initial Tests', test); | ||
describe('User Tests', userTest); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import { describe, it } from 'mocha'; | ||
import app from '../index'; | ||
import messages from '../helpers/responseMessages'; | ||
|
||
|
||
chai.should(); | ||
chai.use(chaiHttp); | ||
|
||
const verifiedUser = { email: 'user@caretbn.com', password: 'default' }; | ||
const unVerifiedUser = { email: 'johndoe@test.com', password: 'default' }; | ||
const invalidData = { email: 'email@email.com', password: 'password' }; | ||
|
||
describe('User Login Test', () => { | ||
it('it should should return 200 and log in a user successfully ', done => { | ||
chai.request(app) | ||
.post('/api/v1/auth/login') | ||
.send(verifiedUser) | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
res.body.should.be.a('object'); | ||
res.body.should.have.property('status').eql(200); | ||
res.body.should.have.property('message').eql(`${messages.user.success.SUCCESSFUL_LOGIN}`); | ||
done(); | ||
}); | ||
}); | ||
it('it should should return 400 and tell the user that email or password is incorrect ', done => { | ||
chai.request(app) | ||
.post('/api/v1/auth/login') | ||
.send(invalidData) | ||
.end((err, res) => { | ||
res.should.have.status(400); | ||
res.body.should.be.a('object'); | ||
res.body.should.have.property('status').eql(400); | ||
res.body.should.have.property('message').eql(`${messages.user.error.LOGIN_FAILURE}`); | ||
done(); | ||
}); | ||
}); | ||
it('it should should return 400 and tell the user to verify first ', done => { | ||
chai.request(app) | ||
.post('/api/v1/auth/login') | ||
.send(unVerifiedUser) | ||
.end((err, res) => { | ||
res.should.have.status(400); | ||
res.body.should.be.a('object'); | ||
res.body.should.have.property('status').eql(400); | ||
res.body.should.have.property('message').eql(`${messages.user.error.VERIFY_FIRST}`); | ||
done(); | ||
}); | ||
}); | ||
}); |