Skip to content

Commit 968c4c7

Browse files
committed
chore: logic working
1 parent a1c049f commit 968c4c7

17 files changed

+411
-283
lines changed

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
. "$(dirname -- "$0")/_/husky.sh"
33

44
npm run format
5+
6+
npm run lint

curl/login.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
curl -X POST \
2+
http://localhost:3000/graphql \
3+
-H 'Content-Type: application/json' \
4+
-d '{
5+
"query": "mutation login($email: String!, $password: String!) { login(email: $email, password: $password) { success message user { id name email password updated_at created_at jwt_token_session } } }",
6+
"variables": {
7+
"email": "[email protected]",
8+
"password": "testqweBR@123"
9+
}
10+
}'

rest-client/all-todos.http

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
POST http://localhost:3000/graphql
2+
Content-Type: application/json
3+
authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIwMTc0YjYyOC1lNTEzLTRkYzEtODIwMS04NmQ3ZDdiNDYxMjMiLCJ1c2VyRW1haWwiOiJwZWRyb0BnbWFpbC5jb20iLCJpYXQiOjE3MTQyNDI0MjUsImV4cCI6MTcxNDI0NjAyNX0.gMiG9gzWVaMf52EpmgQR_5JWKH_Zz_E86ne9OyaHFIM
4+
5+
6+
{
7+
"query": "query allToDos { allToDos { success message todos { id user_id user_email title done updated_at created_at } } }"
8+
}

rest-client/delete-todo.http

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
POST http://localhost:3000/graphql
2+
Content-Type: application/json
3+
authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIwMTc0YjYyOC1lNTEzLTRkYzEtODIwMS04NmQ3ZDdiNDYxMjMiLCJ1c2VyRW1haWwiOiJwZWRyb0BnbWFpbC5jb20iLCJpYXQiOjE3MTQyNDI0MjUsImV4cCI6MTcxNDI0NjAyNX0.gMiG9gzWVaMf52EpmgQR_5JWKH_Zz_E86ne9OyaHFIM
4+
5+
6+
{
7+
"query": "mutation deleteToDo($id: String!) { deleteToDo(id: $id) { success message } }",
8+
"variables": {
9+
"id": "dda14636-f143-4793-8d03-93d880d1a2f6"
10+
}
11+
}

rest-client/get-todo-by-id.http

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
POST http://localhost:3000/graphql
2+
Content-Type: application/json
3+
authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIwMTc0YjYyOC1lNTEzLTRkYzEtODIwMS04NmQ3ZDdiNDYxMjMiLCJ1c2VyRW1haWwiOiJwZWRyb0BnbWFpbC5jb20iLCJpYXQiOjE3MTQyNDI0MjUsImV4cCI6MTcxNDI0NjAyNX0.gMiG9gzWVaMf52EpmgQR_5JWKH_Zz_E86ne9OyaHFIM
4+
5+
6+
{
7+
"query": "query getToDoById($id: String!) { getToDoById(id: $id) { success message todo { id user_id user_email title done updated_at created_at } } }",
8+
"variables": {
9+
"id": "dda14636-f143-4793-8d03-93d880d1a2f6"
10+
}
11+
}

rest-client/new-todo.http

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
POST http://localhost:3000/graphql
2+
Content-Type: application/json
3+
authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIwMTc0YjYyOC1lNTEzLTRkYzEtODIwMS04NmQ3ZDdiNDYxMjMiLCJ1c2VyRW1haWwiOiJwZWRyb0BnbWFpbC5jb20iLCJpYXQiOjE3MTQyNDI0MjUsImV4cCI6MTcxNDI0NjAyNX0.gMiG9gzWVaMf52EpmgQR_5JWKH_Zz_E86ne9OyaHFIM
4+
5+
6+
{
7+
"query": "mutation newToDo($title: String!) { newToDo(title: $title) { success message todo { id user_id user_email title done updated_at created_at } } }",
8+
"variables": {
9+
"title": "jantar a noite"
10+
}
11+
}

rest-client/todos.http

Lines changed: 0 additions & 1 deletion
This file was deleted.

rest-client/update-todo.http

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
POST http://localhost:3000/graphql
2+
Content-Type: application/json
3+
authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIwMTc0YjYyOC1lNTEzLTRkYzEtODIwMS04NmQ3ZDdiNDYxMjMiLCJ1c2VyRW1haWwiOiJwZWRyb0BnbWFpbC5jb20iLCJpYXQiOjE3MTQyNDI0MjUsImV4cCI6MTcxNDI0NjAyNX0.gMiG9gzWVaMf52EpmgQR_5JWKH_Zz_E86ne9OyaHFIM
4+
5+
6+
{
7+
"query": "mutation updateToDo($id: String!, $title: String!, $done: Boolean!) { updateToDo(id: $id, title: $title, done: $done) { success message todo { id user_id user_email title done updated_at created_at } } }",
8+
"variables": {
9+
"id": "dda14636-f143-4793-8d03-93d880d1a2f6",
10+
"title": "editando para true",
11+
"done": true
12+
}
13+
}

src/config/redis.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Redis from "ioredis";
22

