Пример #1
0
        public async Task <IActionResult> RegisterUser(RegisterDto model)
        {
            try
            {
                var existingUser = await _userManager.FindByEmailAsync(model.Email);

                if (existingUser is not null)
                {
                    return(BadRequest(new Response {
                        Message = "A user with that email already exists", Success = false
                    }));
                }

                var newUser = new ApplicationUser {
                    Email = model.Email, UserName = model.UserName
                };
                var isCreated = await _userManager.CreateAsync(newUser, model.Password);

                if (isCreated.Succeeded)
                {
                    var response = await _mailService.SendEmailAsync(newUser.Email, "Welcome to Remuner8!", "Thank you for signing up on <strong>Remuner8</strong>.<p>Your life is about to change for the better.</p>");

                    if (!response.IsSuccessStatusCode)
                    {
                        return(StatusCode(StatusCodes.Status500InternalServerError,
                                          new { Success = false, Message = "Mail Service failed", Response = response.Body }));
                    }

                    var jwtToken = await _userService.GenerateJwtToken(newUser);

                    jwtToken.Message  = "User created successfully";
                    jwtToken.UserName = newUser.UserName;
                    return(Ok(jwtToken));
                }
                else
                {
                    return(BadRequest(new RegistrationResponse
                    {
                        Errors = isCreated.Errors.Select(x => x.Description).ToList(),
                        Success = false
                    }));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, new Response {
                    Success = false, Message = $"{ex.Message} {ex.StackTrace}"
                }));
            }
        }
        public async Task <IActionResult> Login(LoginDto model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var verifyEmail = await _userManager.FindByEmailAsync(model.Email);

                    if (verifyEmail is not null)
                    {
                        var verifyPassword = await _userManager.CheckPasswordAsync(verifyEmail, model.Password);

                        if (verifyPassword == true)
                        {
                            await _mailService.SendEmailAsync(model.Email, "New login", "<h1>Hey!, new login to your account noticed</h1><p>New login to your account at " + DateTime.Now + "</p>");

                            var jwtToken = await _userService.GenerateJwtToken(verifyEmail);

                            return(Ok(jwtToken));
                        }
                    }

                    return(Unauthorized(new RegistrationResponse
                    {
                        Errors = new List <string>()
                        {
                            "Invalid login request"
                        },
                        Success = false
                    }));
                }

                return(BadRequest(new Response {
                    Status = "Not sucessful", Message = "The data is not valid please enter valid data"
                }));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }