Example #1
0
        public async Task <IActionResult> GenerateToken(TokenRequestResource tokenRequestResource)
        {
            var tokenRequest = _mapper.Map <TokenRequestResource, TokenRequest>(tokenRequestResource);

            var result = await _tokenService.GetToken(tokenRequest, _jwtSettings);

            SetRefreshTokenInCookie(result.RefreshToken);
            return(Ok(result));
        }
Example #2
0
        public async Task <IActionResult> SignIn(UserLoginResource userLoginResource)
        {
            // Email exits ?
            var user = _userManager.Users.SingleOrDefault(u => u.Email == userLoginResource.Email);

            //List<UserRole> list = new List<UserRole>();
            //UserRole userRole = new UserRole();

            if (user is null)
            {
                return(NotFound("User not found"));
            }

            // Check Password
            var userSigninResult = await _userManager.CheckPasswordAsync(user, userLoginResource.Password);

            List <string> roles = new List <string>();

            if (userSigninResult)
            {
                TokenRequestResource newTokenRequest = new TokenRequestResource();
                newTokenRequest.Email    = user.Email;
                newTokenRequest.Password = userLoginResource.Password;

                var listRoles = await _userManager.GetRolesAsync(user);

                foreach (var role in listRoles)
                {
                    Role newRole = new Role();
                    newRole.Name = role;
                    roles.Add(newRole.Name);
                }
                newTokenRequest.Roles = roles;

                var tokenRequest = _mapper.Map <TokenRequestResource, TokenRequest>(newTokenRequest);
                var result       = await _tokenService.GetToken(tokenRequest, _jwtSettings);

                var tokenRequestResource = _mapper.Map <TokenRequest, TokenRequestResource>(result);

                return(Ok(tokenRequestResource));
            }

            return(BadRequest("Email or password incorrect."));
        }