Skip to content

Commit

Permalink
Merge pull request #16 from andela/ft-setup-user-registration-167733810
Browse files Browse the repository at this point in the history
#167733810 A JWT Is Sent Back On Successful Registration
  • Loading branch information
Oluwafayokemi committed Aug 21, 2019
2 parents 62a9b4d + 02913a2 commit 1a85dc4
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 43 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ services:
- postgresql
before_script:
- psql -c 'create database barefootnomadDB;' -U postgres
- psql -c 'CREATE USER evans WITH PASSWORD null;' -U postgres
- npm run build
script:
- npm test
Expand Down
119 changes: 96 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A Social platform for the creative at heart",
"main": "index.js",
"scripts": {
"test": "nyc mocha './src/**/**test.js' --require @babel/polyfill --require @babel/register --timeout 30000 --exit",
"test": "npm run migration && nyc mocha './src/**/**test.js' --require @babel/polyfill --require @babel/register --timeout 30000 --exit",
"start-dev": "nodemon --exec babel-node src/index.js",
"start": "node dist/index.js",
"start-docker:dev": "babel-node src/index.js",
Expand All @@ -18,17 +18,18 @@
"author": "Andela Simulations Programme",
"license": "MIT",
"dependencies": {
"faker": "^4.1.0",
"bcryptjs": "^2.4.3",
"@babel/polyfill": "^7.4.4",
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"chai-http": "^4.3.0",
"cors": "^2.8.4",
"dotenv": "^6.0.0",
"ejs": "^2.6.1",
"errorhandler": "^1.5.0",
"express": "^4.16.3",
"express-jwt": "^5.3.1",
"express-session": "^1.15.6",
"faker": "^4.1.0",
"jsonwebtoken": "^8.3.0",
"method-override": "^2.3.10",
"methods": "^1.1.2",
Expand Down
47 changes: 47 additions & 0 deletions src/controllers/auth.js
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;
6 changes: 6 additions & 0 deletions src/controllers/index.js
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';
Loading

0 comments on commit 1a85dc4

Please sign in to comment.