Пример #1
0
 public Task SignOutAsync()
 {
     _userContext.SignedInUser = null;
     _tokenManager.RemoveTokenAsync();
     AuthenticationStateUpdated?.Invoke(null, EventArgs.Empty);
     return(Task.CompletedTask);
 }
Пример #2
0
        public async Task SignInAsync(JwtTokenDto tokenDto)
        {
            _userContext.SignedInUser = new SignedInUser(tokenDto.Token);
            await _tokenManager.SaveTokenAsync(tokenDto);

            AuthenticationStateUpdated?.Invoke(null, EventArgs.Empty);
        }
Пример #3
0
        public async Task <bool> IsUserSignedInAsync()
        {
            if (_userContext.SignedInUser is not null)
            {
                if (_userContext.SignedInUser.Expiry > DateTime.Now)
                {
                    return(true);
                }
                await SignOutAsync();

                return(false);
            }

            var token = await _tokenManager.GetTokenAsync();

            if (token is not null)
            {
                _userContext.SignedInUser = new SignedInUser(token.Token);
                if (_userContext.SignedInUser.Expiry < DateTime.Now)
                {
                    var newToken = await _authenticationProxy.RefreshAsync(token.Refresh, _userContext.SignedInUser.Email);

                    if (newToken is null)
                    {
                        Console.WriteLine("Failed to use refresh token");
                        _userContext.SignedInUser = null;
                        await _tokenManager.RemoveTokenAsync();

                        AuthenticationStateUpdated?.Invoke(null, EventArgs.Empty);
                        return(false);
                    }

                    _userContext.SignedInUser = new SignedInUser(token.Token);
                    await _tokenManager.SaveTokenAsync(token);
                }
                AuthenticationStateUpdated?.Invoke(null, EventArgs.Empty);
                return(true);
            }

            return(false);
        }