コード例 #1
0
        //public async Task<LoginResponseModel> Login(LoginRequestModel model)
        //{
        //    var user = _unitOfWork.Repository<ApplicationUser>().Get(x => x.Email == model.Email)
        //        .Include(x => x.UserRoles)
        //            .ThenInclude(x => x.Role)
        //        .FirstOrDefault();
        //
        //    if (user == null || !await _userManager.CheckPasswordAsync(user, model.Password) || !user.UserRoles.Any(x => x.Role.Name == Role.User))
        //        throw new CustomException(HttpStatusCode.BadRequest, "credentials", "Invalid credentials");
        //
        //    if (!string.IsNullOrEmpty(model.Email) && !user.EmailConfirmed)
        //        throw new CustomException(HttpStatusCode.BadRequest, "email", "Email is not confirmed");
        //
        //    if (user.IsDeleted)
        //        throw new CustomException(HttpStatusCode.BadRequest, "general", "Your account was deleted by admin, to know more please contact administration.");
        //
        //    if (!user.IsActive)
        //        throw new CustomException(HttpStatusCode.MethodNotAllowed, "general", "Your account was blocked. For more information please email to following address: ");
        //
        //    return await _jwtService.BuildLoginResponse(user, model.AccessTokenLifetime);
        //}

        //public async Task<LoginResponseModel> LoginUsingPhone(LoginWithPhoneRequestModel model)
        //{
        //    var user = _unitOfWork.Repository<ApplicationUser>().Find(x => x.PhoneNumber == model.PhoneNumber);
        //
        //    if (user == null || !await _userManager.CheckPasswordAsync(user, model.Password))
        //        throw new CustomException(HttpStatusCode.BadRequest, "credentials", "Invalid credentials");
        //
        //    if (!user.PhoneNumberConfirmed)
        //        throw new CustomException(HttpStatusCode.BadRequest, "phoneNumber", "PhoneNumber is not confirmed");
        //
        //    if (user.IsDeleted)
        //        throw new CustomException(HttpStatusCode.BadRequest, "general", "Your account was deleted by admin, to know more please contact administration.");
        //
        //    if (!user.IsActive)
        //        throw new CustomException(HttpStatusCode.MethodNotAllowed, "general", "Your account was blocked. For more information please email to following address: ");
        //
        //    return await _jwtService.BuildLoginResponse(user, model.AccessTokenLifetime);
        //}

        //public async Task<LoginResponseModel> AdminLogin(AdminLoginRequestModel model)
        //{
        //    var user = _unitOfWork.Repository<ApplicationUser>().Get(x => x.Email == model.Email)
        //        .TagWith(nameof(Login) + "_GetAdmin")
        //        .Include(x => x.UserRoles)
        //            .ThenInclude(x => x.Role)
        //        .FirstOrDefault();
        //
        //    if (user == null || !await _userManager.CheckPasswordAsync(user, model.Password) || !user.UserRoles.Any(x => x.Role.Name == Role.Admin || x.Role.Name == Role.SuperAdmin))
        //        throw new CustomException(HttpStatusCode.BadRequest, "general", "Invalid credentials");
        //
        //    return await _jwtService.BuildLoginResponse(user, model.AccessTokenLifetime);
        //}

        public async Task <TokenResponseModel> RefreshTokenAsync(string refreshToken)
        {
            var token = _unitOfWork.Repository <UserToken>().Get(w => w.RefreshTokenHash == _hashUtility.GetHash(refreshToken) && w.IsActive && w.RefreshExpiresDate > DateTime.UtcNow)
                        .TagWith(nameof(RefreshTokenAsync) + "_GetRefreshToken")
                        .Include(x => x.User)
                        .FirstOrDefault();

            if (token == null)
            {
                throw new CustomException(HttpStatusCode.BadRequest, "refreshToken", "Refresh token is invalid");
            }

            var result = await _jwtService.CreateUserTokenAsync(token.User, isRefresh : true);

            _unitOfWork.SaveChanges();

            return(result);
        }