33
export const redis = new Redis({
4-
port: 6379,
5-
host: "127.0.0.1",
6-
name: "default",
7-
password: "my-top-secret",
8-
db: 0,
4+
port: 6379,
5+
host: "127.0.0.1",
6+
// name: "default",
7+
// password: "my-top-secret",
8+
// db: 0,
99
});

src/controllers/Auth.controller.ts

Lines changed: 81 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,85 @@ import { LoginDTO, SignupDTO } from "src/models/auth.dto";
77
import { redis } from "src/config/redis";
88

99
export default class AuthController {
10-
static async signup(signupDTO: SignupDTO) {
11-
try {
12-
const { name, email, password } = signupDTO
13-
14-
validateSignupPayload({ name, email, password });
15-
16-
const emailAlreadyRegistred = await redis
17-
.get(`user:${email}`)
18-
.then((result) => {
19-
return JSON.parse(result);
20-
})
21-
.catch((error) => {
22-
throw new Error(error);
23-
});
24-
25-
if (emailAlreadyRegistred) return { success: false, message: "Email already registred" };
26-
27-
const user = {
28-
id: randomUUID(),
29-
name,
30-
email,
31-
password: await bcrypt.hash(password, 10),
32-
updated_at: null,
33-
created_at: new Date().toISOString(),
34-
jwt_token_session: null
35-
};
36-
37-
const jwt_token = jsonwebtoken.sign({ userId: user.id, userEmail: user.email }, process.env.JWT_SECRET_KEY, {
38-
expiresIn: "1h",
39-
});
40-
41-
user.jwt_token_session = jwt_token;
42-
43-
redis.set(`user:${user.email}`, JSON.stringify(user));
44-
45-
return { success: true, user, jwt_token };
46-
} catch (error: any) {
47-
return { success: false, message: error.message };
48-
}
49-
}
50-
51-
static async login(loginDTO: LoginDTO) {
52-
try {
53-
const { email, password } = loginDTO
54-
55-
validateLoginPayload({ email, password });
56-
57-
const user = await redis
58-
.get(`user:${email}`)
59-
.then((result) => {
60-
return JSON.parse(result);
61-
})
62-
.catch((error) => {
63-
throw new Error(error);
64-
});
65-
66-
if (!user) return { success: false, message: "Email and/or password invalid" };
67-
68-
const passwordMatch = await bcrypt.compare(password, user.password);
69-
70-
if (!passwordMatch) return { success: false, message: "Email and/or password invalid" };
71-
72-
const jwt_token = jsonwebtoken.sign(
73-
{ userId: user.id, userEmail: user.email },
74-
process.env.JWT_SECRET_KEY,
75-
{ expiresIn: "1h" },
76-
);
77-
78-
return { success: true, user, jwt_token };
79-
} catch (error: any) {
80-
return { success: false, message: error.message };
81-
}
82-
}
10+
static async signup(signupDTO: SignupDTO) {
11+
try {
12+
const { name, email, password } = signupDTO;
13+
14+
validateSignupPayload({ name, email, password });
15+
16+
const emailAlreadyRegistred = await redis
17+
.get(`user:${email}`)
18+
.then((result) => {
19+
return JSON.parse(result);
20+
})
21+
.catch((error) => {
22+
throw new Error(error);
23+
});
24+
25+
if (emailAlreadyRegistred) return { success: false, message: "Email already registred" };
26+
27+
const user = {
28+
id: randomUUID(),
29+
name,
30+
email,
31+
password: await bcrypt.hash(password, 10),
32+
updated_at: null,
33+
created_at: new Date().toISOString(),
34+
jwt_token_session: null,
35+
};
36+
37+
const jwt_token = jsonwebtoken.sign(
38+
{ userId: user.id, userEmail: user.email },
39+
process.env.JWT_SECRET_KEY,
40+
{
41+
expiresIn: "1h",
42+
},
43+
);
44+
45+
user.jwt_token_session = jwt_token;
46+
47+
redis.set(`user:${user.email}`, JSON.stringify(user));
48+
49+
return { success: true, user, jwt_token };
50+
} catch (error: any) {
51+
return { success: false, message: error.message };
52+
}
53+
}
54+
55+
static async login(loginDTO: LoginDTO) {
56+
try {
57+
const { email, password } = loginDTO;
58+
59+
validateLoginPayload({ email, password });
60+
61+
const user = await redis
62+
.get(`user:${email}`)
63+
.then((result) => {
64+
return JSON.parse(result);
65+
})
66+
.catch((error) => {
67+
throw new Error(error);
68+
});
69+
70+
if (!user) return { success: false, message: "Email and/or password invalid" };
71+
72+
const passwordMatch = await bcrypt.compare(password, user.password);
73+
74+
if (!passwordMatch) return { success: false, message: "Email and/or password invalid" };
75+
76+
const jwt_token = jsonwebtoken.sign(
77+
{ userId: user.id, userEmail: user.email },
78+
process.env.JWT_SECRET_KEY,
79+
{ expiresIn: "1h" },
80+
);
81+
82+
user.jwt_token_session = jwt_token;
83+
84+
redis.set(`user:${user.email}`, JSON.stringify(user));
85+
86+
return { success: true, user, jwt_token };
87+
} catch (error: any) {
88+
return { success: false, message: error.message };
89+
}
90+
}
8391
}

0 commit comments

Comments
 (0)