Skip to content

Commit

Permalink
adding some helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
makhlouf1102 committed Jan 7, 2025
1 parent 49810e5 commit a9e4a55
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
27 changes: 4 additions & 23 deletions handlers/auth/auth.go → handlers/auth/authHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"lets-go/libs/bcrypt"
commonerrors "lets-go/libs/commonErrors"
"lets-go/libs/env"
localconstants "lets-go/libs/localConstants"
loglib "lets-go/libs/logLib"
role_model "lets-go/models/role"
Expand Down Expand Up @@ -125,36 +124,18 @@ func Login(w http.ResponseWriter, r *http.Request) {
return
}

cookieMaxAge := 3600 * 24 * 7
expiresIn := 3600 * 24 * 7

refreshCookie := http.Cookie{
Name: env.Get("REFRESH_HTTP_COOKIE_NAME"),
Value: tokens.refreshToken,
Path: "/",
MaxAge: cookieMaxAge,
HttpOnly: true,
Secure: false,
SameSite: http.SameSiteLaxMode,
}

isAuthCookie := http.Cookie{
Name: "has-refresh-token",
Value: "true",
Path: "/",
MaxAge: cookieMaxAge,
HttpOnly: false,
Secure: false,
SameSite: http.SameSiteLaxMode,
}
cookies := generateCookies(*tokens, expiresIn)

response := localTypes.LoginResponseData{
Status: "success",
Message: "User successfully logged in",
AccessToken: tokens.accessToken,
Data: user,
}
http.SetCookie(w, &refreshCookie)
http.SetCookie(w, &isAuthCookie)
http.SetCookie(w, &cookies.RefreshCookie)
http.SetCookie(w, &cookies.IsAuthCookie)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

Expand Down
14 changes: 14 additions & 0 deletions handlers/auth/authHandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// package authHandler_test

// import (
// "bytes"
// "io"
// "net/http"
// "net/http/httptest"
// "testing"
// )

// func TestRegistrationValid(t *testing.T) {

// req := httptest.NewRequest(http.MethodGet, "/api/v1/auth/register", )
// }

Check failure on line 14 in handlers/auth/authHandler_test.go

View workflow job for this annotation

GitHub Actions / build

expected 'package', found 'EOF'
34 changes: 34 additions & 0 deletions handlers/auth/helperFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"errors"
"fmt"
"lets-go/libs/bcrypt"
"lets-go/libs/env"
loglib "lets-go/libs/logLib"
"lets-go/libs/token"
userModel "lets-go/models/user"
localTypes "lets-go/types"
"net/http"
"reflect"
)

Expand Down Expand Up @@ -79,3 +81,35 @@ func generateTokens(userID string, listRoles []string) (*Tokens, error) {

return &Tokens{accessToken, refreshToken}, nil
}

type LocalCookies struct {
RefreshCookie http.Cookie
IsAuthCookie http.Cookie
}

func generateCookies(tokens Tokens, expiresIn int) *LocalCookies {
refreshCookie := http.Cookie{
Name: env.Get("REFRESH_HTTP_COOKIE_NAME"),
Value: tokens.refreshToken,
Path: "/",
MaxAge: expiresIn,
HttpOnly: true,
Secure: false,
SameSite: http.SameSiteLaxMode,
}

isAuthCookie := http.Cookie{
Name: "has-refresh-token",
Value: "true",
Path: "/",
MaxAge: expiresIn,
HttpOnly: false,
Secure: false,
SameSite: http.SameSiteLaxMode,
}

return &LocalCookies{
RefreshCookie: refreshCookie,
IsAuthCookie: isAuthCookie,
}
}
1 change: 0 additions & 1 deletion tmp/build-errors.log

This file was deleted.

0 comments on commit a9e4a55

Please sign in to comment.