-
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
19 changed files
with
480 additions
and
11 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
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,69 @@ | ||
import bcrypt from 'bcryptjs'; | ||
import helpers from '../helpers'; | ||
// import models from '../database/models'; | ||
|
||
// const { Session } = models; | ||
const { | ||
findUser, | ||
// generateToken, | ||
serverResponse, | ||
serverError | ||
// expiryDate | ||
} = helpers; | ||
// const mobileDeviceIndicator = | ||
// ['mobile', 'andriod', 'iphone', 'tablet', 'ipad', 'ipod']; | ||
// const userAgent = req.headers['user-agent'] | ||
// const devicePlatform = | ||
// mobileDeviceIndicator | ||
// .some(device => userAgent.toLowerCase().indexOf(device) > 0) ? 'mobile' : 'browser'; | ||
|
||
/** | ||
* | ||
* | ||
* @class Sessions | ||
*/ | ||
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 (!/\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 { id, dataValues } = user; | ||
// const token = generateToken(id); | ||
// await Session.create({ | ||
// userId: id, | ||
// token, | ||
// expiresAt: expiryDate, | ||
// userAgent, | ||
// ipAddress: req.ip | ||
// devicePlatform, | ||
// }); | ||
delete dataValues.password; | ||
return serverResponse(res, 200, { user: { ...dataValues } }); | ||
} catch (error) { | ||
serverError(res); | ||
} | ||
} | ||
} | ||
|
||
export default Sessions; |
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 @@ | ||
/* istanbul ignore file */ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import Sequelize from 'sequelize'; | ||
|
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,9}\./.test(parameter)) { | ||
param = { email: parameter }; | ||
} else { | ||
param = { userName: parameter }; | ||
} | ||
return User.findOne({ where: param }); | ||
}; | ||
|
||
export default findUser; |
Oops, something went wrong.