Add auth login and refresh handlers
This commit is contained in:
parent
199fd3a93a
commit
03bce210ac
3 changed files with 81 additions and 60 deletions
80
backend-old/internal/http/handler/auth_handler.go
Normal file
80
backend-old/internal/http/handler/auth_handler.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Login godoc
|
||||
// @Summary Autenticação de usuário
|
||||
// @Description Realiza login e retorna token JWT.
|
||||
// @Description **Credenciais Padrão (Master):**
|
||||
// @Description Email: `andre.fr93@gmail.com`
|
||||
// @Description Senha: `teste1234`
|
||||
// @Tags Autenticação
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param login body loginRequest true "Credenciais"
|
||||
// @Success 200 {object} authResponse
|
||||
// @Failure 401 {object} map[string]string
|
||||
// @Router /api/v1/auth/login [post]
|
||||
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
var req loginRequest
|
||||
if err := decodeJSON(r.Context(), r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Username == "" {
|
||||
writeError(w, http.StatusBadRequest, errors.New("username is required"))
|
||||
return
|
||||
}
|
||||
|
||||
token, exp, err := h.svc.Login(r.Context(), req.Username, req.Password)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, authResponse{Token: token, ExpiresAt: exp})
|
||||
}
|
||||
|
||||
// Refresh godoc
|
||||
// @Summary Atualizar token
|
||||
// @Description Gera um novo JWT a partir de um token válido.
|
||||
// @Tags Autenticação
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "Bearer token"
|
||||
// @Success 200 {object} authResponse
|
||||
// @Failure 401 {object} map[string]string
|
||||
// @Router /api/v1/auth/refresh [post]
|
||||
func (h *Handler) Refresh(w http.ResponseWriter, r *http.Request) {
|
||||
h.RefreshToken(w, r)
|
||||
}
|
||||
|
||||
// RefreshToken godoc
|
||||
// @Summary Atualizar token
|
||||
// @Description Gera um novo JWT a partir de um token válido.
|
||||
// @Tags Autenticação
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "Bearer token"
|
||||
// @Success 200 {object} authResponse
|
||||
// @Failure 401 {object} map[string]string
|
||||
// @Router /api/v1/auth/refresh-token [post]
|
||||
func (h *Handler) RefreshToken(w http.ResponseWriter, r *http.Request) {
|
||||
tokenStr, err := parseBearerToken(r)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
|
||||
token, exp, err := h.svc.RefreshToken(r.Context(), tokenStr)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, authResponse{Token: token, ExpiresAt: exp})
|
||||
}
|
||||
|
|
@ -103,40 +103,6 @@ func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
|
|||
writeJSON(w, http.StatusCreated, authResponse{Token: token, ExpiresAt: exp})
|
||||
}
|
||||
|
||||
// Login godoc
|
||||
// @Summary Autenticação de usuário
|
||||
// @Description Realiza login e retorna token JWT.
|
||||
// @Description **Credenciais Padrão (Master):**
|
||||
// @Description Email: `andre.fr93@gmail.com`
|
||||
// @Description Senha: `teste1234`
|
||||
// @Tags Autenticação
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param login body loginRequest true "Credenciais"
|
||||
// @Success 200 {object} authResponse
|
||||
// @Failure 401 {object} map[string]string
|
||||
// @Router /api/v1/auth/login [post]
|
||||
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
var req loginRequest
|
||||
if err := decodeJSON(r.Context(), r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Username == "" {
|
||||
writeError(w, http.StatusBadRequest, errors.New("username is required"))
|
||||
return
|
||||
}
|
||||
|
||||
token, exp, err := h.svc.Login(r.Context(), req.Username, req.Password)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, authResponse{Token: token, ExpiresAt: exp})
|
||||
}
|
||||
|
||||
// GetMe godoc
|
||||
// @Summary Obter dados do usuário logado
|
||||
// @Tags Autenticação
|
||||
|
|
@ -229,32 +195,6 @@ func (h *Handler) RegisterTenant(w http.ResponseWriter, r *http.Request) {
|
|||
h.registerWithPayload(w, r, req)
|
||||
}
|
||||
|
||||
// RefreshToken godoc
|
||||
// @Summary Atualizar token
|
||||
// @Description Gera um novo JWT a partir de um token válido.
|
||||
// @Tags Autenticação
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "Bearer token"
|
||||
// @Success 200 {object} authResponse
|
||||
// @Failure 401 {object} map[string]string
|
||||
// @Router /api/v1/auth/refresh-token [post]
|
||||
func (h *Handler) RefreshToken(w http.ResponseWriter, r *http.Request) {
|
||||
tokenStr, err := parseBearerToken(r)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
|
||||
token, exp, err := h.svc.RefreshToken(r.Context(), tokenStr)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, authResponse{Token: token, ExpiresAt: exp})
|
||||
}
|
||||
|
||||
// Logout godoc
|
||||
// @Summary Logout
|
||||
// @Description Endpoint para logout (invalidação client-side).
|
||||
|
|
|
|||
|
|
@ -160,6 +160,7 @@ func New(cfg config.Config) (*Server, error) {
|
|||
mux.Handle("POST /api/v1/auth/logout", chain(http.HandlerFunc(h.Logout), middleware.Logger, middleware.Gzip))
|
||||
mux.Handle("POST /api/v1/auth/password/forgot", chain(http.HandlerFunc(h.ForgotPassword), middleware.Logger, middleware.Gzip))
|
||||
mux.Handle("POST /api/v1/auth/password/reset", chain(http.HandlerFunc(h.ResetPassword), middleware.Logger, middleware.Gzip))
|
||||
mux.Handle("POST /api/v1/auth/refresh", chain(http.HandlerFunc(h.Refresh), middleware.Logger, middleware.Gzip))
|
||||
mux.Handle("POST /api/v1/auth/refresh-token", chain(http.HandlerFunc(h.RefreshToken), middleware.Logger, middleware.Gzip))
|
||||
mux.Handle("POST /api/v1/auth/verify-email", chain(http.HandlerFunc(h.VerifyEmail), middleware.Logger, middleware.Gzip))
|
||||
// Address
|
||||
|
|
|
|||
Loading…
Reference in a new issue