-
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.
Merge pull request #12 from andela/ft-user-signout-backend-166841010
#166841010 User should be able to sign out
- Loading branch information
Showing
16 changed files
with
230 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import auth from '../middleware/Auth'; | ||
import { Blacklist, User } from '../db/models'; | ||
/** | ||
* | ||
* | ||
* @class AuthController | ||
*/ | ||
class AuthController { | ||
/** | ||
* | ||
* | ||
* @static | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} res | ||
* @memberof AuthController | ||
*/ | ||
static async login(req, res) { | ||
const { email } = req.body; | ||
try { | ||
const user = await User.findOne({ | ||
where: { email }, | ||
attributes: { exclude: ['password'] } | ||
}); | ||
const { id } = user; | ||
const userToken = auth.authenticate(id); | ||
if (user) { | ||
return res.status(200).json({ | ||
status: 200, | ||
message: 'User successfully Logged In', | ||
data: userToken | ||
}); | ||
} | ||
} catch (error) { | ||
return res.status(500).json({ | ||
status: 500, | ||
message: error, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
*@description Logout a user | ||
* @static | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} res | ||
* @memberof AuthController | ||
*/ | ||
static async logOut(req, res) { | ||
const { token } = req.headers || req.body || req.query; | ||
try { | ||
const createdToken = await Blacklist.create({ | ||
token | ||
}); | ||
return res.status(200).json({ | ||
status: 200, | ||
message: 'User successfully Logged Out', | ||
data: createdToken | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
status: 500, | ||
data: error, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default AuthController; |
Empty file.
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,28 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Blacklists', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
token: { | ||
type: Sequelize.TEXT | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Blacklists'); | ||
} | ||
}; |
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,11 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Blacklist = sequelize.define('Blacklist', { | ||
token: { | ||
type: DataTypes.TEXT, | ||
}, | ||
}, {}); | ||
Blacklist.associate = function(models) { | ||
// associations can be defined here | ||
}; | ||
return Blacklist; | ||
}; |
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,5 +1,3 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Sequelize = require('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
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,27 @@ | ||
// third-party libraries | ||
import jwt from 'jsonwebtoken'; | ||
import config from '../db/config/config'; | ||
|
||
const { secret } = config; | ||
|
||
const auth = { | ||
/** | ||
* @static | ||
* @param {object} user | ||
* @description Generates token for user | ||
* @return {string} string | ||
*/ | ||
authenticate(user) { | ||
return jwt.sign( | ||
{ | ||
id: user.id, | ||
}, | ||
secret, | ||
{ | ||
expiresIn: '24h' | ||
} | ||
); | ||
} | ||
}; | ||
|
||
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,9 @@ | ||
import express from 'express'; | ||
import AuthController from '../controllers/Auth'; | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/login', AuthController.login); | ||
router.post('/logout', AuthController.logOut); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import express from 'express'; | ||
import authRoute from './auth'; | ||
|
||
const router = express.Router(); | ||
|
||
router.use('/api/v1/auth', authRoute); | ||
|
||
export default router; |
Empty file.
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,36 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../src/server'; | ||
|
||
chai.should(); | ||
chai.use(chaiHttp); | ||
const { expect } = chai; | ||
|
||
describe('User Controller', () => { | ||
it('should logout user successfully', (done) => { | ||
chai.request(app).post('/api/v1/auth/logout') | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
expect(res.body.message).equal('User successfully Logged Out'); | ||
expect(res.body.data).to.have.property('id'); | ||
expect(res.body.data).to.have.property('token'); | ||
done(); | ||
}); | ||
}); | ||
it('should login user successfully', (done) => { | ||
const user = { | ||
email: 'john.doe@andela.com', | ||
password: 'password', | ||
}; | ||
chai.request(app).post('/api/v1/auth/login') | ||
.send(user) | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
expect(res.body.message).equal('User successfully Logged In'); | ||
expect(res.body).to.have.property('message'); | ||
expect(res.body).to.have.property('data'); | ||
expect(res.body).to.have.property('status'); | ||
done(); | ||
}); | ||
}); | ||
}); |
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