Skip to content

Commit

Permalink
fix(login): return data in the old format, no breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Apr 11, 2020
1 parent ccf9cb9 commit 5c02325
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 32 deletions.
14 changes: 6 additions & 8 deletions middlewares/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ module.exports.login = async (req, res) => {
});

if (!user) {
return errors.makeUnauthorizedError(res, 'User is not found.');
return errors.makeForbiddenError(res, 'User is not found.');
}

if (!await user.checkPassword(req.body.password)) {
return errors.makeUnauthorizedError(res, 'Password is not valid.');
return errors.makeForbiddenError(res, 'Password is not valid.');
}

if (!user.mail_confirmed_at) {
return errors.makeUnauthorizedError(res, 'Please confirm your mail first.');
return errors.makeForbiddenError(res, 'Please confirm your mail first.');
}

// Some fields can be empty while registering, but we shouldn't allow login for such users.
Expand All @@ -43,10 +43,8 @@ module.exports.login = async (req, res) => {

return res.json({
success: true,
data: {
access_token: accessToken.value,
refresh_token: refreshToken.value
}
access_token: accessToken.value,
refresh_token: refreshToken.value
});
};

Expand Down Expand Up @@ -110,7 +108,7 @@ module.exports.renew = async (req, res) => {
});

if (!token) {
return res.status(401).json({
return res.status(403).json({
success: false,
message: 'Token is not found.'
});
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
"dependencies": {
"@bugsnag/js": "^6.5.0",
"bcrypt": "^3.0.7",
"bcrypt": "^3.0.8",
"body-parser": "^1.19.0",
"eslint": "^6.8.0",
"express": "^4.17.1",
Expand Down
2 changes: 1 addition & 1 deletion scripts/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function createAdmin() {
last_name: 'Admin',
username: 'admin',
email: 'admin@example.com',
password: '5ecret5ecret',
password: '5ecr3t5ecr3t',
about_me: 'I\'m the superadmin of the system, please do not remove me.',
date_of_birth: '1970-01-01',
gender: 'machine',
Expand Down
17 changes: 10 additions & 7 deletions test/api/authorization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Authorization', () => {
}
});

expect(res.statusCode).toEqual(401);
expect(res.statusCode).toEqual(403);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
Expand All @@ -44,7 +44,7 @@ describe('Authorization', () => {
}
});

expect(res.statusCode).toEqual(401);
expect(res.statusCode).toEqual(403);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
Expand All @@ -62,7 +62,7 @@ describe('Authorization', () => {
}
});

expect(res.statusCode).toEqual(401);
expect(res.statusCode).toEqual(403);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
Expand All @@ -80,7 +80,7 @@ describe('Authorization', () => {
}
});

expect(res.statusCode).toEqual(401);
expect(res.statusCode).toEqual(403);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
Expand Down Expand Up @@ -120,7 +120,8 @@ describe('Authorization', () => {

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).toHaveProperty('access_token');
expect(res.body).toHaveProperty('refresh_token');
expect(res.body).not.toHaveProperty('errors');
});

Expand All @@ -138,7 +139,8 @@ describe('Authorization', () => {

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).toHaveProperty('access_token');
expect(res.body).toHaveProperty('refresh_token');
expect(res.body).not.toHaveProperty('errors');
});

Expand All @@ -156,7 +158,8 @@ describe('Authorization', () => {

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).toHaveProperty('access_token');
expect(res.body).toHaveProperty('refresh_token');
expect(res.body).not.toHaveProperty('errors');
});
});
2 changes: 1 addition & 1 deletion test/api/renew.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Tokens renewal', () => {
}
});

expect(res.statusCode).toEqual(401);
expect(res.statusCode).toEqual(403);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
Expand Down

0 comments on commit 5c02325

Please sign in to comment.