Skip to content

Commit b63ea56

Browse files
authored
Merge pull request #38 from Xenomorph07/master
i have done smth
2 parents 8dd9704 + 98e15fd commit b63ea56

File tree

6 files changed

+165
-11
lines changed

6 files changed

+165
-11
lines changed

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ REDIS_PASSWORD=huehuehue
1515
JUDGE0_URI=https://google.com
1616

1717
JWT_KEY=secret
18-
CALLBACK_URL=https://google.com/callback
18+
CALLBACK_URL=https://google.com/callback
19+
20+
SECRET_KEY_FUCKERS=

database/queries/user.sql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
-- :name CreateUser :one
2-
INSERT INTO users (id, submissions, email, reg_no, password, role, round_qualified, score, name)
3-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
1+
-- name: CreateUser :one
2+
INSERT INTO users (id, email, reg_no, password, role, round_qualified, score, name)
3+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
44
RETURNING *;
5+
56
-- name: GetUserByEmail :one
67
SELECT id, email, reg_no, password, role, round_qualified, score, name
78
FROM users
89
WHERE email = $1;
10+
911
-- name: GetUserByUsername :one
1012
SELECT id, email, reg_no, password, role, round_qualified, score, name
1113
FROM users
1214
WHERE name = $1;
15+
1316
-- name: GetUserById :one
1417
SELECT id, email, reg_no, password, role, round_qualified, score, name
1518
FROM users

internal/controllers/auth.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ package controllers
33
import (
44
"errors"
55
"net/http"
6+
"os"
67

8+
"github.com/CodeChefVIT/cookoff-backend/internal/db"
9+
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/auth"
710
helpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/auth"
811
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
912
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
1013
logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging"
14+
"github.com/google/uuid"
15+
"github.com/jackc/pgx/v5/pgtype"
1116
"golang.org/x/crypto/bcrypt"
1217
)
1318

@@ -16,6 +21,61 @@ type LoginRequest struct {
1621
Password string `json:"password"`
1722
}
1823

24+
type SignupRequest struct {
25+
Email string `json:"email"`
26+
Name string `json:"name"`
27+
RegNo string `json:"reg_no"`
28+
Key string `json:"fuck_you"`
29+
}
30+
31+
func SignUp(w http.ResponseWriter, r *http.Request) {
32+
var payload SignupRequest
33+
34+
if err := httphelpers.ParseJSON(r, &payload); err != nil {
35+
httphelpers.WriteError(w, http.StatusBadRequest, err.Error())
36+
return
37+
}
38+
39+
if payload.Key != os.Getenv("SECRET_KEY_FUCKERS") {
40+
httphelpers.WriteError(w, http.StatusUnauthorized, "I WILL POP YOUR CHERRY BRO")
41+
return
42+
}
43+
44+
passwd := auth.PasswordGenerator(6)
45+
hashed, err := bcrypt.GenerateFromPassword([]byte(passwd), 10)
46+
if err != nil {
47+
httphelpers.WriteError(w, http.StatusInternalServerError, err.Error())
48+
return
49+
}
50+
51+
id, err := uuid.NewV7()
52+
if err != nil {
53+
httphelpers.WriteError(w, http.StatusInternalServerError, err.Error())
54+
return
55+
}
56+
57+
_, err = database.Queries.CreateUser(r.Context(), db.CreateUserParams{
58+
ID: id,
59+
Email: payload.Email,
60+
RegNo: payload.RegNo,
61+
Password: string(hashed),
62+
Role: "user",
63+
RoundQualified: 0,
64+
Score: pgtype.Int4{},
65+
Name: payload.Name,
66+
})
67+
if err != nil {
68+
httphelpers.WriteError(w, http.StatusInternalServerError, err.Error())
69+
return
70+
}
71+
72+
httphelpers.WriteJSON(w, http.StatusOK, map[string]any{
73+
"message": "user added",
74+
"email": payload.Email,
75+
"password": passwd,
76+
})
77+
}
78+
1979
func LoginHandler(w http.ResponseWriter, r *http.Request) {
2080
var req LoginRequest
2181

internal/db/user.sql.go

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package auth
2+
3+
import (
4+
"math/rand"
5+
"time"
6+
)
7+
8+
func PasswordGenerator(passwordLength int) string {
9+
lowerCase := "abcdefghijklmnopqrstuvwxyz"
10+
upperCase := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
11+
numbers := "0123456789"
12+
specialChar := "!@#$%^&*()_-+={}[/?]"
13+
14+
password := ""
15+
16+
source := rand.NewSource(time.Now().UnixNano())
17+
rng := rand.New(source)
18+
19+
for n := 0; n < passwordLength; n++ {
20+
randNum := rng.Intn(4)
21+
22+
switch randNum {
23+
case 0:
24+
randCharNum := rng.Intn(len(lowerCase))
25+
password += string(lowerCase[randCharNum])
26+
case 1:
27+
randCharNum := rng.Intn(len(upperCase))
28+
password += string(upperCase[randCharNum])
29+
case 2:
30+
randCharNum := rng.Intn(len(numbers))
31+
password += string(numbers[randCharNum])
32+
case 3:
33+
randCharNum := rng.Intn(len(specialChar))
34+
password += string(specialChar[randCharNum])
35+
}
36+
}
37+
38+
return password
39+
}

internal/server/routes.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,29 @@ func (s *Server) RegisterRoutes(taskClient *asynq.Client) http.Handler {
2020
r.Put("/callback", func(w http.ResponseWriter, r *http.Request) {
2121
controllers.CallbackUrl(w, r, taskClient)
2222
})
23-
23+
24+
r.Post("/user/signup", controllers.SignUp)
25+
2426
r.Post("/login/user", controllers.LoginHandler)
2527
r.Post("/token/refresh", controllers.RefreshTokenHandler)
2628
r.Group(func(protected chi.Router) {
2729
protected.Use(jwtauth.Verifier(auth.TokenAuth))
2830
protected.Use(jwtauth.Authenticator(auth.TokenAuth))
29-
31+
3032
protected.Get("/protected", controllers.ProtectedHandler)
3133
protected.Post("/submit", controllers.SubmitCode)
3234
protected.Post("/runcode", controllers.RunCode)
33-
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).Post("/question/create", controllers.CreateQuestion)
34-
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).Get("/questions", controllers.GetAllQuestion)
35+
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).
36+
Post("/question/create", controllers.CreateQuestion)
37+
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).
38+
Get("/questions", controllers.GetAllQuestion)
3539
protected.Get("/question/round", controllers.GetQuestionsByRound)
36-
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).Get("/question/{question_id}", controllers.GetQuestionById)
37-
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).Delete("/question/{question_id}", controllers.DeleteQuestion)
38-
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).Patch("/question", controllers.UpdateQuestion)
40+
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).
41+
Get("/question/{question_id}", controllers.GetQuestionById)
42+
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).
43+
Delete("/question/{question_id}", controllers.DeleteQuestion)
44+
protected.With(middlewares.RoleAuthorizationMiddleware("admin")).
45+
Patch("/question", controllers.UpdateQuestion)
3946
})
4047

4148
return r

0 commit comments

Comments
 (0)