예제 #1
0
        public IActionResult Token(SignInViewModel credential)
        {
            if (credential == null || string.IsNullOrEmpty(credential.Password) || string.IsNullOrEmpty(credential.Email))
            {
                UsersViewModel result = new UsersViewModel();
                result.Success = false;
                result.Error   = "User credentials are null";
                return(Ok(result));
            }
            var user = userRepository.getByEmailPassword(credential.Email, credential.Password);

            if (user == null)
            {
                UsersViewModel result = new UsersViewModel();
                result.Success = false;
                result.Error   = "Invalid User Credentials";
                return(Ok(result));
            }

            //User authenticated successfully
            user = userRepository.getUserById(user.UserId);
            string tokenString = generateToken(user.Name, user.Email, user.UserId, 60);
            //make the userViewModel
            UserRolesViewModel userRolesViewModel = CodeFreakMapper.UsersToUserRolesViewModel(user);

            userRolesViewModel.Token   = tokenString;
            userRolesViewModel.Success = true;

            UserIdentity userIdentity = new UserIdentity();

            userIdentity.UserRolesIdentity = userRolesViewModel;
            Claim claim = new Claim("user", userRolesViewModel.User.UserId.ToString());

            userIdentity.AddClaim(claim);
            User.AddIdentity(userIdentity);



            return(Ok(userRolesViewModel));
        }