public async Task <CompanyRegisterOutput> UserRegister(RegisterCompanyModel model)
        {
            CompanyRegisterOutput output = new CompanyRegisterOutput();

            if (model == null)
            {
                Log(ErrorCodes.NullRequest, "Model is Null", model);

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

            try
            {
                var user = new ApplicationUser
                {
                    Email                = model.Email,
                    UserName             = model.Email,
                    IsCompany            = true,
                    EmailConfirmed       = true,
                    PhoneNumber          = model.Mobile,
                    PhoneNumberConfirmed = false,
                    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.ServiceException, "User failed to register", model);


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

                    output.ErrorCode        = CompanyRegisterOutput.ErrorCodes.ServiceException;
                    output.ErrorDescription = 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);

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

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

                        return(output);
                    }
                    else
                    {
                        Log(ErrorCodes.NullResponse, "User not found", model);

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

                        return(output);
                    }
                }
            }
            catch (Exception exp)
            {
                Log(ErrorCodes.MethodException, exp.ToString(), model);

                output.ErrorCode        = CompanyRegisterOutput.ErrorCodes.MethodException;
                output.ErrorDescription = "UserLogin through exception";
                output.Token            = null;

                return(output);
            }
        }
예제 #2
0
        public async Task <LoginOutput> UserLogin(LoginModel model)
        {
            LoginOutput output = new LoginOutput();

            if (model == null)
            {
                Log(ErrorCodes.NullRequest, "Model is Null", null, null, null, null);

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

            try
            {
                var user = await _userManager.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    var result = await _signInManager.CheckPasswordSignInAsync(user, model.Password, false);

                    if (result.Succeeded)
                    {
                        Log(ErrorCodes.Success, "Success authenticated User", model.UserName, model.Password, model.Email, model.Channel);


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

                        output.ErrorCode        = LoginOutput.ErrorCodes.Success;
                        output.ErrorDescription = "Success authenticated User";
                        output.Token            = new JwtSecurityTokenHandler().WriteToken(token);

                        return(output);
                    }
                    else
                    {
                        Log(ErrorCodes.NullResponse, "Incorrect password", model.UserName, model.Password, model.Email, model.Channel);

                        output.ErrorCode        = LoginOutput.ErrorCodes.NullResponse;
                        output.ErrorDescription = "Incorrect password";
                        output.Token            = null;

                        return(output);
                    }
                }
                else
                {
                    Log(ErrorCodes.NullResponse, "User not exist", model.UserName, model.Password, model.Email, model.Channel);

                    output.ErrorCode        = LoginOutput.ErrorCodes.NullResponse;
                    output.ErrorDescription = "User not exist";
                    output.Token            = null;

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

                output.ErrorCode        = LoginOutput.ErrorCodes.MethodException;
                output.ErrorDescription = "UserLogin through exception";
                output.Token            = null;

                return(output);
            }
        }