feat: update

This commit is contained in:
yanghao05
2025-04-29 20:57:56 +08:00
parent 029ff4d84a
commit dd702a5bee
2 changed files with 17 additions and 6 deletions

View File

@@ -29,11 +29,6 @@ func (f *Middlewares) Auth(ctx fiber.Ctx) error {
return ctx.Next() return ctx.Next()
} }
// check is XMLHttpRequest
if ctx.XHR() {
return ctx.SendStatus(fiber.StatusUnauthorized)
}
fullUrl := utils.FullURI(ctx) fullUrl := utils.FullURI(ctx)
u, err := url.Parse(fullUrl) u, err := url.Parse(fullUrl)
if err != nil { if err != nil {
@@ -50,6 +45,9 @@ func (f *Middlewares) Auth(ctx fiber.Ctx) error {
log.Infof("cookie: %s", cookie) log.Infof("cookie: %s", cookie)
if cookie == "" { if cookie == "" {
log.Infof("auth redirect_uri: %s", fullUrl) log.Infof("auth redirect_uri: %s", fullUrl)
if ctx.XHR() {
return ctx.SendStatus(fiber.StatusUnauthorized)
}
return ctx.Redirect().To(fullUrl) return ctx.Redirect().To(fullUrl)
} }
@@ -57,6 +55,9 @@ func (f *Middlewares) Auth(ctx fiber.Ctx) error {
if err != nil { if err != nil {
// remove cookie // remove cookie
ctx.ClearCookie("token") ctx.ClearCookie("token")
if ctx.XHR() {
return ctx.SendStatus(fiber.StatusUnauthorized)
}
return ctx.Redirect().To(fullUrl) return ctx.Redirect().To(fullUrl)
} }
@@ -64,6 +65,9 @@ func (f *Middlewares) Auth(ctx fiber.Ctx) error {
if err != nil { if err != nil {
// remove cookie // remove cookie
ctx.ClearCookie("token") ctx.ClearCookie("token")
if ctx.XHR() {
return ctx.SendStatus(fiber.StatusUnauthorized)
}
return ctx.Redirect().To(fullUrl) return ctx.Redirect().To(fullUrl)
} }
ctx.Locals("user", user) ctx.Locals("user", user)

View File

@@ -58,6 +58,11 @@ Content-Type: application/json
GET {{host}}/v1/admin/orders HTTP/1.1 GET {{host}}/v1/admin/orders HTTP/1.1
Content-Type: application/json Content-Type: application/json
### get posts
GET {{host}}/v1/posts HTTP/1.1
Content-Type: application/json
Cookie: token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxMDAxLCJleHAiOjE3NDY1MzU4MjYsIm5iZiI6MTc0NTkzMTAxNn0.Vg8LqDQLkpEvohBYEPSTPrGzKAyMNJ_m7uSm3NBbuDM
### get orders ### get orders
GET {{host}}/v1/posts/mine HTTP/1.1 GET {{host}}/v1/posts/mine HTTP/1.1
Content-Type: application/json Content-Type: application/json
@@ -80,3 +85,5 @@ Authorization: {{token}}
### get user profile ### get user profile
GET {{host}}/v1/users/profile HTTP/1.1 GET {{host}}/v1/users/profile HTTP/1.1
Authorization: {{token}} Authorization: {{token}}