メニュー
Amazon Cognito
開発者ガイド (Version 最終更新日: 2016 年 7 月 28 日)

Mobile SDK for Android でのユーザープールの使用例

このトピックでは、Mobile SDK for Android を使用した基本的なタスクを実行するコードの例を示します。SDK はネットワーク呼び出しを行うため、すべての API コールはノンアクティビティスレッドから行う必要があります。

CognitoUserPool を作成する

Copy
CognitoUserPool userPool = new CognitoUserPool(context, userPoolId, clientId, clientSecret); // user pool can also be created with client app configuration: CognitoUserPool userPool = new CognitoUserPool(context, userPoolId, clientId, clientSecret, clientConfiguration);

新規ユーザーを登録する

Copy
// create a handler for registration SignUpHandler handler = new SignUpHandler() { @Override public void onSuccess(CognitoUser user, CognitoUserCodeDeliveryDetails codeDeliveryDetails) { // If the sign up was successful, "user" is a CognitoUser object of the user who was signed up. // "codeDeliveryDetails" will contain details about where the confirmation codes will be delivered. } @Override public void onFailure(Exception exception) { // Sign up failed, code check the exception for cause and perform remedial actions. } }

キャッシュされたユーザーを取得する

Copy
CognitoUser user = userPool.getCurrentUser();

UserId でユーザーオブジェクトを作成する

Copy
CognitoUser user = userPool.getUser(userId);

ユーザーの確認

Copy
// create a callback handler for confirm GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // User was successfully confirmed! } @Override public void onFailure(Exception exception) { // Confirmation failed, probe exception for details } } user.confirmSignUp(code, handler);

認証コードのリクエスト

Copy
// create a callback handler for the confirmation code request GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Confirmation code was successfully sent! } @Override public void onFailure(Exception exception) { // Confirmation code request failed, probe exception for details } } user.resendConfirmationCode(handler);

パスワードを忘れた場合: 新しいパスワードを設定するためのコードの取得

Copy
ForgotPasswordHandler handler = new ForgotPasswordHandler() { @Override public void onSuccess() { // Forgot password process completed successfully, new password has been successfully set } @Override public void getResetCode(ForgotPasswordContinuation continuation) { // A code will be sent, use the "continuation" object to continue with the forgot password process // This will indicate where the code was sent String codeSentHere = continuation.getParameters(); // Code to get the code from the user - user dialogs etc. // If the program control has to exit this method, take the "continuation" object. // "continuation" is the only possible way to continue with the process // When the code is available // Set the new password continuation.setPassword(newPassword); // Set the code to verify continuation.setVerificationCode(code); // Let the forgot password process proceed continuation.continueTask(); } /** * This is called for all fatal errors encountered during the password reset process * Probe {@exception} for cause of this failure. * @param exception */ public void onFailure(Exception exception) { // Forgot password processing failed, probe the exception for cause } } user.forgotPassword(handler);

認証ハンドラー: トークンの取得

Copy
// Implement authentication handler, AuthenticationHandler handler = new AuthenticationHandler() { @Override public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) { // Authentication was successful, the "userSession" will have the current valid tokens // Time to do awesome stuff } @Override public void getAuthenticationDetails(final AuthenticationContinuation continuation, final String userID) { // User authentication details, userId and password are required to continue. // Use the "continuation" object to pass the user authentication details // After the user authentication details are available, wrap them in an AuthenticationDetails class // Along with userId and password, parameters for user pools for Lambda can be passed here // The validation parameters "validationParameters" are passed in as a Map<String, String> AuthenticationDetails authDetails = new AuthenticationDetails(userId, password, validationParameters); // Now allow the authentication to continue continuation.setAuthenticationDetails(authDetails); continuation.continueTask(); } @Override public void getMFACode(final MultiFactorAuthenticationContinuation continuation) { // Multi-factor authentication is required to authenticate // A code was sent to the user, use the code to continue with the authentication // Find where the code was sent to String codeSentHere = continuation.getParameter()[0]; // When the verification code is available, continue to authenticate continuation.setMfaCode(code); continuation.continueTask(); } @Override public void authenticationChallenge(final ChallengeContinuation continuation) { // A custom challenge has to be solved to authenticate // Set the challenge responses // Call continueTask() method to respond to the challenge and continue with authentication. } @Override public void onFailure(final Exception exception) { // Authentication failed, probe exception for the cause } }; user.getSession(handler);

ユーザー詳細の取得

Copy
GetDetailsHandler handler = new GetDetailsHandler() { @Override public void onSuccess(final CognitoUserDetails list) { // Successfully retrieved user details } @Override public void onFailure(final Exception exception) { // Failed to retrieve the user details, probe exception for the cause } }; user.getDetails(handler);

属性確認コードの取得

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Attribute verification code was successfully sent! } @Override public void onFailure(Exception exception) { // Attribute verification code request failed, probe exception for details } }; user.getAttibuteVerificationCode(attributeName, handler);

属性の確認

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Attribute verification was successful! } @Override public void onFailure(Exception exception) { // Attribute verification failed, probe exception for details } }; user.verifyAttribute(attributeName, code, handler);

属性の削除

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Attribute deletion was successful! } @Override public void onFailure(Exception exception) { // Attribute deletion failed, probe exception for details } }; user.deleteAttribute(attributeName, handler);

パスワードの変更

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Password change was successful! } @Override public void onFailure(Exception exception) { // Password change failed, probe exception for details } }; user.changePassword(oldPassword, newPassword, handler);

ユーザー設定の変更または設定

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Successfully changed settings! } @Override public void onFailure(Exception exception) { // Change failed, probe exception for details } }; // userSettings is an object of the type CognitoUserSettings, CognitoUserSettings userSettings = new CognitoUserSettings(); // Set the user settings userSettings.setSettings(settingName, settingValue); // Now update the new settings to the Amazon Cognito Identity Provider Service user.setUserSettings(userSettings, handler);

ユーザーの削除

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Delete was successful! } @Override public void onFailure(Exception exception) { // Delete failed, probe exception for details } }; user.deleteUser(handler);

サインアウトしたユーザー

Copy
// This has cleared all tokens and this user will have to go through the authentication process to get tokens. user.signOut();

CognitoUserSession からアクセスと ID のトークンを取得する

Copy
// Session is an object of the type CognitoUserSession String accessToken = session.getAccessToken().getJWT(); String idToken = session.getIdToken().getJWTToken();

ユーザーのすべてのデバイスをリストする

Copy
DevicesHandler devicesHandler = new DevicesHandler() { @Override public void onSuccess(List<CognitoDevice> devices) { // devices will contain a list of all remembered devices } @Override public void onFailure(Exception e) { // List devices failed, probe exception for details } }; user.listDevicesInBackground(10, null, devicesHandler);

デバイスを記憶する

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Successful! } @Override public void onFailure(Exception exception) { // Failed, probe exception for details } }; cognitoDevice.rememberThisDeviceInBackground(handler)

デバイスを記憶しない

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Successful! } @Override public void onFailure(Exception exception) { // Failed, probe exception for details } }; cognitoDevice.doNotRememberThisDeviceInBackground(handler)