-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ft-#166240824] enable login via Facebook, Google, Twitter
- Loading branch information
Showing
28 changed files
with
498 additions
and
215 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
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,66 @@ | ||
import bcrypt, { hashSync, genSaltSync } from 'bcrypt'; | ||
import models from '../models/index'; | ||
import generateToken from '../../helpers/tokens/generate.token'; | ||
import sendResult from '../../helpers/results/send.auth'; | ||
|
||
const { User } = models; | ||
|
||
/** | ||
* containing all user's model controllers (signup, login) | ||
* | ||
* @export | ||
* @class Auth | ||
*/ | ||
export default class Auth { | ||
/** | ||
* register a new user | ||
* | ||
* @static | ||
* @param {*} req the request | ||
* @param {*} res the response to be sent | ||
* @memberof Auth | ||
* @returns {Object} res | ||
*/ | ||
static async signup(req, res) { | ||
const { username, email, password } = req.body; | ||
const salt = genSaltSync(parseFloat(process.env.BCRYPT_HASH_ROUNDS) || 10); | ||
const hashedPassword = hashSync(password, salt); | ||
const user = await User.create({ | ||
username, | ||
email, | ||
password: hashedPassword | ||
}); | ||
const tokenData = { username, email }; | ||
const token = generateToken(tokenData, process.env.TOKEN_KEY); | ||
return sendResult(res, 201, 'user created successfully', user, token); | ||
} | ||
|
||
/** | ||
*login an existing user | ||
* | ||
* @static | ||
* @param {*} req the request | ||
* @param {*} res the response to be sent | ||
* @memberof Auth | ||
* @returns {Object} res | ||
*/ | ||
static login(req, res) { | ||
const { email, password } = req.body; | ||
User.findByEmail(email).then((user) => { | ||
if (user) { | ||
const isPasswordValid = bcrypt.compareSync(password, user.dataValues.password); | ||
if (isPasswordValid) { | ||
const tokenData = { username: user.dataValues.username, email }; | ||
const token = generateToken(tokenData, process.env.TOKEN_KEY); | ||
return sendResult(res, 200, 'user logged in successfully', user, token); | ||
} | ||
return res.status(401).json({ | ||
message: 'password is incorrect' | ||
}); | ||
} | ||
return res.status(404).json({ | ||
message: "user doesn't exist" | ||
}); | ||
}); | ||
} | ||
} |
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 was deleted.
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,10 @@ | ||
const token = (sequelize, DataTypes) => { | ||
const Token = sequelize.define('Tokens', { | ||
data: { | ||
type: DataTypes.TEXT, | ||
} | ||
}); | ||
return Token; | ||
}; | ||
|
||
export default token; |
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 { Router } from 'express'; | ||
import authController from '../controllers/authController'; | ||
|
||
const authRouter = Router(); | ||
|
||
authRouter.post('/signup', authController.signup); | ||
authRouter.post('/login', authController.login); | ||
|
||
|
||
export default authRouter; |
This file was deleted.
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import express from 'express'; | ||
import dbTestRouter from './db.test'; | ||
import authRouter from './authRouter'; | ||
|
||
const v2router = express(); | ||
const router = express(); | ||
|
||
v2router.use('/dbTest', dbTestRouter); | ||
router.use('/auth', authRouter); | ||
|
||
export default v2router; | ||
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
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.