Пример #1
0
        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),
            }));
        }