-
Notifications
You must be signed in to change notification settings - Fork 6
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 #16 from andela/ft-setup-user-registration-167733810
#167733810 A JWT Is Sent Back On Successful Registration
- Loading branch information
Showing
11 changed files
with
322 additions
and
43 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,47 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import jwt from 'jsonwebtoken'; | ||
import models from '../models'; | ||
|
||
const UserModel = models.User; | ||
|
||
class AuthController { | ||
static async signUp(req, res) { | ||
try { | ||
const { | ||
firstName, | ||
lastName, | ||
email, | ||
password, | ||
} = req.body; | ||
// Check if the email already exists | ||
const user = await UserModel.findOne({ where: { email, } }); | ||
if (user) { | ||
res.status(400).json({ status: 400, error: 'Email already exists', }); | ||
} else { | ||
// Password hash occurs in the hook beforeSave | ||
const newUser = await UserModel.create({ | ||
firstName, | ||
lastName, | ||
email, | ||
password, | ||
}); | ||
// Generate JWT | ||
const token = jwt.sign({ id: newUser.id, email, }, process.env.JWT_SECRET, { expiresIn: '50h' }); | ||
return res.status(201).json({ | ||
status: 201, | ||
data: { | ||
id: newUser.id, | ||
token, | ||
firstName, | ||
lastName, | ||
email, | ||
}, | ||
}); | ||
} | ||
} catch (error) { | ||
return res.status(500).json({ status: 500, error }); | ||
} | ||
} | ||
} | ||
|
||
export default AuthController; |
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,6 @@ | ||
import authController from './auth'; | ||
|
||
export const AuthController = authController; | ||
|
||
// An example for subsequent controller imports | ||
export const xController = 'xController'; |
Oops, something went wrong.