Skip to content

Commit

Permalink
update readme sample
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Jun 19, 2017
1 parent cb650d7 commit 05e9c9f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,33 @@ This library ships with a `CredentialsManager` class to easily store and retriev

### Usage
1. **Instantiate the manager**
You'll need an `AuthenticationAPIClient` instance used to renew the credentials when they expire and a `Storage`. The Storage implementation is up to you. We provide a `SharedPreferencesStorage` that uses `SharedPreferences` to create a file in the application's directory with Context.MODE_PRIVATE mode. This implementation is thread safe and can either be obtained through a Singleton like method or be created every time it's needed.

```java
AuthenticationAPIClient authClient = //create auth client
Storage storage = //create new storage
CredentialsManager manager = new CredentialsManager(authClient, storage);
AuthenticationAPIClient authentication = new AuthenticationAPIClient(account);
Storage storage = new SharedPreferencesStorage(this);
CredentialsManager manager = new CredentialsManager(authentication, storage);
```

2. **Save credentials**
The credentials **must have** `expires_in` and at least an `access_token` or `id_token` value. If one of the values is missing when trying to set the credentials, the method will throw a `CredentialsManagerException`.
The credentials to save **must have** `expires_in` and at least an `access_token` or `id_token` value. If one of the values is missing when trying to set the credentials, the method will throw a `CredentialsManagerException`. If you want the manager to successfully renew the credentials when expired you must also request the `offline_access` scope when logging in in order to receive a `refresh_token` value along with the rest of the tokens. i.e. Logging in with a database connection and saving the credentials:

```java
Credentials credentials = //login to Auth0, i.e. by using the authClient
manager.setCredentials(credentials);
authentication
.login("info@auth0.com", "a secret password", "my-database-connection")
.setScope("openid offline_access")
.start(new BaseCallback<Credentials>() {
@Override
public void onSuccess(Credentials credentials) {
//Save the credentials
manager.saveCredentials(credentials);
}

@Override
public void onFailure(AuthenticationException error) {
//Error!
}
});
```

3. **Retrieve credentials**
Expand All @@ -405,19 +419,15 @@ manager.setCredentials(credentials);
```java
manager.getCredentials(new BaseCallback<Credentials, CredentialsManagerException>(){
public void onSuccess(Credentials credentials){
//success
//Use the Credentials
}

public void onFailure(CredentialsManagerException error){
//error
//Error!
}
});
```

### Customize the Storage

The storage implementation is up to you. We provide a `SharedPreferencesStorage` that uses `SharedPreferences` to create a file in the application directory with Context.MODE_PRIVATE mode.


## FAQ

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import android.support.annotation.Nullable;

/**
* Represents a Storage of generic key-value data.
* Represents a Storage of key-value data.
* Supported classes are String, Long and Integer.
*/
@SuppressWarnings("WeakerAccess")
public interface Storage {
Expand Down

0 comments on commit 05e9c9f

Please sign in to comment.