public async Task <UserWrapper> Handle(RegisterUserCommand command, CancellationToken cancellationToken) { var userData = command.User; var user = await _context.Users.FirstOrDefaultAsync(i => i.Login == userData.Email, cancellationToken); if (user != default(User)) { throw new ValidationCommandException(new { User = ErrorMessages.AlreadyExist }); } user = new User { Login = userData.Email, Password = _passwordHashProvider.HashPassword(userData.Password), Profile = new Profile() { Username = userData.Username, Email = userData.Email }, }; var claims = GetUserClaims(user); user.Token = _jwtService.GenerateToken(claims); _context.Users.Add(user); await _context.SaveChangesAsync(cancellationToken); return(new UserWrapper(user)); }
public IActionResult Login([FromBody] UserLoginDTO userLoginDto) { try { User user = _eyadtakDbContext.Users.FirstOrDefault(x => x.UserEmail == userLoginDto.Email); if (user == null || Encription.Decrypt(user?.Password, "SecretCode_hamed") != userLoginDto.Password) { return(Ok(new { message = "Wrong Email or Password", ErrorHappen = true })); } if (!user.Active) { return(Ok(new { message = "Your account is not active yet, please check your email", ErrorHappen = true })); } List <int> abilitiesIds = _eyadtakDbContext.Users_Roles.Where(x => x.UserId == user.UserId).Include(x => x.Role).SelectMany(x => x.Role.Role_Ability).Select(x => x.Ability.AbilityId).ToList(); string token = _jwt.GenerateToken(user.UserId); return(Ok(new { Token = token, AbilitiesIds = abilitiesIds, userName = user.UserName, userEmail = user.UserEmail, ErrorHappen = false })); } catch (Exception e) { return(Ok(new { message = "Something went wrong", ErrorHappen = true })); throw e; } }
public IActionResult Register([FromBody] UserRegisterDTO userRegisterDto) { User user = UserMapper.Map(userRegisterDto); FillEmptyFields(user); UserValidator validator = new UserValidator(ValidationMode.Create, db); var result = validator.Validate(user); if (!result.IsValid) { return(Json(new { statusCode = ResponseStatus.ValidationError, responseMessage = result.Errors })); } user.Password = Encription.Encrypt(user.Password, "SecretCode_hamed"); db.Add(user); db.SaveChanges(); string token = jwt.GenerateToken(user.Id); email.SendAccountActivationEmail(user.Email, "https://localhost:44340/Account/ActivateAccount/?token=" + token); return(Json(new { statusCode = ResponseStatus.Success, responseMessage = user.Id })); }
public IHttpActionResult Registrar(Usuario usuario) { try { if (_usuarioService.VerificarEmail(usuario.Email)) { return(_customMessasge.Create(HttpStatusCode.Conflict, "E-mail já cadastrado.")); } var novoUsuario = new Usuario(usuario.Nome, usuario.Email, _criptografia.Hash(usuario.Senha), usuario.Telefones, _jwt.GenerateToken(usuario.Email)); _uokOfWork.BeginTransaction(); _usuarioService.Adicionar(novoUsuario); _uokOfWork.Commit(); return(Created("Usuario", novoUsuario)); } catch (Exception ex) { return(InternalServerError(ex)); } }