Skip to content

Commit

Permalink
feat(user account verification): Implemention of user account verific…
Browse files Browse the repository at this point in the history
…ation to avoid spams

- send Verify email to user using nodemailer
- verify if token in email url is same with token in database
- update isVerified to true
- user can login only is verified
[Delivers #187419049]
  • Loading branch information
Angemichel12 committed Apr 25, 2024
1 parent d014e09 commit eb6f169
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ SERVICE = < your SERVICE >


EMAIL=<your email>
PASSWORD=<your email password>
PASSWORD=<your email>

5 changes: 5 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ jobs:
runs-on: ubuntu-latest

env:
BASE_URL: ${{ secrets.BASE_URL}}
HOST: ${{ secrets.HOST}}
SERVICE: ${{ secrets.SERVICE}}
EMAIL: ${{ secrets.EMAIL}}
PASSWORD: ${{ secrets.PASSWORD}}
DB_TEST_URL: ${{ secrets.DB_TEST_URL }}
DEV_MODE: ${{ secrets.DEV_MODE }}
DB_HOSTED_MODE: ${{ secrets.DB_HOSTED_MODE }}
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@types/supertest": "^6.0.2",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^7.7.0",
"@typescript-eslint/parser": "^7.7.0",
"dotenv": "^16.4.5",
Expand Down
9 changes: 6 additions & 3 deletions src/__test__/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
NewUser,
user_bad_request,
} from "../mock/static";
import { Token } from "../database/models/token";

jest.setTimeout(30000);

Expand All @@ -34,6 +35,7 @@ describe("USER API TEST", () => {

afterAll(async () => {
await deleteTableData(User, "users");
await deleteTableData(Token, "tokens");
});
it("it should register a user and return 201", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
Expand Down Expand Up @@ -63,7 +65,8 @@ describe("USER API TEST", () => {

const { body } = await Jest_request.get(
`/api/v1/users/account/verify/${token}`,
).expect(200);
);
console.log(`/api/v1/users/account/verify/${token}`);

expect(body.status).toStrictEqual(200);
expect(body.message).toStrictEqual("Email verified successfull");
Expand Down Expand Up @@ -98,8 +101,8 @@ describe("USER API TEST", () => {
it("should return 404 when a user login with wrong credentials", async () => {
const { body } = await Jest_request.post("/api/v1/users/login")
.send(login_user_wrong_credentials)
.expect(404);
expect(body.status).toStrictEqual("NOT FOUND");
.expect(403);
expect(body.status).toStrictEqual("FORBIDDEN");
expect(body.message).toStrictEqual("Wrong credentials!");
});

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ const login = async (req: Request, res: Response, next: NextFunction) => {

if (info) {
return res
.status(404)
.json(new HttpException("NOT FOUND", info.message));
.status(403)
.json(new HttpException("FORBIDDEN", info.message));
}

(req as any).login(user, (err: Error) => {
Expand Down
52 changes: 52 additions & 0 deletions src/documention/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,58 @@ const users = {
responses,
},
},
"/users/account/verify/{token}": {
get: {
tags: ["User"],
summary: "Verify user account",
parameters: [
{
in: "path",
name: "token",
required: true,
type: "string",
description: "Verification token",
},
],
responses: {
"200": {
description: "Email verified successfully",
schema: {
type: "object",
properties: {
status: {
type: "integer",
example: 200,
},
message: {
type: "string",
example: "Email verified successfull",
},
},
},
},
"400": {
description: "Invalid link or something went wrong",
schema: {
type: "object",
properties: {
status: {
type: "integer",
example: 400,
},
message: {
type: "string",
example: "Invalid link",
},
error: {
type: "string",
},
},
},
},
},
},
},
};

export default users;

0 comments on commit eb6f169

Please sign in to comment.