public async Task <IActionResult> IndividualRegister([FromBody] IndividualRegisterModel model)
        {
            IndividualRegisterOutput output = new IndividualRegisterOutput();

            if (model.IsValid)
            {
                Register registerUser = new Register(_signInManager, _userManager, _configuration);
                output = await registerUser.UserRegister(model);

                return(Ok(output));
            }
            else
            {
                output.ErrorCode        = IndividualRegisterOutput.ErrorCodes.NullRequest;
                output.ErrorDescription = "Model is not valid";
                output.Token            = null;

                return(BadRequest(output));
            }
        }
Beispiel #2
0
        public async Task <IndividualRegisterOutput> UserRegister(IndividualRegisterModel model)
        {
            IndividualRegisterOutput output = new IndividualRegisterOutput();


            if (model == null)
            {
                Log(ErrorCodes.NullRequest, "Model is not valid", model.Email, model.UserName, model.Channel, model.Password);

                output.ErrorCode        = IndividualRegisterOutput.ErrorCodes.NullRequest;
                output.ErrorDescription = "Model is not valid";
                output.Token            = null;
                return(output);
            }

            try
            {
                var user = new ApplicationUser
                {
                    Email                = model.Email,
                    EmailConfirmed       = true,
                    IsCompany            = false,
                    PhoneNumber          = model.Mobile,
                    PhoneNumberConfirmed = false,
                    UserName             = model.UserName,
                    CreatedDate          = DateTime.Now,
                    LastModifiedDate     = DateTime.Now,
                    LastLoginDate        = DateTime.Now,
                    RoleId               = Guid.Parse("DB5159FA-D585-4FEE-87B1-D9290D515DFB"),
                    LanguageId           = Guid.Parse("5046A00B-D915-48A1-8CCF-5E5DFAB934FB")
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (!result.Succeeded)
                {
                    Log(ErrorCodes.ServiceError, "User failed to register", model.Email, model.UserName, model.Channel, model.Password);


                    StringBuilder errorList = new StringBuilder();
                    result.Errors.ToList().ForEach(e => errorList.Append(e.Description + Environment.NewLine));

                    output.ErrorCode        = IndividualRegisterOutput.ErrorCodes.Success;
                    output.ErrorDescription = "Can not create user, " + errorList.ToString();
                    output.Token            = null;

                    return(output);
                }
                else
                {
                    var registeredUser = await _userManager.FindByEmailAsync(model.Email);

                    if (registeredUser != null)
                    {
                        Log(ErrorCodes.Success, "User Registered Successfully", model.Email, model.UserName, model.Channel, model.Password);

                        GenerateToken    generateToken = new GenerateToken(_configuration);
                        JwtSecurityToken token         = generateToken.GenerateTokenJWT(user.Id, user.Email);

                        output.ErrorCode        = IndividualRegisterOutput.ErrorCodes.Success;
                        output.ErrorDescription = "Registered Successfully";
                        output.Token            = new JwtSecurityTokenHandler().WriteToken(token);

                        return(output);
                    }
                    else
                    {
                        Log(ErrorCodes.Success, "User Not Found", model.Email, model.UserName, model.Channel, model.Password);

                        output.ErrorCode        = IndividualRegisterOutput.ErrorCodes.NullResponse;
                        output.ErrorDescription = "Failed to create user";
                        output.Token            = null;

                        return(output);
                    }
                }
            }
            catch (Exception exp)
            {
                Log(ErrorCodes.Success, exp.ToString(), model.Email, model.UserName, model.Channel, model.Password);

                output.ErrorCode        = IndividualRegisterOutput.ErrorCodes.MethodException;
                output.ErrorDescription = "UserRegister through exception";
                output.Token            = null;

                return(output);
            }
        }