Skip to content

Commit

Permalink
Merge 7ec3c16 into 865f2e5
Browse files Browse the repository at this point in the history
  • Loading branch information
oesukam committed Feb 11, 2019
2 parents 865f2e5 + 7ec3c16 commit 72ddcbc
Show file tree
Hide file tree
Showing 9 changed files with 10,085 additions and 329 deletions.
30 changes: 16 additions & 14 deletions __tests__/routes/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,37 @@ describe('auth', () => {
test('Signup- bad request', async () => {
expect.assertions(2);
const res = await request(app)
.post(`${urlPrefix}/users/signup`)
.send({ email: 'test@email.com', password: 'test@test' });
.post(`${urlPrefix}/users`)
.send({ user: { email: 'test@email.com', password: 'test@test' } });
expect(res.status).toBe(400);
expect(res.body.message).toBe('Bad Request');
});

test('Signup- success', async () => {
expect.assertions(2);
expect.assertions(3);
const res = await request(app)
.post(`${urlPrefix}/users/signup`)
.send({ username: 'test', email: 'test@email.com', password: 'test@test' });
.post(`${urlPrefix}/users`)
.send({ user: { username: 'test', email: 'test@email.com', password: 'test@test' } });
expect(res.status).toBe(201);
expect(res.body.message).toBe('Account created sucessfully');
expect(res.body).toBeDefined();
expect(res.body.user).toBeDefined();
});

test('Signup- account already exist', async () => {
expect.assertions(2);
expect.assertions(3);
const res = await request(app)
.post(`${urlPrefix}/users/signup`)
.send({ username: 'test', email: 'test@email.com', password: 'test@test' });
.post(`${urlPrefix}/users`)
.send({ user: { username: 'test', email: 'test@email.com', password: 'test@test' } });
expect(res.status).toBe(401);
expect(res.body).toBeDefined();
expect(res.body.message).toBe('Account already exist');
});

test('should return Bad Request message', async () => {
expect.assertions(2);
const res = await request(app)
.post(`${urlPrefix}/users/login`)
.send({ email: 'fake@email.com', password: 'test@test' });
.send({ user: { email: 'fake@email.com', password: 'test@test' } });
expect(res.status).toBe(400);
expect(res.body.message).toBe('Bad Request');
});
Expand All @@ -61,9 +63,9 @@ describe('auth', () => {
expect.assertions(2);
const res = await request(app)
.post(`${urlPrefix}/users/login`)
.send({ username: 'fake@email.com', password: 'test@test' });
.send({ user: { username: 'fake@email.com', password: 'test@test' } });
expect(res.status).toBe(404);
expect(res.body.message).toBe('Email or Password is incorrect');
expect(res.body.message).toBe("Email and password don't match");
});

test('should return user data and token', async () => {
Expand All @@ -78,9 +80,9 @@ describe('auth', () => {
});
const res = await request(app)
.post(`${urlPrefix}/users/login`)
.send({ username, password });
.send({ user: { username, password } });
expect(res.status).toBe(200);
expect(res.body.token).toBeDefined();
expect(res.body.user).toBeDefined();
expect(res.body.user.token).toBeDefined();
});
});
34 changes: 18 additions & 16 deletions controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,31 @@ class AuthController {
* @returns {Object} Returns the response
*/
static async signup(req, res) {
let user;
let userModel;
let token;
const { body } = req;
const {
body: { user }
} = req;
try {
user = await User.findOne({
where: { [Op.or]: [{ email: body.email }, { username: body.username }] }
userModel = await User.findOne({
where: { [Op.or]: [{ email: user.email }, { username: user.username }] }
});
if (user) {
return res.status(401).json({ status: 401, message: 'Account already exist' });
if (userModel) {
return res.status(401).json({ message: 'Account already exist' });
}
const password = await bcrypt.hash(body.password, 10);
const password = await bcrypt.hash(user.password, 10);

user = await User.create({ ...body, password });
userModel = await User.create({ ...user, password });

token = jwt.sign({ id: user.id, userType: user.userType }, JWT_SECRET);
token = jwt.sign({ id: userModel.get().id, userType: userModel.get().userType }, JWT_SECRET);
} catch (error) {
return res.status(401).json({ status: 401, message: 'Please try again' });
return res.status(401).json({ message: 'Please try again' });
}

res.cookie('jwt', jwt, { httpOnly: true, secure: true });
const { password, ...userData } = user.get();
const { password, ...userData } = userModel.get();
return res.status(201).json({
status: 201,
message: 'Account created sucessfully',
token,
User: userData
user: { ...userData, ...token }
});
}

Expand All @@ -58,6 +57,9 @@ class AuthController {
* @returns {Object} Returns the response
*/
static async login(req, res, next) {
const { user: loginUser } = req.body;
req.body.username = loginUser.username;
req.body.password = loginUser.password;
passport.authenticate('login', async (err, user) => {
try {
if (err || !user) {
Expand All @@ -70,7 +72,7 @@ class AuthController {

res.cookie('jwt', jwt, { httpOnly: true, secure: true });

return res.json({ token, user });
return res.json({ user: { ...user, token } });
});
} catch (error) {
return next(error);
Expand Down
Loading

0 comments on commit 72ddcbc

Please sign in to comment.