mirror of https://github.com/Xhofe/alist
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
parent
54b1cf7041
commit
a2aaa32c34
|
@ -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"`
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue