memos/api/middlewares.go

29 lines
680 B
Go
Raw Normal View History

2021-12-08 23:43:14 +08:00
package api
import (
2021-12-09 22:02:57 +08:00
"memos/api/e"
2021-12-08 23:43:14 +08:00
"net/http"
)
func AuthCheckerMiddleWare(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2021-12-10 13:41:17 +08:00
session, _ := SessionStore.Get(r, "session")
2021-12-08 23:43:14 +08:00
2021-12-10 13:41:17 +08:00
if userId, ok := session.Values["user_id"].(string); !ok || userId == "" {
2021-12-09 22:02:57 +08:00
e.ErrorHandler(w, "NOT_AUTH", "Need authorize")
2021-12-08 23:43:14 +08:00
return
}
next.ServeHTTP(w, r)
})
}
2021-12-09 22:02:57 +08:00
func CorsMiddleWare(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
next.ServeHTTP(w, r)
})
}