-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature-[167573212]: implement login feature
- added login route - tested login route - created user model, migration and seeds file - added a findUser helper function - added a password helper function that compares password [finishes #167573212]
- Loading branch information
Showing
18 changed files
with
340 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import bcrypt from 'bcryptjs'; | ||
import { | ||
findUser, | ||
generateToken, | ||
serverResponse, | ||
serverError, | ||
expiryDate | ||
} from '../helpers'; | ||
import models from '../database/models'; | ||
|
||
const { Sessions } = models; | ||
const mobileDeviceIndicator = [ | ||
'mobile', | ||
'andriod', | ||
'iphone', | ||
'tablet', | ||
'ipad', | ||
'ipod' | ||
]; | ||
|
||
/** | ||
* | ||
* | ||
* @class Session | ||
*/ | ||
class Session { | ||
/** | ||
* | ||
* | ||
* @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 (!/\D/.test(userLogin)) { | ||
return serverResponse(res, 400, { | ||
message: 'user login must be a string' | ||
}); | ||
} | ||
const user = await findUser(userLogin); | ||
let verifyPassword; | ||
if (user) verifyPassword = bcrypt.compareSync(password, user.password); | ||
if (!user || !verifyPassword) { | ||
return serverResponse(res, 403, { | ||
message: 'incorrect username or password' | ||
}); | ||
} | ||
const userAgent = req.headers['user-agent']; | ||
const devicePlatform = mobileDeviceIndicator.some( | ||
device => userAgent.toLowerCase().indexOf(device) > 0 | ||
) | ||
? 'mobile' | ||
: 'browser'; | ||
const { id, dataValues } = user; | ||
const expiresAt = expiryDate(devicePlatform); | ||
const token = generateToken(id); | ||
await Sessions.create({ | ||
userId: id, | ||
token, | ||
expiresAt, | ||
userAgent, | ||
ipAddress: req.ip, | ||
devicePlatform | ||
}); | ||
delete dataValues.password; | ||
return serverResponse(res, 200, { user: { ...dataValues }, token }); | ||
} catch (error) { | ||
serverError(res); | ||
} | ||
} | ||
} | ||
|
||
export default Session; |
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,14 @@ | ||
/** | ||
* @name expiryDate | ||
* @param {string} platform - platform that apps is accessed from | ||
* @returns {date} date object | ||
*/ | ||
const expiryDate = (platform) => { | ||
const todaysDate = new Date(Date.now()); | ||
const expirationDate = new Date(); | ||
const timeExtension = platform === 'browser' ? 8 : 24; | ||
expirationDate.setHours(todaysDate.getHours() + timeExtension); | ||
return expirationDate; | ||
}; | ||
|
||
export default { expiryDate }; |
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,21 @@ | ||
import models from '../database/models'; | ||
|
||
const { User } = models; | ||
/** | ||
* @name findUser | ||
* @param {string} parameter - user input | ||
* @returns {object} user object | ||
*/ | ||
const findUser = async (parameter) => { | ||
let param; | ||
if (!/\D/.test(parameter)) { | ||
param = { id: parameter }; | ||
} else if (/\D/.test(parameter) && /@.{2,15}\./.test(parameter)) { | ||
param = { email: parameter }; | ||
} else { | ||
param = { userName: parameter }; | ||
} | ||
return User.findOne({ where: param }); | ||
}; | ||
|
||
export default findUser; |
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import express from 'express'; | ||
import users from './user'; | ||
import session from './session'; | ||
|
||
const route = express.Router(); | ||
|
||
route.use('/users', users); | ||
route.use('/', session); | ||
|
||
export default route; |
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 express from 'express'; | ||
import Session from '../controllers/Session'; | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/sessions/create', Session.create); | ||
|
||
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,13 @@ | ||
import { expect } from 'chai'; | ||
import { expiryDate } from '../../server/helpers'; | ||
|
||
describe('Find User Test', () => { | ||
it('find user by id', async () => { | ||
const date = await expiryDate('browser'); | ||
expect(date).to.be.a('date'); | ||
}); | ||
it('find user by id', async () => { | ||
const date = await expiryDate('mobile'); | ||
expect(date).to.be.a('date'); | ||
}); | ||
}); |
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,9 @@ | ||
import { expect } from 'chai'; | ||
import { findUser } from '../../server/helpers'; | ||
|
||
describe('Find User Test', () => { | ||
it('find user by id', async () => { | ||
const user = await findUser(1); | ||
expect(user).to.have.property('dataValues'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import * as generateToken from './generateToken.test'; | ||
import * as findToken from './findToken.test'; | ||
import * as sessionMigration from './sessionMigration.test'; | ||
import * as findUSer from './findUser.test'; | ||
import * as dateHelper from './dateHelper.test'; | ||
|
||
export { generateToken, findToken }; | ||
export { | ||
generateToken, findToken, sessionMigration, findUSer, dateHelper | ||
}; |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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 @@ | ||
const rightUserWithEmail = { | ||
userLogin: 'abiola.jz@andela.com', | ||
password: 'incorrect' | ||
}; | ||
|
||
const rightUserWithUserName = { | ||
userLogin: 'JhayX', | ||
password: 'incorrect' | ||
}; | ||
|
||
const userWithId = { | ||
userLogin: 1, | ||
password: 'incorrect' | ||
}; | ||
|
||
const wrongUser = { | ||
userLogin: 'a54678yujkomnibvr5fgrht6y78umiouh@gmail.com', | ||
password: 'incorrect' | ||
}; | ||
|
||
export default { | ||
rightUserWithUserName, | ||
rightUserWithEmail, | ||
userWithId, | ||
wrongUser | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import './signup.test'; | ||
import * as signUp from './signup.test'; | ||
import * as sessions from './sessions.test'; | ||
|
||
export default './signup.test'; | ||
export { signUp, sessions }; |
Oops, something went wrong.