public async Task <ResultDto <AuthenticatedDto> > Login(AuthDto authDto) { var success = await _signInManager.PasswordSignInAsync(authDto.Email, authDto.Senha, false, false); if (!success.Succeeded) { return(ResultDto <AuthenticatedDto> .Validation("Login ou senha inválidos!")); } var applicationUser = await GetUserByEmail(authDto.Email); if (!applicationUser.Estabelecimentos.Any(x => x.EstabelecimentoId == authDto.EstabelecimentoId)) { return(ResultDto <AuthenticatedDto> .Validation("Usuário não vinculado ao estabalecimento!")); } var userDto = new UserDto { Id = applicationUser.Id, Nome = applicationUser.Nome, Email = applicationUser.Email, Role = applicationUser.Role, Claims = applicationUser.Claims, EstabelecimentoId = authDto.EstabelecimentoId, EstabelecimentoNomeUrl = applicationUser.Estabelecimentos.FirstOrDefault(x => x.EstabelecimentoId == authDto.EstabelecimentoId).Url, EstabelecimentoNome = applicationUser.Estabelecimentos.FirstOrDefault(x => x.EstabelecimentoId == authDto.EstabelecimentoId).Nome }; return(ResultDto <AuthenticatedDto> .Success(TokenWrite.WriteToken(userDto, _tokenConfigurations, _signingConfigurations))); }
public async Task <ResultDto <AuthenticatedDto> > LoginAdmin(AuthDto authDto) { var success = await _signInManager.PasswordSignInAsync(authDto.Email, authDto.Senha, false, false); if (!success.Succeeded) { return(ResultDto <AuthenticatedDto> .Validation("Login ou senha inválidos!")); } var applicationUser = await GetUserByEmail(authDto.Email); if (applicationUser.Role != "Administrador") { return(ResultDto <AuthenticatedDto> .Validation("Perfil não é Administrador!")); } var userDto = new UserDto { Id = applicationUser.Id, Nome = applicationUser.Nome, Email = applicationUser.Email, Role = applicationUser.Role, Claims = applicationUser.Claims }; return(ResultDto <AuthenticatedDto> .Success(TokenWrite.WriteToken(userDto, _tokenConfigurations, _signingConfigurations))); }
public async Task <ResultDto <AuthenticatedDto> > LoginSocial(AuthDto authDto) { var success = await _signInManager.PasswordSignInAsync(authDto.Email, authDto.Senha, false, false); UserDto userDto; if (!success.Succeeded) { var result = await SalvarCliente(new UserDto() { Email = authDto.Email, Senha = authDto.Senha, Nome = authDto.Nome, EstabelecimentoId = authDto.EstabelecimentoId, IsFacebook = authDto.IsFacebook, IsGoogle = authDto.IsGoogle }); if (result.StatusCode != 200) { return(await Task.FromResult(ResultDto <AuthenticatedDto> .Validation(result.Errors))); } userDto = result.Data; } else { var applicationUser = await GetUserByEmail(authDto.Email); if (!applicationUser.Estabelecimentos.Any(x => x.EstabelecimentoId == authDto.EstabelecimentoId)) { var user = await _userManager.Users?.FirstOrDefaultAsync(u => u.Id == applicationUser.Id); var userEstabelecimento = new UserEstabelecimento() { EstabelecimentoId = authDto.EstabelecimentoId }; user.AdicionarEstabelecimento(userEstabelecimento); await _userManager.UpdateAsync(user); applicationUser = await GetUserByEmail(authDto.Email); userDto = new UserDto { Id = applicationUser.Id, Nome = applicationUser.Nome, Email = applicationUser.Email, Role = applicationUser.Role, Claims = applicationUser.Claims, EstabelecimentoId = authDto.EstabelecimentoId, EstabelecimentoNomeUrl = applicationUser.Estabelecimentos.FirstOrDefault(x => x.EstabelecimentoId == authDto.EstabelecimentoId).Url, EstabelecimentoNome = applicationUser.Estabelecimentos.FirstOrDefault(x => x.EstabelecimentoId == authDto.EstabelecimentoId).Nome }; _emailService.Send(userDto.Email, "Confirmação de cadastro", userDto.EstabelecimentoNome, EmailTemplate.ConfirmacaoCadastro(_configuration, userDto)); } else { userDto = new UserDto { Id = applicationUser.Id, Nome = applicationUser.Nome, Email = applicationUser.Email, Role = applicationUser.Role, Claims = applicationUser.Claims, EstabelecimentoId = authDto.EstabelecimentoId, EstabelecimentoNomeUrl = applicationUser.Estabelecimentos.FirstOrDefault(x => x.EstabelecimentoId == authDto.EstabelecimentoId).Url, EstabelecimentoNome = applicationUser.Estabelecimentos.FirstOrDefault(x => x.EstabelecimentoId == authDto.EstabelecimentoId).Nome }; } } return(ResultDto <AuthenticatedDto> .Success(TokenWrite.WriteToken(userDto, _tokenConfigurations, _signingConfigurations))); }