chore: set guest while token is empty

This commit is contained in:
Noah Hsu
2022-06-26 16:39:02 +08:00
parent 54ca68e4b3
commit 7cbfe93a02
6 changed files with 31 additions and 8 deletions

View File

@ -6,8 +6,16 @@ import (
"github.com/gin-gonic/gin"
)
func AuthAdmin(c *gin.Context) {
// Auth is a middleware that checks if the user is logged in.
// if token is empty, set user to guest
func Auth(c *gin.Context) {
token := c.GetHeader("Authorization")
if token == "" {
guest, _ := db.GetGuest()
c.Set("user", guest)
c.Next()
return
}
userClaims, err := common.ParseToken(token)
if err != nil {
common.ErrorResp(c, err, 401)