Skip to content

Commit 51abfd9

Browse files
authored
Merge pull request #24 from JothishKamal/master
feat: get user by uuid for refresh token + update .env.example
2 parents fe6f253 + b7ef276 commit 51abfd9

File tree

7 files changed

+59
-25
lines changed

7 files changed

+59
-25
lines changed

.env.example

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ POSTGRES_USER=huehuehue
88
POSTGRES_PASSWORD=huehuehue
99
POSTGRES_DB=huehuehue
1010

11-
DRAGONFLY_HOST=redis
12-
DRAGONFLY_PORT=6379
13-
DRAGONFLY_PASSWORD=huehuehue
11+
REDIS_HOST=redis
12+
REDIS_PORT=6379
13+
REDIS_PASSWORD=huehuehue
1414

15-
JUDGE0_URI=https://google.com
15+
JUDGE0_URI=https://google.com
16+
17+
JWT_KEY=secret

database/queries/user.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ WHERE email = $1;
99
-- name: GetUserByUsername :one
1010
SELECT id, email, "regNo", password, role, "roundQualified", "score", name
1111
FROM "user"
12-
WHERE name = $1;
12+
WHERE name = $1;
13+
-- name: GetUserById :one
14+
SELECT id, email, "regNo", password, role, "roundQualified", "score", name
15+
FROM "user"
16+
WHERE id = $1;

go.mod

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ go 1.22
44

55
require (
66
github.com/go-chi/chi/v5 v5.1.0
7-
github.com/golang-jwt/jwt/v5 v5.2.1
7+
github.com/go-chi/jwtauth/v5 v5.3.1
8+
github.com/go-chi/render v1.0.3
9+
github.com/golang-jwt/jwt/v4 v4.5.0
810
github.com/google/uuid v1.6.0
911
github.com/jackc/pgx/v5 v5.6.0
1012
github.com/joho/godotenv v1.5.1
@@ -17,10 +19,7 @@ require (
1719
github.com/cespare/xxhash/v2 v2.2.0 // indirect
1820
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
1921
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
20-
github.com/go-chi/jwtauth/v5 v5.3.1 // indirect
2122
github.com/goccy/go-json v0.10.2 // indirect
22-
github.com/go-chi/render v1.0.3 // indirect
23-
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
2423
github.com/jackc/pgpassfile v1.0.0 // indirect
2524
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
2625
github.com/jackc/puddle/v2 v2.2.1 // indirect
@@ -31,7 +30,6 @@ require (
3130
github.com/lestrrat-go/jwx/v2 v2.0.20 // indirect
3231
github.com/lestrrat-go/option v1.0.1 // indirect
3332
github.com/segmentio/asm v1.2.0 // indirect
34-
github.com/stretchr/testify v1.8.4 // indirect
3533
go.uber.org/multierr v1.10.0 // indirect
3634
golang.org/x/crypto v0.21.0 // indirect
3735
golang.org/x/sync v0.1.0 // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
1717
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
1818
github.com/go-chi/jwtauth/v5 v5.3.1 h1:1ePWrjVctvp1tyBq5b/2ER8Th/+RbYc7x4qNsc5rh5A=
1919
github.com/go-chi/jwtauth/v5 v5.3.1/go.mod h1:6Fl2RRmWXs3tJYE1IQGX81FsPoGqDwq9c15j52R5q80=
20-
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
21-
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
22-
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
23-
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
2420
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
2521
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
22+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
23+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
2624
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
2725
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
2826
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=

internal/controllers/refresh_token.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
99
logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging"
1010
"github.com/go-chi/jwtauth/v5"
11+
"github.com/google/uuid"
1112
)
1213

1314
func RefreshTokenHandler(w http.ResponseWriter, r *http.Request) {
@@ -25,23 +26,30 @@ func RefreshTokenHandler(w http.ResponseWriter, r *http.Request) {
2526
return
2627
}
2728

28-
userName, ok := claims.PrivateClaims()["username"].(string)
29+
userId, ok := claims.PrivateClaims()["user_id"].(string)
2930
if !ok {
30-
logger.Errof("Invalid token claims, email not found")
31+
logger.Errof("Invalid token claims, user_id not found")
3132
httphelpers.WriteError(w, http.StatusUnauthorized, "invalid token claims")
3233
return
3334
}
3435

35-
user, err := database.Queries.GetUserByUsername(r.Context(), userName)
36+
userIdUUID, err := uuid.Parse(userId)
3637
if err != nil {
37-
logger.Errof("User not found: %s, err: %v", userName, err)
38+
logger.Errof("Invalid user_id: %s, err: %v", userId, err)
39+
httphelpers.WriteError(w, http.StatusUnauthorized, "invalid user_id")
40+
return
41+
}
42+
43+
user, err := database.Queries.GetUserById(r.Context(), userIdUUID)
44+
if err != nil {
45+
logger.Errof("User not found: %s, err: %v", user.Name, err)
3846
httphelpers.WriteError(w, http.StatusUnauthorized, "user not found")
3947
return
4048
}
4149

4250
accessToken, accessExp, err := helpers.GenerateJWT(&user, false)
4351
if err != nil {
44-
logger.Errof("Failed to generate new access token for user: %s, err: %v", userName, err)
52+
logger.Errof("Failed to generate new access token for user: %s, err: %v", user.Name, err)
4553
httphelpers.WriteError(w, http.StatusUnauthorized, "failed to generate token")
4654
return
4755
}
@@ -57,7 +65,7 @@ func RefreshTokenHandler(w http.ResponseWriter, r *http.Request) {
5765

5866
refreshToken, refreshExp, err := helpers.GenerateJWT(&user, true)
5967
if err != nil {
60-
logger.Errof("Failed to generate new refresh token for user: %s, err: %v", userName, err)
68+
logger.Errof("Failed to generate new refresh token for user: %s, err: %v", user.Name, err)
6169
httphelpers.WriteError(w, http.StatusUnauthorized, "failed to generate token")
6270
return
6371
}

internal/db/user.sql.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/helpers/auth/jwt.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func GenerateJWT(user *db.User, isRefresh bool) (string, time.Time, error) {
2323
var expirationTime time.Time
2424

2525
if !isRefresh {
26-
expirationTime = time.Now().Add(time.Hour / 2)
26+
expirationTime = time.Now().Add(time.Hour / 4)
2727
_, tokenString, err := TokenAuth.Encode(map[string]interface{}{
2828
"username": user.Name,
2929
"role": user.Role,
@@ -33,11 +33,11 @@ func GenerateJWT(user *db.User, isRefresh bool) (string, time.Time, error) {
3333
return tokenString, expirationTime, err
3434
}
3535

36-
expirationTime = time.Now().Add(time.Hour * 2)
36+
expirationTime = time.Now().Add(time.Hour*1 + time.Minute*30)
3737
_, tokenString, err := TokenAuth.Encode(map[string]interface{}{
38-
"username": user.Name,
39-
"type": "refresh",
40-
"exp": expirationTime.Unix(),
38+
"user_id": user.ID,
39+
"type": "refresh",
40+
"exp": expirationTime.Unix(),
4141
})
4242
return tokenString, expirationTime, err
4343
}

0 commit comments

Comments
 (0)