Passed
Push — main ( 01f0b4...eeea6b )
by Yume
03:58 queued 01:52
created

app/v2/handlers/auth.go   A

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 64
dl 0
loc 99
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handlers.NewAuthController 0 2 1
A handlers.*AuthController.PostLogout 0 13 1
A handlers.*AuthController.PostRegister 0 24 2
A handlers.*AuthController.PostLogin 0 30 2
1
package handlers
2
3
import (
4
	"context"
5
	"log/slog"
6
	"net/http"
7
	"time"
8
9
	"github.com/labstack/echo/v4"
10
	"github.com/memnix/memnix-rest/app/v2/views/components"
11
	"github.com/memnix/memnix-rest/domain"
12
	"github.com/memnix/memnix-rest/services/auth"
13
)
14
15
type AuthController struct {
16
	useCase auth.IUseCase
17
}
18
19
const (
20
	// SessionTokenCookieKey is the key for the session token cookie.
21
	SessionTokenCookieKey = "session_token"
22
	ExpiresDuration       = 24 * time.Hour
23
)
24
25
func NewAuthController(auth auth.IUseCase) AuthController {
26
	return AuthController{useCase: auth}
27
}
28
29
func (a *AuthController) PostLogin(c echo.Context) error {
30
	// Get the username and password from the request
31
	email := c.FormValue("email")
32
	password := c.FormValue("password")
33
34
	slog.Debug("Auth: ", slog.String("email", email), slog.String("password", password))
35
36
	// Call the use case to authenticate the user
37
	jwtToken, err := a.useCase.Login(context.Background(), password, email)
38
	if err != nil {
39
		setFlashmessages(c, "error", "Invalid email or password")
40
		slog.Debug("Auth: ", slog.String("error", err.Error()))
41
42
		return Redirect(c, "/login", http.StatusForbidden)
43
	}
44
45
	cookie := &http.Cookie{
46
		Name:     SessionTokenCookieKey,
47
		Value:    jwtToken,
48
		Path:     "/",
49
		Expires:  time.Now().Add(ExpiresDuration),
50
		HttpOnly: true,
51
		Secure:   true,
52
		SameSite: http.SameSiteLaxMode,
53
	}
54
	c.SetCookie(cookie)
55
56
	setFlashmessages(c, "success", "You are now logged in")
57
58
	return Redirect(c, "/", http.StatusAccepted)
59
}
60
61
func (a *AuthController) PostLogout(c echo.Context) error {
62
	cookie := &http.Cookie{
63
		Name:     SessionTokenCookieKey,
64
		Value:    "",
65
		Path:     "/",
66
		Expires:  time.Now().Add(-1 * time.Hour),
67
		HttpOnly: true,
68
		Secure:   true,
69
		SameSite: http.SameSiteLaxMode,
70
	}
71
	c.SetCookie(cookie)
72
	c.Response().Header().Set("HX-Redirect", "/login")
73
	return c.NoContent(http.StatusAccepted)
74
}
75
76
func (a *AuthController) PostRegister(c echo.Context) error {
77
	// Get the username and password from the request
78
	email := c.FormValue("email")
79
	password := c.FormValue("password")
80
	username := c.FormValue("username")
81
82
	slog.Debug("Auth: ", slog.String("email", email), slog.String("username", username))
83
84
	registerStruct := domain.Register{
85
		Email:    email,
86
		Password: password,
87
		Username: username,
88
	}
89
90
	// Call the use case to authenticate the user
91
	_, err := a.useCase.Register(c.Request().Context(), registerStruct)
92
	if err != nil {
93
		loginError := components.RegisterError("Invalid email or password")
94
		slog.Info("Auth: ", slog.String("error", err.Error()))
95
		return Render(c, http.StatusForbidden, loginError)
96
	}
97
98
	c.Response().Header().Set("HX-Redirect", "/login")
99
	return c.NoContent(http.StatusAccepted)
100
}
101