示例#1
0
文件: Login.cs 项目: paxbun/MyProject
        public async Task <DataResult <LoginResultView> > Handle(LoginQuery request, CancellationToken cancellationToken)
        {
            if (request.Ip is null)
            {
                return(request.MakeFailure());
            }

            var user = await _dbContext.Set <User>().FirstOrDefaultAsync(user => user.Username == request.Username, cancellationToken);

            if (user is null)
            {
                return(request.MakeFailure());
            }

            if (!user.VerifyPassword(request.Password))
            {
                return(request.MakeFailure());
            }

            var identity = UserIdentity.FromUser(user, request.Ip);

            await _logger.LogInPlaceOfResponseAsync(
                request, DataResult <UserIdentity> .MakeSuccess(identity), cancellationToken);

            return(request.MakeSuccess(new LoginResultView
            {
                AccessToken = _identityService.GenerateToken(identity, TokenType.AccessToken),
                RefreshToken = _identityService.GenerateToken(identity, TokenType.RefreshToken),
            }));
        }