Ejemplo n.º 1
0
        public async Task <User> LoginAsync()
        {
            TokenModel token = await TokenHelper.RetrieveTokenFromSecureStoreAsync();

            if (token != null)
            {
                if (IsTokenExpired(token.Token))
                {
                    try
                    {
                        RefreshTokenRequest tokenRequest = new RefreshTokenRequest()
                        {
                            RefreshToken = token.RefreshToken,
                            Token        = token.Token
                        };
                        ApiResponse <TokenResponse> response = await _restClient.GetClient <IAuthApi>().RefreshTokenAsync(tokenRequest);

                        if (!response.IsSuccessStatusCode)
                        {
                            throw response.Error;
                        }
                        token.RefreshToken = response.Content.RefreshToken;
                        token.Token        = response.Content.Token;
                        await TokenHelper.StoreTokenInSecureStore(token);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"Could not refresh token: {ex.Message}");
                    }
                }
                return(await _userService.GetUserInfo());
            }
            return(null);
        }
Ejemplo n.º 2
0
        public async Task <User> GetUserInfo()
        {
            ApiResponse <UserInfoResponse> response = await _restClient.GetClient <IUserApi>().GetUserInfoAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw response.Error;
            }

            UserInfoResponse userInfo = response.Content;
            User             user     = new User()
            {
                UserId         = userInfo.Id,
                Email          = userInfo.Email,
                EmailConfirmed = userInfo.EmailConfirmed,
                FirstName      = userInfo.FirstName,
                LastName       = userInfo.LastName
            };

            App.CurrentUser = user;
            return(user);
        }