示例#1
0
        public async Task <IActionResult> Login([FromBody] LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                await NotifyErrorModelInvalid();

                return(await ResponseAsync(model));
            }

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

            if (user != null && !await _userManager.IsEmailConfirmedAsync(user))
            {
                await NotifyError("ConfirmEmail", "E-mail não confirmado");

                return(await ResponseAsync(model));
            }

            if (model.GrantType == "password")
            {
                if (model.Senha == null)
                {
                    await NotifyError("Login", "E-mail ou senha incorreto(s)");

                    return(await ResponseAsync(model));
                }

                var result = await _signInManager.PasswordSignInAsync(model.Email, model.Senha, false, true);

                if (result.Succeeded)
                {
                    var response = await GenerateToken(model);

                    return(await ResponseAsync(response));
                }

                await NotifyError(result.ToString(), "E-mail ou senha incorreto(s)");

                return(await ResponseAsync(model));
            }
            else if (model.GrantType == "refresh_token")
            {
                if (user == null)
                {
                    await NotifyError("RefreshToken", "Usuário não encontrado");

                    return(await ResponseAsync(model));
                }

                var refreshToken = _refreshTokenRepository.GetByRefreshToken(user.Id, model.RefreshToken);

                if (refreshToken == null || refreshToken.ExpirationDate < DateTime.Now)
                {
                    await NotifyError("RefreshToken", "RefreshToken não autorizado");

                    return(await ResponseAsync(model));
                }

                var response = await GenerateToken(model);

                return(await ResponseAsync(response));
            }

            await NotifyError("Login", "Tipo de login inválido.");

            return(await ResponseAsync(model));
        }
 public async Task <RefreshToken> GetByRefreshToken(string refreshToken)
 {
     return(await refreshTokenRepository.GetByRefreshToken(refreshToken));
 }