Skip to content

Commit

Permalink
add zustand and integrate with cookies to store user profile data, an…
Browse files Browse the repository at this point in the history
…d authenticated layout
  • Loading branch information
Sudarsh1010 committed Nov 15, 2024
1 parent 342d989 commit c5ed242
Show file tree
Hide file tree
Showing 19 changed files with 485 additions and 229 deletions.
10 changes: 6 additions & 4 deletions internal/controllers/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers
import (
"errors"
"fmt"

"keizer-auth/internal/models"
"keizer-auth/internal/services"
"keizer-auth/internal/utils"
Expand Down Expand Up @@ -65,7 +64,7 @@ func (ac *AuthController) SignIn(c *fiber.Ctx) error {
}
if !isValid {
return c.
Status(fiber.StatusUnauthorized).
Status(fiber.StatusBadRequest).
JSON(fiber.Map{"error": "Invalid email or password. Please try again."})
}

Expand All @@ -76,8 +75,9 @@ func (ac *AuthController) SignIn(c *fiber.Ctx) error {
JSON(fiber.Map{"error": "Something went wrong, Failed to create session"})
}

fmt.Print(sessionId)
utils.SetSessionCookie(c, sessionId)
return c.JSON(fiber.Map{"message": "signed in successfully"})
return c.JSON(user)
}

func (ac *AuthController) SignUp(c *fiber.Ctx) error {
Expand Down Expand Up @@ -149,11 +149,13 @@ func (ac *AuthController) VerifyOTP(c *fiber.Ctx) error {
}

utils.SetSessionCookie(c, sessionID)
return c.JSON(fiber.Map{"message": "OTP Verified!"})
return c.JSON(user)
}

func (ac *AuthController) VerifyTokenHandler(c *fiber.Ctx) error {
sessionID := utils.GetSessionCookie(c)
fmt.Print("\n")
fmt.Print(sessionID)
if sessionID == "" {
return c.
Status(fiber.StatusUnauthorized).
Expand Down
2 changes: 1 addition & 1 deletion internal/models/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Base struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `sql:"index" json:"deleted_at"`
ID uuid.UUID `gorm:"type:uuid"`
ID uuid.UUID `json:"id" gorm:"type:uuid"`
}

func (base *Base) BeforeCreate(tx *gorm.DB) (err error) {
Expand Down
13 changes: 4 additions & 9 deletions internal/services/session_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package services
import (
"encoding/json"
"fmt"
"time"

"keizer-auth/internal/models"
"keizer-auth/internal/repositories"
"keizer-auth/internal/utils"
"time"

"github.com/redis/go-redis/v9"
)
Expand All @@ -22,22 +21,18 @@ func NewSessionService(redisRepo *repositories.RedisRepository, userRepo *reposi
}

func (ss *SessionService) CreateSession(user *models.User) (string, error) {
sessionID, err := utils.GenerateSessionID()
if err != nil {
return "", fmt.Errorf("error in generating session %w", err)
}
sessionID := utils.GenerateSessionID()

userJson, err := json.Marshal(user)
if err != nil {
return "", fmt.Errorf("error occured %w", err)
}

err = ss.redisRepo.Set(
if err = ss.redisRepo.Set(
"dashboard-user-session-"+sessionID,
string(userJson),
utils.SessionExpiresIn,
)
if err != nil {
); err != nil {
return "", fmt.Errorf("error in setting session %w", err)
}

Expand Down
17 changes: 3 additions & 14 deletions internal/utils/session_helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"fmt"
"time"

"github.com/gofiber/fiber/v2"
Expand All @@ -10,16 +9,8 @@ import (

const SessionExpiresIn = 30 * 24 * time.Hour

func GenerateSessionID() (string, error) {
generate, err := cuid2.Init(
cuid2.WithLength(15),
)
if err != nil {
fmt.Println(err.Error())
return "", err
}

return generate(), nil
func GenerateSessionID() string {
return cuid2.Generate()
}

func SetSessionCookie(c *fiber.Ctx, sessionID string) {
Expand All @@ -28,11 +19,9 @@ func SetSessionCookie(c *fiber.Ctx, sessionID string) {
Value: sessionID,
Expires: time.Now().Add(SessionExpiresIn),
HTTPOnly: true,
Secure: true,
Secure: false,
SameSite: fiber.CookieSameSiteNoneMode,
// TODO: handle domain
Domain: "localhost",
Path: "/",
})
}

Expand Down
2 changes: 2 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"input-otp": "^1.4.1",
"js-cookie": "^3.0.5",
"lucide-react": "^0.454.0",
"next-themes": "^0.4.3",
"react": "^18.3.1",
Expand All @@ -41,6 +42,7 @@
"@eslint/js": "^9.14.0",
"@tanstack/eslint-plugin-query": "^5.59.7",
"@tanstack/router-plugin": "^1.79.0",
"@types/js-cookie": "^3.0.6",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react-swc": "^3.5.0",
Expand Down
Loading

0 comments on commit c5ed242

Please sign in to comment.