Android SDK
How to use authgear android SDK
Setup Application in Authgear
Signup for an account in https://portal.authgearapps.com/ and create a project. Or you can use your self-deployed Authgear.
After that, we will need to create an application in Authgear.
Create an application
Go to "Applications".
Click "Add Application" in the top right corner
Input name of your application, this is for reference only
Defining a custom scheme that the users will be redirected back to your app after they have authenticated with Authgear. Add the URI to "Redirect URIs". (e.g. com.myapp://host/path).
Click "Save" and keep the client id. You can also obtain the client id from the list later.
oauth:
clients:
- name: your_app_name
client_id: a_random_generated_string
redirect_uris:
- "com.myapp://host/path"
grant_types:
- authorization_code
- refresh_token
response_types:
- code
- noneGet the SDK
Add
jitpackrepository togradleallprojects { repositories { // Other repository maven { url 'https://jitpack.io' } } }Add authgear in dependencies. Use
$branch-SNAPSHOT(e.g.main-SNAPSHOT) for the latest version in a branch or a release tag/git commit hash of the desired version.dependencies { // Other implementations implementation 'com.github.authgear:authgear-sdk-android:SNAPSHOT' }
Setup Redirect URI for Your Android App
Add the following activity entry to the AndroidManifest.xml of your app. The intent system would dispatch the redirect URI to OauthRedirectActivity and the SDK would handle the rest.
Initialize Authgear
Add the following code to your app's Application class. If there is none, add a class that extends Application. Make sure it is declared in AndroidManifest.xml's application tag with the name attribute as described here.
Authorize a user
Add the following code to your view model. Do NOT call these codes in activity as this can lead to memory leak when your activity instance is destroyed. You can read more on the view model in the official documentation here.
The above call of authorize passes in the exact redirect URI as configured in the applications and manifest, the callback then indicates authorization success or failure. By default, the callback is called on the main thread.
Using the Access Token in HTTP Requests
Call refreshAccessTokenIfNeeded every time before using the access token, the function will check and make the network call only if the access token has expired. Include the access token into the Authorization header of your application request. If you are using OKHttp in your project, you can also use the interceptor extension provided by the SDK, see detail.
Logout
To log out the user from the current app session, you need to invoke thelogoutfunction.
Next steps
To protect your application server from unauthorized access. You will need to integrate your backend with Authgear.
Backend IntegrationLast updated
Was this helpful?