Skip to content

Commit

Permalink
Login redirect correction
Browse files Browse the repository at this point in the history
  • Loading branch information
barats committed Feb 28, 2023
1 parent 828922b commit 2500c3f
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions controller/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,49 +78,49 @@ func AdminCookieValue(user core.User) (string, error) {
// AdminAuthHandler Authorization for /admin
func AdminAuthHandler() gin.HandlerFunc {
return func(c *gin.Context) {

user, err := c.Cookie("ohUrlShortenerAdmin")
if err != nil {
c.AbortWithStatus(http.StatusUnauthorized)
// c.AbortWithError(http.StatusFound, err)
return
c.Redirect(http.StatusFound, "/login")
c.Next()
}

cookie, err := c.Cookie("ohUrlShortenerCookie")
if err != nil {
c.AbortWithStatus(http.StatusUnauthorized)
// c.Redirect(http.StatusFound, "/login")
c.Redirect(http.StatusFound, "/login")
c.Next()
return
}

if len(user) <= 0 || len(cookie) <= 0 {
c.AbortWithStatus(http.StatusUnauthorized)
// c.Redirect(http.StatusFound, "/login")
c.Redirect(http.StatusFound, "/login")
c.Next()
return
}

found, err := service.GetUserByAccountFromRedis(user)
if err != nil {
c.AbortWithStatus(http.StatusUnauthorized)
// c.Redirect(http.StatusFound, "/login")
c.Redirect(http.StatusFound, "/login")
c.Next()
return
}

if found.IsEmpty() {
c.AbortWithStatus(http.StatusUnauthorized)
// c.Redirect(http.StatusFound, "/login")
c.Redirect(http.StatusFound, "/login")
c.Next()
return
}

cValue, err := AdminCookieValue(found)
if err != nil {
c.AbortWithStatus(http.StatusUnauthorized)
// c.Redirect(http.StatusFound, "/login")
c.Redirect(http.StatusFound, "/login")
c.Next()
return
}

if !strings.EqualFold(cValue, cookie) {
c.AbortWithStatus(http.StatusUnauthorized)
// c.Redirect(http.StatusFound, "/login")
c.Redirect(http.StatusFound, "/login")
c.Next()
return
}

Expand Down

0 comments on commit 2500c3f

Please sign in to comment.