✨ bootstrap
This commit is contained in:
24
server/auth.go
Normal file
24
server/auth.go
Normal file
@ -0,0 +1,24 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Auth(ctx *fiber.Ctx) error {
|
||||
token := ctx.Get("token")
|
||||
password, err := model.GetSettingByKey("password")
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return ErrorResp(ctx, fmt.Errorf("password not set"), 400)
|
||||
}
|
||||
return ErrorResp(ctx, err, 500)
|
||||
}
|
||||
if token != utils.GetMD5Encode(password.Value) {
|
||||
return ErrorResp(ctx, fmt.Errorf("wrong password"), 401)
|
||||
}
|
||||
return ctx.Next()
|
||||
}
|
@ -10,11 +10,12 @@ func InitApiRouter(app *fiber.App) {
|
||||
{
|
||||
// TODO check accounts
|
||||
public.Post("/path", Path)
|
||||
public.Get("/settings", GetSettingsPublic)
|
||||
}
|
||||
|
||||
admin := app.Group("/api/admin")
|
||||
{
|
||||
// TODO auth
|
||||
admin.Use(Auth)
|
||||
admin.Get("/settings", GetSettingsByType)
|
||||
admin.Post("/settings", SaveSettings)
|
||||
admin.Post("/account", SaveAccount)
|
||||
|
@ -36,3 +36,11 @@ func GetSettingsByType(ctx *fiber.Ctx) error {
|
||||
}
|
||||
return SuccessResp(ctx,settings)
|
||||
}
|
||||
|
||||
func GetSettingsPublic(ctx *fiber.Ctx) error {
|
||||
settings, err := model.GetSettingByType(0)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 400)
|
||||
}
|
||||
return SuccessResp(ctx,settings)
|
||||
}
|
Reference in New Issue
Block a user