-
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.
Merge pull request #14 from andela/ft-login-167573212
#167573212 Implement Login Feature
- Loading branch information
Showing
43 changed files
with
1,006 additions
and
139 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
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,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; |
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,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; |
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
Oops, something went wrong.