feat: added refresh tokens

This commit is contained in:
2026-08-03 11:41:03 +02:00
parent 3b4a6cba82
commit 8fefc5d5c7
5 changed files with 172 additions and 19 deletions
+36 -3
View File
@@ -25,7 +25,7 @@ func NewAuthHandler(authService *domain.AuthService) *AuthHandler {
// @Accept json
// @Produce json
// @Param payload body request.LoginPayload true "Login payload"
// @Success 200 {object} domain.AuthToken
// @Success 200 {object} domain.TokenPair
// @Error 400 {object} response.ErrorResponse "failed to decode request body"
// @Error 500 {object} response.ErrorResponse "failed to login"
// @Router /api/v1/login [post]
@@ -39,12 +39,45 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
return
}
token, err := h.AuthService.Login(ctx, payload.Email, payload.Password)
tokens, err := h.AuthService.Login(ctx, payload.Email, payload.Password)
if err != nil {
slog.ErrorContext(ctx, "failed to login", slog.Any("error", err))
response.Error(ctx, w, http.StatusInternalServerError, "failed to login")
return
}
response.JSON(ctx, w, http.StatusOK, token)
response.JSON(ctx, w, http.StatusOK, tokens)
}
// Refresh handles refreshing an access token
//
// @Summary Refresh route
// @Description Token issuing with refresh token
// @Tags Authorization
// @Accept json
// @Produce json
// @Param payload body request.RefreshPayload true "Refresh payload"
// @Success 200 {object} domain.TokenPair
// @Error 400 {object} response.ErrorResponse "failed to decode request body"
// @Error 500 {object} response.ErrorResponse "failed to refresh token"
// @Router /api/v1/refresh [post]
func (h *AuthHandler) Refresh(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
payload, err := request.DecodeRefresh(r)
if err != nil {
slog.ErrorContext(ctx, "failed to decode refresh payload", slog.Any("error", err))
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body")
return
}
tokens, err := h.AuthService.Refresh(ctx, payload.RefreshToken)
if err != nil {
slog.ErrorContext(ctx, "failed to refresh token", slog.Any("error", err))
// TODO: Add error definition to repository, respond with Unauthorized when token is invalid
response.Error(ctx, w, http.StatusInternalServerError, "failed to refresh token")
return
}
response.JSON(ctx, w, http.StatusOK, tokens)
}