feat(register-and-statistics): add user registration endpoint

- Added `POST /auth/register` endpoint to support user registration.
- Implemented registration logic in `auth.go` with dynamic role assignment.
- Integrated settings `AllowRegister` and `DefaultRole` for registration flow.
- Updated imports to include new modules: `conf`, `setting`.
- Adjusted user creation logic to use `DefaultRole` setting dynamically.
pull/9277/head
okatu-loli 2025-08-18 12:53:09 +08:00
parent 54b1cf7041
commit a2aaa32c34
2 changed files with 37 additions and 0 deletions

View File

@ -9,8 +9,10 @@ import (
"time"
"github.com/Xhofe/go-cache"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"github.com/pquerna/otp/totp"
@ -89,6 +91,40 @@ func loginHash(c *gin.Context, req *LoginReq) {
loginCache.Del(ip)
}
type RegisterReq struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
// Register a new user
func Register(c *gin.Context) {
if !setting.GetBool(conf.AllowRegister) {
common.ErrorStrResp(c, "registration is disabled", 403)
return
}
var req RegisterReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := &model.User{
Username: req.Username,
Role: model.Roles{setting.GetInt(conf.DefaultRole, int(model.GUEST))},
Authn: "[]",
}
user.SetPassword(req.Password)
if err := op.CreateUser(user); err != nil {
common.ErrorResp(c, err, 500, true)
return
}
token, err := common.GenerateToken(user)
if err != nil {
common.ErrorResp(c, err, 500, true)
return
}
common.SuccessResp(c, gin.H{"token": token})
}
type UserResp struct {
model.User
Otp bool `json:"otp"`

View File

@ -61,6 +61,7 @@ func Init(e *gin.Engine) {
api.POST("/auth/login", handles.Login)
api.POST("/auth/login/hash", handles.LoginHash)
api.POST("/auth/login/ldap", handles.LoginLdap)
api.POST("/auth/register", handles.Register)
auth.GET("/me", handles.CurrentUser)
auth.POST("/me/update", handles.UpdateCurrent)
auth.GET("/me/sshkey/list", handles.ListMyPublicKey)