Auth0.Androidのログイン、ログアウト、およびユーザープロファイル

Androidアプリケーションにログインを追加する

WebAuthProvider.loginメソッドを使用して、ユーザーをログインさせることができます。

WebAuthProvider.login(account)
  .withScheme(getString(R.string.com_auth0_scheme))
  .withScope("openid profile email")
  .start(this, object : Callback<Credentials, AuthenticationException> {
    override fun onFailure(exception: AuthenticationException) {
       // Authentication failed
     }

      override fun onSuccess(credentials: Credentials) {
         // Authentication succeeded
       }
  })

Was this helpful?

/

認証結果はonSuccessコールバックに配信されます。

WebAuthProviderクラスのその他のオプションについては、「Auth0.Androidの構成」を参照してください。

Androidアプリケーションにログアウトを追加する

ユーザーをログアウトさせるには、WebAuthProvider.logoutメソッドを呼び出します。ログアウト結果はonSuccessコールバックに配信されます。

この方法では、認証時にブラウザが設定したCookieが削除されるため、ユーザーは次回認証を行うときに資格情報を再入力する必要があります。

WebAuthProvider.logout(account)
  .withScheme("demo")
  .start(this, object: Callback<Void?, AuthenticationException> {
    override fun onSuccess(payload: Void?) {
      // The user has been logged out!
    }

    override fun onFailure(error: AuthenticationException) {
      // Something went wrong!
    }
  })

Was this helpful?

/

ユーザープロファイルを表示する

AuthenticationAPIClientクラスを使用して、Auth0からユーザーのプロファイルを取得します。これには以下のものが必要です。

  • ログインフェーズで受信したアクセストークン

  • WebAuthProvider.loginが呼び出されたときを含むためのprofileスコープ

  • emailスコープ(ユーザーのメールアドレスを取得したい場合)

このサンプルはユーザープロファイルを取得して画面に表示する機能のデモです。

var client = AuthenticationAPIClient(account)

// Use the received access token to call `userInfo` and get the profile from Auth0.
client.userInfo(accessToken)
  .start(object : Callback<UserProfile, AuthenticationException> {
      override fun onFailure(exception: AuthenticationException) {
          // Something went wrong!
      }

      override fun onSuccess(profile: UserProfile) {
        // We have the user's profile!
        val email = profile.email
        val name = profile.name
      }
})

Was this helpful?

/