Пример #1
0
        public async Task <AuthorizedViewModel> Authorize(AuthorizingViewModel model)
        {
            var user = await _userDbService.GetOneByEmail(model.Email);

            var result = await _userDbService.CheckPasswordSignIn(user, model.Password);

            if (result.Succeeded)
            {
                var tokens = _jwtGenerator.CreateTokens(user.UserName);

                await _authDbService.CreateToken(new AuthTokenModel()
                {
                    UserId      = user.Id,
                    AuthJti     = tokens.AuthToken.Jti,
                    RefreshJti  = tokens.RefreshToken.Jti,
                    DateCreated = tokens.DateCreated
                });

                return(new AuthorizedViewModel
                {
                    Name = user.UserName,
                    Token = tokens.AuthToken.Token,
                    RefreshToken = tokens.RefreshToken.Token
                });
            }

            throw new Exception($"Пользователь {model.Email} не авторизован");
        }