public async Task <TokenDto> SignIn(SignInDto signInDto)
        {
            var user = await userRepository.FindByLoginAsync(signInDto.Email);

            TokenDto tokenDto = new TokenDto();

            if (user == null || user.StatusOfVerification == "Processing" || user.StatusOfVerification == "Blocked")
            {
                tokenDto.Code = 401;
                return(tokenDto);
            }
            if (signInDto.Email != user.Email || !VerifyPassword(signInDto.Password, user.HashPassword, user.Salt))
            {
                tokenDto.Code = 401;
                return(tokenDto);
            }
            //Return two tokens Access, Refresh
            tokenDto.Name         = user.FirstName;
            tokenDto.Code         = 200;
            tokenDto.AccessToken  = token.GenerateToken(user);
            tokenDto.RefreshToken = token.RefreshGenerateToken();
            //Save To database Refresh token
            RefreshTokens refreshToken = new RefreshTokens(tokenDto.RefreshToken, user.Id, true);

            refreshRepository.Create(refreshToken);
            await refreshRepository.SaveChangesAsync();

            return(tokenDto);
        }
Example #2
0
        /// <summary>
        /// Sign in with correct email and input password
        /// </summary>
        /// <param name="userLoginDto"></param>
        /// <returns>return TokenDto with new Access Token and Refresh Token
        /// if user null or email or password not correct
        /// return tokenDto with code 401</returns>
        public async Task <TokenDto> SignInAsync(UserLoginDto userLoginDto)
        {
            var user = await userRepository.FindByLoginAsync(userLoginDto.Email);

            TokenDto tokenDto = new TokenDto();

            if (user == null)
            {
                tokenDto.Code = 401;
                return(tokenDto);
            }
            if (userLoginDto.Email != user.Email || !VerifyPassword(userLoginDto.Password, user.HashPassword, user.Salt))
            {
                tokenDto.Code = 401;
                return(tokenDto);
            }
            //Return two tokens Access, Refresh
            tokenDto.Code         = 200;
            tokenDto.AccessToken  = token.GenerateToken(user);
            tokenDto.RefreshToken = token.RefreshGenerateToken();
            //Save To database Refresh token
            RefreshToken refreshToken = new RefreshToken(tokenDto.RefreshToken, user.UserId, true);

            refreshRepository.Create(refreshToken);
            await refreshRepository.SaveChangesAsync();

            return(tokenDto);
        }
Example #3
0
        /// <summary>
        /// Generate Access Token and Refresh Token
        /// </summary>
        /// <param name="token"></param>
        /// <returns>return TokenDto with code 200</returns>
        public async Task <TokenDto> GenerateRefreshTokenAsync(TokenClaimsDto token)
        {
            TokenDto tokenDto = new TokenDto();
            var      user     = await userRepository.FindByIdDetailsAsync(token.UserId);

            tokenDto.AccessToken  = tokenGeneratorService.GenerateToken(user);
            tokenDto.RefreshToken = tokenGeneratorService.RefreshGenerateToken();
            tokenDto.Code         = 200;
            return(tokenDto);
        }