feat: complete login

This commit is contained in:
yanghao05
2025-04-15 21:20:04 +08:00
parent 45a0b6848a
commit ca08568e1a
23 changed files with 842 additions and 28 deletions

View File

@@ -0,0 +1,57 @@
package middlewares
import (
"net/url"
"strings"
"quyun/app/models"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
)
func (f *Middlewares) Auth(ctx fiber.Ctx) error {
if strings.HasPrefix(ctx.Path(), "/admin/") {
return ctx.Next()
}
if strings.HasPrefix(ctx.Path(), "/auth/") {
return ctx.Next()
}
fullUrl := string(ctx.Request().URI().FullURI())
u, err := url.Parse(fullUrl)
if err != nil {
return err
}
query := u.Query()
query.Set("redirect", fullUrl)
u.RawQuery = query.Encode()
u.Path = "/auth/wechat"
fullUrl = u.String()
// check cookie exists
cookie := ctx.Cookies("token")
log.Infof("cookie: %s", cookie)
if cookie == "" {
log.Infof("auth redirect_uri: %s", fullUrl)
return ctx.Redirect().To(fullUrl)
}
jwt, err := f.jwt.Parse(cookie)
if err != nil {
// remove cookie
ctx.ClearCookie("token")
return ctx.Redirect().To(fullUrl)
}
user, err := models.Users.GetByID(ctx.Context(), jwt.UserID)
if err != nil {
// remove cookie
ctx.ClearCookie("token")
return ctx.Redirect().To(fullUrl)
}
ctx.Locals("user", user)
return ctx.Next()
}

View File

@@ -2,8 +2,13 @@ package middlewares
import (
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
)
func (f *Middlewares) DebugMode(c fiber.Ctx) error {
return c.Next()
func (f *Middlewares) DebugMode(ctx fiber.Ctx) error {
log.Infof("c.Path: %s", ctx.Path())
log.Infof("Request Method: %s", ctx.Method())
log.Infof("FullURL: %s", ctx.Request().URI().FullURI())
return ctx.Next()
}

View File

@@ -1,12 +1,15 @@
package middlewares
import (
"quyun/providers/jwt"
log "github.com/sirupsen/logrus"
)
// @provider
type Middlewares struct {
log *log.Entry `inject:"false"`
jwt *jwt.JWT
}
func (f *Middlewares) Prepare() error {

View File

@@ -1,13 +1,19 @@
package middlewares
import (
"quyun/providers/jwt"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/opt"
)
func Provide(opts ...opt.Option) error {
if err := container.Container.Provide(func() (*Middlewares, error) {
obj := &Middlewares{}
if err := container.Container.Provide(func(
jwt *jwt.JWT,
) (*Middlewares, error) {
obj := &Middlewares{
jwt: jwt,
}
if err := obj.Prepare(); err != nil {
return nil, err
}