Exemple #1
0
        public async Task <IActionResult> LoginAsync([FromBody] UserAuthDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ApplicationUser user         = null;
            bool            userNotFound = false;

            try
            {
                user = await userService.FindUserByEmailAsync(dto.Email);
            }
            catch (CurrentEntryNotFoundException)
            {
                userNotFound = true;
            }

            if (userNotFound || !await userService.CheckPasswordAsync(user, dto.Password) || await userService.IsCustomer(user))
            {
                ModelState.AddModelError("loginFailure", "Invalid email or password");
                return(BadRequest(ModelState));
            }
            else if (user.IsBlocked ?? false)
            {
                ModelState.AddModelError("loginFailure", "Account has been blocked");
                return(BadRequest(ModelState));
            }

            var userClaims = await jwtService.GetClaimsAsync(user);

            var accessToken  = jwtService.GenerateJwtAccessToken(userClaims);
            var refreshToken = jwtService.GenerateJwtRefreshToken();
            await jwtService.LoginByRefreshTokenAsync(user.Id, refreshToken);

            return(Ok(await GetBuildToken(accessToken, refreshToken)));
        }