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 e026e5b
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 8 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
11 changes: 7 additions & 4 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 All @@ -43,7 +45,8 @@ describe("USER API TEST", () => {
expect(body.message).toStrictEqual(
"Account Created successfully, Plase Verify your Account",
);
token = body.token;
const tokenRecord = await Token.findOne();
token = tokenRecord?.dataValues.token ?? "";
});
it("it should return a user not found and status 400", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
Expand All @@ -63,7 +66,7 @@ describe("USER API TEST", () => {

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

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
7 changes: 4 additions & 3 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const registerUser = async (
"SUCCESS",
"Account Created successfully, Plase Verify your Account",
).response();
res.status(201).json({ ...response, token });

res.status(201).json({ ...response });
});
},
)(req, res, next);
Expand All @@ -62,8 +63,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
61 changes: 61 additions & 0 deletions src/documention/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,67 @@ 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",
},
},
},
},
},
},
},
"/users/logout": {
post: {
tags: ["User"],
security: [{ JWT: [] }],
summary: "Log out a user",
consumes: ["application/json"],
responses,
},
},
};

export default users;

0 comments on commit e026e5b

Please sign in to comment.