Skip to content

Commit 22abdb7

Browse files
committed
2 parents 9075c50 + c6d2d17 commit 22abdb7

19 files changed

Lines changed: 717 additions & 16 deletions

.env.example

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,36 @@ ENV=
1515

1616
GOOSE_MIGRATION_DIR=database/schema
1717
GOOSE_DRIVER=postgres
18-
GOOSE_DBSTRING=postgres://devsoc:devsoc@localhost:5432/devsoc?sslmode=disable
18+
GOOSE_DBSTRING=postgres://devsoc:devsoc@localhost:5432/devsoc?sslmode=disable
19+
20+
SMTP_ACCOUNTS=6
21+
22+
SMTP_0_HOST=
23+
SMTP_0_PORT=
24+
SMTP_0_USER=
25+
SMTP_0_PASS=
26+
27+
SMTP_1_HOST=
28+
SMTP_1_PORT=
29+
SMTP_1_USER=
30+
SMTP_1_PASS=
31+
32+
SMTP_2_HOST=
33+
SMTP_2_PORT=
34+
SMTP_2_USER=
35+
SMTP_2_PASS=
36+
37+
SMTP_3_HOST=
38+
SMTP_3_PORT=
39+
SMTP_3_USER=
40+
SMTP_3_PASS=
41+
42+
SMTP_4_HOST=
43+
SMTP_4_PORT=
44+
SMTP_4_USER=
45+
SMTP_4_PASS=
46+
47+
SMTP_5_HOST=
48+
SMTP_5_PORT=
49+
SMTP_5_USER=
50+
SMTP_5_PASS=

database/queries/comments.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- name: CreateComment :one
2+
INSERT INTO comment (
3+
id,
4+
team_id,
5+
comment,
6+
round,
7+
created_by
8+
)
9+
VALUES ($1, $2, $3, $4, $5)
10+
RETURNING *;
11+
12+
-- name: GetCommentByRound :one
13+
SELECT *
14+
FROM comment
15+
WHERE team_id = $1 AND round = $2;
16+
17+
-- name: GetAllComments :many
18+
SELECT *
19+
FROM comment
20+
WHERE team_id = $1;
21+
22+
-- name: CommentExistsByRound :one
23+
SELECT EXISTS (
24+
SELECT 1
25+
FROM comment
26+
WHERE team_id = $1 AND round = $2
27+
);
28+
29+
-- name: UpdateComment :one
30+
UPDATE comment
31+
SET comment = $1
32+
WHERE team_id = $2 and round = $3
33+
RETURNING *;
34+
35+
-- name: DeleteComment :exec
36+
DELETE FROM comment WHERE team_id = $1 and round = $2;

database/queries/users.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ SELECT EXISTS (
2626
SELECT 1
2727
FROM users
2828
WHERE email = $1
29-
);
29+
);
30+
31+
-- name: GetUserIDByEmail :one
32+
SELECT id
33+
FROM users
34+
WHERE email = $1;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- +goose Up
2+
3+
ALTER TABLE comment
4+
ADD COLUMN round INTEGER NOT NULL DEFAULT 0;
5+
6+
-- +goose Down
7+
8+
ALTER TABLE comment
9+
DROP COLUMN round;

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ services:
2727
- .:/app
2828
env_file:
2929
- .env
30+
environment:
31+
- GOFLAGS=-buildvcs=false
3032
restart: on-failure
3133
depends_on:
3234
- postgres

pkg/controllers/auth.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"errors"
66
"time"
77

8-
"github.com/google/uuid"
9-
108
"github.com/CodeChefVIT/devsoc-backend-26/pkg/db"
119
"github.com/CodeChefVIT/devsoc-backend-26/pkg/redis"
1210
"github.com/CodeChefVIT/devsoc-backend-26/pkg/utils"
@@ -33,7 +31,11 @@ func SendOTP(email string) error {
3331
return errors.New("otp already sent")
3432
}
3533

36-
otp := utils.GenerateOTP()
34+
otp, err := utils.GenerateOTP()
35+
if err != nil {
36+
return errors.New("failed to generate otp")
37+
}
38+
3739
hashed := utils.HashOTP(otp)
3840

3941
err = redis.Client.Set(
@@ -47,26 +49,36 @@ func SendOTP(email string) error {
4749
return err
4850
}
4951

50-
println("OTP:", otp)
52+
err = utils.SendMail(email, otp)
53+
if err != nil {
54+
redis.Client.Del(redis.Ctx, key)
55+
return err
56+
}
57+
5158
return nil
5259
}
5360

54-
5561
func VerifyOTP(email, otp string) (string, error) {
5662
key := "otp:" + email
5763

5864
storedHash, err := redis.Client.Get(redis.Ctx, key).Result()
5965
if err != nil {
66+
if redis.IsKeyMissing(err) {
67+
return "", errors.New("otp expired or invalid")
68+
}
6069
return "", err
6170
}
6271

6372
if utils.HashOTP(otp) != storedHash {
64-
return "", err
73+
return "", errors.New("invalid otp")
6574
}
6675

6776
redis.Client.Del(redis.Ctx, key)
6877

69-
userID := uuid.New().String()
78+
userID, err := db.Queries.GetUserIDByEmail(context.Background(), email)
79+
if err != nil {
80+
return "", err
81+
}
7082

71-
return utils.CreateJWT(userID, email)
83+
return utils.CreateJWT(userID.String(), email)
7284
}

0 commit comments

Comments
 (0)