feat: implement authentication service and controller for user signin, signup, logout, and token refresh
All checks were successful
Build and Release / release (push) Successful in 1m30s

This commit is contained in:
2026-05-13 16:38:23 +07:00
parent b94618c7d7
commit 033e5bf60b
2 changed files with 13 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ import (
"history-api/internal/models" "history-api/internal/models"
"history-api/internal/services" "history-api/internal/services"
"history-api/pkg/config" "history-api/pkg/config"
"history-api/pkg/constants"
"history-api/pkg/validator" "history-api/pkg/validator"
"strings" "strings"
"time" "time"
@@ -59,6 +60,7 @@ func (h *AuthController) Signin(c fiber.Ctx) error {
Message: err.Message, Message: err.Message,
}) })
} }
c.Cookie(&fiber.Cookie{ c.Cookie(&fiber.Cookie{
Name: "access_token", Name: "access_token",
Value: res.AccessToken, Value: res.AccessToken,
@@ -66,6 +68,7 @@ func (h *AuthController) Signin(c fiber.Ctx) error {
Secure: true, Secure: true,
SameSite: "None", SameSite: "None",
Path: "/", Path: "/",
Expires: time.Now().Add(constants.AccessTokenDuration),
}) })
c.Cookie(&fiber.Cookie{ c.Cookie(&fiber.Cookie{
@@ -75,6 +78,7 @@ func (h *AuthController) Signin(c fiber.Ctx) error {
Secure: true, Secure: true,
SameSite: "None", SameSite: "None",
Path: "/", Path: "/",
Expires: time.Now().Add(constants.RefreshTokenDuration),
}) })
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{ return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
@@ -121,6 +125,7 @@ func (h *AuthController) Signup(c fiber.Ctx) error {
Secure: true, Secure: true,
SameSite: "None", SameSite: "None",
Path: "/", Path: "/",
Expires: time.Now().Add(constants.AccessTokenDuration),
}) })
c.Cookie(&fiber.Cookie{ c.Cookie(&fiber.Cookie{
@@ -130,6 +135,7 @@ func (h *AuthController) Signup(c fiber.Ctx) error {
Secure: true, Secure: true,
SameSite: "None", SameSite: "None",
Path: "/", Path: "/",
Expires: time.Now().Add(constants.RefreshTokenDuration),
}) })
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{ return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
@@ -185,6 +191,7 @@ func (h *AuthController) RefreshToken(c fiber.Ctx) error {
Secure: true, Secure: true,
SameSite: "None", SameSite: "None",
Path: "/", Path: "/",
Expires: time.Now().Add(constants.AccessTokenDuration),
}) })
c.Cookie(&fiber.Cookie{ c.Cookie(&fiber.Cookie{
@@ -194,6 +201,7 @@ func (h *AuthController) RefreshToken(c fiber.Ctx) error {
Secure: true, Secure: true,
SameSite: "None", SameSite: "None",
Path: "/", Path: "/",
Expires: time.Now().Add(constants.RefreshTokenDuration),
}) })
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{ return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
@@ -426,6 +434,7 @@ func (h *AuthController) GoogleCallback(c fiber.Ctx) error {
Secure: true, Secure: true,
SameSite: "None", SameSite: "None",
Path: "/", Path: "/",
Expires: time.Now().Add(constants.AccessTokenDuration),
}) })
c.Cookie(&fiber.Cookie{ c.Cookie(&fiber.Cookie{
@@ -435,6 +444,7 @@ func (h *AuthController) GoogleCallback(c fiber.Ctx) error {
Secure: true, Secure: true,
SameSite: "None", SameSite: "None",
Path: "/", Path: "/",
Expires: time.Now().Add(constants.RefreshTokenDuration),
}) })
allowed := map[string]bool{ allowed := map[string]bool{

View File

@@ -221,9 +221,9 @@ func (a *authService) RefreshToken(ctx context.Context, id string, refreshToken
return nil, fiber.NewError(fiber.StatusNotFound, "User not found") return nil, fiber.NewError(fiber.StatusNotFound, "User not found")
} }
if user.RefreshToken != refreshToken { // if user.RefreshToken != refreshToken {
return nil, fiber.NewError(fiber.StatusUnauthorized, "Invalid or expired refresh token") // return nil, fiber.NewError(fiber.StatusUnauthorized, "Invalid or expired refresh token")
} // }
roles := models.RolesEntityToRoleConstant(user.Roles) roles := models.RolesEntityToRoleConstant(user.Roles)