/// <summary> /// Sets the current user of the app. /// </summary> /// <param name="response"></param> internal void SetUser(UserResponse response) { if (response == null) throw new ArgumentNullException(nameof(response)); if (string.IsNullOrEmpty(response.AccessToken)) throw new ArgumentException("Access token cannot be blank"); if (string.IsNullOrEmpty(response.RefreshToken)) throw new ArgumentException("Refresh token cannot be blank"); // Store user data Platform.Current.Storage.SaveSetting(CREDENTIAL_USER_KEYNAME, response, ApplicationData.Current.RoamingSettings, SerializerTypes.Json); Platform.Current.Storage.SaveCredential(CREDENTIAL_USER_KEYNAME, CREDENTIAL_ACCESSTOKEN_KEYNAME, response.AccessToken); Platform.Current.Storage.SaveCredential(CREDENTIAL_USER_KEYNAME, CREDENTIAL_REFRESHTOKEN_KEYNAME, response.RefreshToken); // Set properties this.User = response; this.AccessToken = response.AccessToken; this.RefreshToken = response.RefreshToken; // Notify any subscribers that authentication status has changed this.NotifyUserAuthenticated(); }
/// <summary> /// Signs the user out of the application and removes and credential data from storage / credential locker. /// </summary> /// <returns>Awaitable task is returned.</returns> public Task SignoutAsync() { Platform.Current.Storage.SaveSetting(CREDENTIAL_USER_KEYNAME, null, ApplicationData.Current.RoamingSettings, SerializerTypes.Json); Platform.Current.Storage.SaveCredential(CREDENTIAL_USER_KEYNAME, null, null); this.User = null; this.AccessToken = null; this.RefreshToken = null; this.NotifyUserAuthenticated(); return Task.CompletedTask; }
/// <summary> /// Initialization logic which is called on launch of this application. /// </summary> protected internal override void Initialize() { // Retrieve the access token from the credential locker string access_token_value = null; if (Platform.Current.Storage.LoadCredential(CREDENTIAL_USER_KEYNAME, CREDENTIAL_ACCESSTOKEN_KEYNAME, ref access_token_value)) this.AccessToken = access_token_value; // Retrieve the refresh token from the credential locker string refresh_token_value = null; if (Platform.Current.Storage.LoadCredential(CREDENTIAL_USER_KEYNAME, CREDENTIAL_REFRESHTOKEN_KEYNAME, ref refresh_token_value)) this.RefreshToken = refresh_token_value; // Retrieve the user profile data from settings this.User = Platform.Current.Storage.LoadSetting<UserResponse>(CREDENTIAL_USER_KEYNAME, ApplicationData.Current.RoamingSettings, SerializerTypes.Json); if (this.User != null) { this.User.AccessToken = this.AccessToken; this.User.RefreshToken = this.RefreshToken; } // Notify any subscribers that authentication status has changed this.NotifyUserAuthenticated(); base.Initialize(); }