Skip to content

Instantly share code, notes, and snippets.

@Viswanathantv
Created September 24, 2014 05:17
Show Gist options
  • Save Viswanathantv/e0fae827884d3b5ff4b0 to your computer and use it in GitHub Desktop.
Save Viswanathantv/e0fae827884d3b5ff4b0 to your computer and use it in GitHub Desktop.
auth0 linez
Mainactivity.java
static final String ClientID = "OXLAh5px6k1X1iryA6OzrdhAABYz1625";
static final String Tenant = "linez";
static final String Callback = "http://YOUR_APP/callback";
static final String Connection = "google-oauth2"; //change to "paypal", "linkedin", etc or leave empty to show the widget
All functionality is encapsulated in the AuthenticationActivity class. Calling it requires just a couple lines of code:
Intent authActivity = new Intent(MainActivity.this,
com.auth0.sdk.auth0sample.AuthenticationActivity.class);
AuthenticationActivitySetup setup;
//setup = new AuthenticationActivitySetup(Tenant, ClientID, Callback, Connection);
setup = new AuthenticationActivitySetup(Tenant, ClientID, Callback);
authActivity.putExtra(AuthenticationActivity.AUTHENTICATION_SETUP, setup);
startActivityForResult(authActivity, AuthenticationActivity.AUTH_REQUEST_COMPLETE);
AuthenticationActivity is an Android Activity and responses are handled with the standard onActivityResult method. If authentication is successful, resultCode will be equal to RESULT_OK, and an object of type AuthenticationActivityResult will be available. It contains two properties: access_token and JsonWebToken:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent authActivityResult) {
super.onActivityResult(requestCode, resultCode, authActivityResult);
switch(requestCode)
{
case AuthenticationActivity.AUTH_REQUEST_COMPLETE:
if(resultCode==RESULT_OK)
{
AuthenticationActivityResult result;
result = (AuthenticationActivityResult) authActivityResult.getSerializableExtra(AuthenticationActivity.AUTHENTICATION_RESULT);
// result have two properties `accessToken` and `JsonWebToken`. You can use the `accessToken` to call the Auth0 API and retrieve the profile of the user that just logged in
// the `JsonWebToken` is a signed JSON that can be sent to your APIs
String userInfoUrl = String.format("https://linez.auth0.com/userinfo?access_token=%s", result.accessToken);
new AsyncTask<String, Void, JSONObject>() {
@Override
protected JSONObject doInBackground(String... url) {
JSONObject json = RestJsonClient.connect(url[0]);
return json;
}
@Override
protected void onPostExecute(JSONObject user) {
try {
((TextView) findViewById(R.id.user)).setText(user.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
}.execute(userInfoUrl);
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment