refactor: migration auth api to v1

This commit is contained in:
Steven
2023-06-20 16:33:36 +08:00
parent 20884e9370
commit 6004a2f657
13 changed files with 428 additions and 251 deletions

View File

@@ -3,7 +3,6 @@ package server
import (
"strings"
"github.com/boojack/shortify/api"
"github.com/labstack/echo/v4"
)
@@ -29,34 +28,10 @@ func hasPrefixes(src string, prefixes ...string) bool {
func defaultAPIRequestSkipper(c echo.Context) bool {
path := c.Path()
return hasPrefixes(path, "/api", "/o")
return hasPrefixes(path, "/api")
}
func (server *Server) defaultAuthSkipper(c echo.Context) bool {
ctx := c.Request().Context()
func (*Server) defaultAuthSkipper(c echo.Context) bool {
path := c.Path()
// Skip auth.
if hasPrefixes(path, "/api/auth") {
return true
}
// If there is openId in query string and related user is found, then skip auth.
openID := c.QueryParam("openId")
if openID != "" {
userFind := &api.UserFind{
OpenID: &openID,
}
user, err := server.Store.FindUser(ctx, userFind)
if err != nil {
return false
}
if user != nil {
// Stores userID into context.
c.Set(getUserIDContextKey(), user.ID)
return true
}
}
return false
return hasPrefixes(path, "/api/v1/auth")
}