Esempio n. 1
0
        public async Task <User> SignInAsync(SignInUserForm form)
        {
            var user = await _userManager.FindByEmailAsync(form.Email);

            if (user == null)
            {
                throw new RestException(HttpStatusCode.Unauthorized, new { message = "Invalid UserName or Password" });
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user, form.Password, false);

            if (result.Succeeded)
            {
                Profile profile = await _dbContext.Profiles.Where(x => x.Id == user.ProfileId).FirstOrDefaultAsync();

                var newRefreshToken = _jwt.GenerateRefreshToken();
                user.RefreshTokens.Add(newRefreshToken);

                await _userManager.UpdateAsync(user);

                return(new User
                {
                    Avatar = profile.Avatar,
                    Email = user.Email,
                    UserName = user.UserName,
                    Token = _jwt.CreateToken(user.UserName, profile.Id),
                    RefreshToken = newRefreshToken.Token
                });
            }

            throw new RestException(HttpStatusCode.Unauthorized, new { message = "Invalid UserName or Password" });
        }
Esempio n. 2
0
        public async Task <IActionResult> SignIn([FromBody] SignInUserForm form)
        {
            User user = await _identityService.SignInAsync(form);

            SetTokenCookie(user.RefreshToken);
            return(Ok(user));
        }