MCPcopy Create free account
hub / github.com/53AI/53AIHub / PasswordRegister

Function PasswordRegister

api/controller/user.go:217–323  ·  view source on GitHub ↗

Register User Register @Summary User Register @Description User Register @Tags User @Accept json @Produce json @Param user body PasswordRegisterUserRequest true "User Registration Data" @Success 200 {object} model.CommonResponse{data=LoginResponse} "Success" @Router /api/register [post]

(c *gin.Context)

Source from the content-addressed store, hash-verified

215// @Success 200 {object} model.CommonResponse{data=LoginResponse} "Success"
216// @Router /api/register [post]
217func PasswordRegister(c *gin.Context) {
218 // Parse the request body into PasswordRegisterUserRequest struct
219 var userRequest PasswordRegisterUserRequest
220 err := json.NewDecoder(c.Request.Body).Decode(&userRequest)
221 if err != nil {
222 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err))
223 return
224 }
225
226 params := map[string]interface{}{
227 "from": "user",
228 }
229 _, err = service.IsFeatureAvailable(c, "registered_user", params)
230 if err != nil {
231 c.JSON(http.StatusForbidden, model.FeatureNotAvailableError.ToResponse(err))
232 return
233 }
234
235 username := userRequest.Username
236
237 isEmail := helper.IsValidEmail(username)
238 isMobile := helper.IsValidPhone(username)
239
240 eid := config.GetEID(c)
241
242 if isMobile && config.IS_SAAS {
243 if userRequest.VerifyCode == "" {
244 c.JSON(http.StatusBadRequest, model.InvalidVerificationCodeError.ToNewErrorResponse(model.InvalidVerificationCode))
245 return
246 }
247
248 redisKey := fmt.Sprintf("Api:CheckVerificationCode:%s", username)
249 code, err := common.RedisGet(redisKey)
250 if err != nil || code != userRequest.VerifyCode {
251 c.JSON(http.StatusBadRequest, model.InvalidVerificationCodeError.ToNewErrorResponse(model.InvalidVerificationCode))
252 return
253 }
254 } else if !isEmail && config.IS_SAAS {
255 c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse(model.InvalidMobileOrEmail))
256 return
257 } else if isEmail {
258 enabled, _ := service.IsEnterpriseConfigEnabled(eid, model.EnterpriseConfigTypeSMTP)
259 if enabled || config.IS_SAAS {
260 _, err = common.VerifyEmailCode(username, userRequest.VerifyCode)
261 if err != nil {
262 c.JSON(http.StatusUnauthorized, model.AuthFailed.ToResponse(err))
263 return
264 }
265 }
266 }
267
268 // Get the first user group for this enterprise
269 theGroup, err := model.GetFirstGroupByEid(eid, model.USER_GROUP_TYPE)
270 if err != nil {
271 c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err))
272 return
273 }
274

Callers

nothing calls this directly

Calls 7

CreateMethod · 0.95
DecodeMethod · 0.80
ToResponseMethod · 0.80
GetEIDMethod · 0.80
ToNewErrorResponseMethod · 0.80
ToErrorResponseMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected