Exemplo n.º 1
0
        public IActionResult SignInUser([FromBody] LoginViewModel loginDetails)
        {
            try
            {
                if (!ModelState.IsValid || loginDetails == null)
                {
                    return(BadRequest());
                }

                if (!string.IsNullOrEmpty(loginDetails.Email) && !string.IsNullOrEmpty(loginDetails.Password))
                {
                    var hashedPassword = PasswordEncryption.SHA512ComputeHash(loginDetails.Password);

                    var userInDb = _userService.GetUserByEmail(loginDetails.Email);

                    if (userInDb.Password.Equals(hashedPassword))
                    {
                        return(Ok("Login successful"));
                    }
                }
            }
            catch (WebException ex)
            {
                Trace.TraceError(ex.Message);
                throw new WebException();
            }

            return(BadRequest("Incorrect login details"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateUser([FromBody] UserViewModel userViewModel)
        {
            if (ModelState.IsValid)
            {
                var checkEmailInDb = _userService.GetUserByEmail(userViewModel.Email);

                if (checkEmailInDb == null)
                {
                    var newUser = new User
                    {
                        UserName = userViewModel.UserName,
                        Email    = userViewModel.Email,
                        Password = PasswordEncryption.SHA512ComputeHash(userViewModel.Password)
                    };

                    await _userService.AddUser(newUser);

                    return(Ok());
                }
                else
                {
                    return(BadRequest("This email is already in use"));
                }
            }

            return(BadRequest());
        }
Exemplo n.º 3
0
        public IActionResult UpdatePassword([FromBody] LoginViewModel loginDetails)
        {
            var hashedPassword = PasswordEncryption.SHA512ComputeHash(loginDetails.Password);

            try
            {
                if (!ModelState.IsValid || loginDetails == null)
                {
                    return(BadRequest());
                }

                if (!string.IsNullOrEmpty(loginDetails.Email) && !string.IsNullOrEmpty(loginDetails.Password))
                {
                    var userInDb = _userService.GetUserByEmail(loginDetails.Email);

                    if (userInDb.Password.Equals(hashedPassword))
                    {
                        var userModel = new UserModel
                        {
                            Id       = userInDb.Id,
                            UserName = userInDb.UserName,
                            Email    = loginDetails.Email,
                            Password = loginDetails.Password
                        };

                        _userService.UpdateUserDetails(userModel);

                        return(Ok("Password changed"));
                    }
                }
            }
            catch (WebException ex)
            {
                Trace.TraceError(ex.Message);
                throw new WebException();
            }

            return(BadRequest("Unable to sign in"));
        }