get current session info
This commit is contained in:
parent
476e5edf38
commit
c59fbc0864
6 changed files with 84 additions and 26 deletions
|
|
@ -23,7 +23,7 @@ func newController(service *service, utils interfaces.Utils) *controller {
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc) {
|
||||
func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, refreshMW gin.HandlerFunc) {
|
||||
userGroup := r.Group("/user")
|
||||
|
||||
userGroup.POST("/", h.controller.register)
|
||||
|
|
@ -36,8 +36,9 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc) {
|
|||
authGroup := r.Group("/user/auth")
|
||||
|
||||
authGroup.POST("/login", h.controller.login)
|
||||
authGroup.POST("/logout", h.controller.logout)
|
||||
authGroup.POST("/refresh", h.controller.refresh)
|
||||
authGroup.POST("/logout", refreshMW, h.controller.logout)
|
||||
authGroup.POST("/refresh", refreshMW, h.controller.refresh)
|
||||
authGroup.GET("/current-session", authMW, refreshMW, h.controller.getCurrentSession)
|
||||
}
|
||||
|
||||
// @Summary Регистрация нового пользователя
|
||||
|
|
@ -200,15 +201,13 @@ func (co *controller) login(c *gin.Context) {
|
|||
// @Failure 500 {object} responses.ErrorResponse500
|
||||
// @Router /user/auth/logout [post]
|
||||
func (co *controller) logout(c *gin.Context) {
|
||||
cookie, err := c.Request.Cookie("refresh_uuid")
|
||||
refreshUuid, err := co.utils.GetRefreshUuidFromContext(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
|
||||
log.WithError(err).Error("User | Failed to get refresh cookie")
|
||||
log.WithError(err).Error("User | Failed to get refresh uuid from context on logout")
|
||||
return
|
||||
}
|
||||
|
||||
refreshUuid := cookie.Value
|
||||
|
||||
if err = co.service.logout(refreshUuid); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||||
log.WithError(err).Error("User | Failed to logout")
|
||||
|
|
@ -226,15 +225,13 @@ func (co *controller) logout(c *gin.Context) {
|
|||
// @Failure 500 {object} responses.ErrorResponse500
|
||||
// @Router /user/auth/refresh [post]
|
||||
func (co *controller) refresh(c *gin.Context) {
|
||||
cookie, err := c.Request.Cookie("refresh_uuid")
|
||||
refreshUuid, err := co.utils.GetRefreshUuidFromContext(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
|
||||
log.WithError(err).Error("User | Failed to get refresh cookie")
|
||||
log.WithError(err).Error("User | Failed to get refresh uuid from context on refresh")
|
||||
return
|
||||
}
|
||||
|
||||
refreshUuid := cookie.Value
|
||||
|
||||
response, err := co.service.refresh(refreshUuid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||||
|
|
@ -254,3 +251,30 @@ func (co *controller) refresh(c *gin.Context) {
|
|||
|
||||
c.JSON(http.StatusOK, LoginResponse{AccessToken: response.AccessToken})
|
||||
}
|
||||
|
||||
// @Summary Возвращает информацию о текущей сессии пользователя
|
||||
// @Description Возвращает информацию о текущей сессии пользователя
|
||||
// @Tags Users
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {object} shared.CurrentSession
|
||||
// @Failure 400 {object} responses.ErrorResponse400
|
||||
// @Failure 401 {object} responses.ErrorResponse401
|
||||
// @Failure 500 {object} responses.ErrorResponse500
|
||||
// @Router /user/auth/current-session [get]
|
||||
func (co *controller) getCurrentSession(c *gin.Context) {
|
||||
refreshUuid, err := co.utils.GetRefreshUuidFromContext(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
|
||||
log.WithError(err).Error("User | Failed to get refresh uuid from context on refresh")
|
||||
return
|
||||
}
|
||||
|
||||
response, err := co.service.getCurrentSession(refreshUuid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||||
log.WithError(err).Error("User | Failed to get user info")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -127,3 +127,7 @@ func (s *service) logout(refreshUuid string) error {
|
|||
func (s *service) refresh(refreshUuid string) (shared.AuthData, error) {
|
||||
return s.auth.Refresh(refreshUuid)
|
||||
}
|
||||
|
||||
func (s *service) getCurrentSession(refreshUuid string) (shared.CurrentSession, error) {
|
||||
return s.auth.GetCurrentSession(refreshUuid)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue