feat: added refresh tokens
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func WithJWT(authService *domain.AuthService) func(http.HandlerFunc) http.Handle
|
||||
return
|
||||
}
|
||||
|
||||
authContext, err := authService.ParseToken(ctx, parts[1])
|
||||
authContext, err := authService.ParseAccessToken(ctx, parts[1])
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "invalid token", slog.Any("error", err))
|
||||
response.Error(ctx, w, http.StatusUnauthorized, "invalid or expired token")
|
||||
|
||||
@@ -11,6 +11,10 @@ type LoginPayload struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type RefreshPayload struct {
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
func DecodeLogin(r *http.Request) (LoginPayload, error) {
|
||||
var payload LoginPayload
|
||||
|
||||
@@ -23,3 +27,16 @@ func DecodeLogin(r *http.Request) (LoginPayload, error) {
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func DecodeRefresh(r *http.Request) (RefreshPayload, error) {
|
||||
var payload RefreshPayload
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
|
||||
if err := decoder.Decode(&payload); err != nil {
|
||||
return payload, fmt.Errorf("error decoding refresh payload: %w", err)
|
||||
}
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user