112 lines
2.1 KiB
Go
112 lines
2.1 KiB
Go
package handler
|
|
|
|
import (
|
|
_ "embed"
|
|
"errors"
|
|
"github.com/loveuer/nf"
|
|
"github.com/loveuer/nf/nft/resp"
|
|
"gorm.io/gorm"
|
|
"uauth/internal/store/db"
|
|
"uauth/internal/tool"
|
|
"uauth/model"
|
|
)
|
|
|
|
func ClientRegistry(c *nf.Ctx) error {
|
|
type Req struct {
|
|
ClientId string `json:"client_id"`
|
|
Icon string `json:"icon"` // url
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
var (
|
|
err error
|
|
req = new(Req)
|
|
)
|
|
|
|
if err = c.BodyParser(req); err != nil {
|
|
return resp.Resp400(c, err.Error())
|
|
}
|
|
|
|
Secret := tool.RandomString(32)
|
|
|
|
platform := &model.Client{
|
|
ClientId: req.ClientId,
|
|
Icon: req.Icon,
|
|
Name: req.Name,
|
|
ClientSecret: Secret,
|
|
}
|
|
|
|
if err = db.Default.Session().Create(platform).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
|
return resp.Resp400(c, err, "当前平台已经存在")
|
|
}
|
|
|
|
return resp.Resp500(c, err)
|
|
}
|
|
|
|
return resp.Resp200(c, platform)
|
|
}
|
|
|
|
var (
|
|
//go:embed serve_registry.html
|
|
registryLogin string
|
|
)
|
|
|
|
func UserRegistryPage(c *nf.Ctx) error {
|
|
return c.HTML(registryLogin)
|
|
}
|
|
|
|
func UserRegistryAction(c *nf.Ctx) error {
|
|
type Req struct {
|
|
Username string `form:"username"`
|
|
Nickname string `form:"nickname"`
|
|
Password string `form:"password"`
|
|
ConfirmPassword string `form:"confirm_password"`
|
|
}
|
|
|
|
var (
|
|
err error
|
|
req = new(Req)
|
|
)
|
|
|
|
if err = c.BodyParser(req); err != nil {
|
|
return resp.Resp400(c, err.Error())
|
|
}
|
|
|
|
if err = tool.CheckPassword(req.Password); err != nil {
|
|
return resp.Resp400(c, req, err.Error())
|
|
}
|
|
|
|
op := &model.User{
|
|
Username: req.Username,
|
|
Nickname: req.Nickname,
|
|
Password: tool.NewPassword(req.Password),
|
|
}
|
|
|
|
if err = db.Default.Session().Create(op).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return resp.Resp400(c, err, "用户名已存在")
|
|
}
|
|
|
|
return resp.Resp500(c, err)
|
|
}
|
|
|
|
return c.HTML(`
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<link
|
|
rel="stylesheet"
|
|
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.jade.min.css"
|
|
>
|
|
<title>注册成功</title>
|
|
</head>
|
|
<body>
|
|
<h1>注册成功</h1>
|
|
<h3>快去试试吧</h3>
|
|
</body>
|
|
</html>
|
|
`)
|
|
}
|