|
| 1 | +/** |
| 2 | + * Copyright Google Inc. All Rights Reserved. |
| 3 | + * <p/> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p/> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p/> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.eusecom.attendance; |
| 17 | + |
| 18 | +import android.os.Bundle; |
| 19 | +import android.support.annotation.NonNull; |
| 20 | +import android.support.v7.app.AppCompatActivity; |
| 21 | +import android.util.Log; |
| 22 | +import android.view.View; |
| 23 | +import android.widget.TextView; |
| 24 | +import android.widget.Toast; |
| 25 | + |
| 26 | +import com.google.android.gms.tasks.OnCompleteListener; |
| 27 | +import com.google.android.gms.tasks.OnSuccessListener; |
| 28 | +import com.google.android.gms.tasks.Task; |
| 29 | +import com.google.firebase.auth.AuthResult; |
| 30 | +import com.google.firebase.auth.FirebaseAuth; |
| 31 | +import com.google.firebase.auth.FirebaseUser; |
| 32 | + |
| 33 | +/** |
| 34 | + * Demonstrate Firebase Authentication using a custom minted token. For more information, see: |
| 35 | + * https://firebase.google.com/docs/auth/android/custom-auth |
| 36 | + */ |
| 37 | +public class CustomAuthActivity extends AppCompatActivity implements View.OnClickListener { |
| 38 | + |
| 39 | + private static final String TAG = "CustomAuthActivity"; |
| 40 | + |
| 41 | + // [START declare_auth] |
| 42 | + private FirebaseAuth mAuth; |
| 43 | + // [END declare_auth] |
| 44 | + |
| 45 | + // [START declare_auth_listener] |
| 46 | + private FirebaseAuth.AuthStateListener mAuthListener; |
| 47 | + // [END declare_auth_listener] |
| 48 | + |
| 49 | + private String mCustomToken; |
| 50 | + private TokenBroadcastReceiver mTokenReceiver; |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void onCreate(Bundle savedInstanceState) { |
| 54 | + super.onCreate(savedInstanceState); |
| 55 | + setContentView(R.layout.activity_custom); |
| 56 | + |
| 57 | + // Button click listeners |
| 58 | + findViewById(R.id.button_sign_in).setOnClickListener(this); |
| 59 | + |
| 60 | + // Create token receiver (for demo purposes only) |
| 61 | + mTokenReceiver = new TokenBroadcastReceiver() { |
| 62 | + @Override |
| 63 | + public void onNewToken(String token) { |
| 64 | + Log.d(TAG, "onNewToken:" + token); |
| 65 | + setCustomToken(token); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + // [START initialize_auth] |
| 70 | + mAuth = FirebaseAuth.getInstance(); |
| 71 | + // [END initialize_auth] |
| 72 | + |
| 73 | + // [START auth_state_listener] |
| 74 | + mAuthListener = new FirebaseAuth.AuthStateListener() { |
| 75 | + @Override |
| 76 | + public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { |
| 77 | + FirebaseUser user = firebaseAuth.getCurrentUser(); |
| 78 | + if (user != null) { |
| 79 | + // User is signed in |
| 80 | + Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); |
| 81 | + } else { |
| 82 | + // User is signed out |
| 83 | + Log.d(TAG, "onAuthStateChanged:signed_out"); |
| 84 | + } |
| 85 | + // [START_EXCLUDE] |
| 86 | + updateUI(user); |
| 87 | + // [END_EXCLUDE] |
| 88 | + } |
| 89 | + }; |
| 90 | + // [END auth_state_listener] |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + }//end of oncreate |
| 95 | + |
| 96 | + // [START on_start_add_listener] |
| 97 | + @Override |
| 98 | + public void onStart() { |
| 99 | + super.onStart(); |
| 100 | + mAuth.addAuthStateListener(mAuthListener); |
| 101 | + // [START_EXCLUDE] |
| 102 | + registerReceiver(mTokenReceiver, TokenBroadcastReceiver.getFilter()); |
| 103 | + // [END_EXCLUDE] |
| 104 | + } |
| 105 | + // [END on_start_add_listener] |
| 106 | + |
| 107 | + // [START on_stop_remove_listener] |
| 108 | + @Override |
| 109 | + public void onStop() { |
| 110 | + super.onStop(); |
| 111 | + if (mAuthListener != null) { |
| 112 | + mAuth.removeAuthStateListener(mAuthListener); |
| 113 | + } |
| 114 | + // [START_EXCLUDE] |
| 115 | + unregisterReceiver(mTokenReceiver); |
| 116 | + // [END_EXCLUDE] |
| 117 | + } |
| 118 | + // [END on_stop_remove_listener] |
| 119 | + |
| 120 | + private void startSignIn() { |
| 121 | + // Initiate sign in with custom token |
| 122 | + // [START sign_in_custom] |
| 123 | + |
| 124 | + mCustomToken="ya29.ElsRBCFaBUX0npVERVcttGZUQJaJ3Ycy8ONDUjwbFZkn4WQkkKg-_Dtnbd9_1W1zj0I7f-eyWtCarTf0uzYsh3JsHT7wcpsgvwEPrUpCjqPzYXFTrnGb6isShIcT"; |
| 125 | + |
| 126 | + mAuth.signInWithCustomToken(mCustomToken) |
| 127 | + .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { |
| 128 | + @Override |
| 129 | + public void onComplete(@NonNull Task<AuthResult> task) { |
| 130 | + Log.d(TAG, "signInWithCustomToken:onComplete:" + task.isSuccessful()); |
| 131 | + |
| 132 | + // If sign in fails, display a message to the user. If sign in succeeds |
| 133 | + // the auth state listener will be notified and logic to handle the |
| 134 | + // signed in user can be handled in the listener. |
| 135 | + if (!task.isSuccessful()) { |
| 136 | + Log.w(TAG, "signInWithCustomToken", task.getException()); |
| 137 | + Toast.makeText(CustomAuthActivity.this, "Authentication failed.", |
| 138 | + Toast.LENGTH_SHORT).show(); |
| 139 | + } |
| 140 | + } |
| 141 | + }); |
| 142 | + // [END sign_in_custom] |
| 143 | + } |
| 144 | + |
| 145 | + private void updateUI(FirebaseUser user) { |
| 146 | + if (user != null) { |
| 147 | + ((TextView) findViewById(R.id.text_sign_in_status)).setText( |
| 148 | + "User ID: " + user.getUid()); |
| 149 | + } else { |
| 150 | + ((TextView) findViewById(R.id.text_sign_in_status)).setText( |
| 151 | + "Error: sign in failed."); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private void setCustomToken(String token) { |
| 156 | + mCustomToken = token; |
| 157 | + |
| 158 | + String status; |
| 159 | + if (mCustomToken != null) { |
| 160 | + status = "Token:" + mCustomToken; |
| 161 | + } else { |
| 162 | + status = "Token: null"; |
| 163 | + } |
| 164 | + |
| 165 | + // Enable/disable sign-in button and show the token |
| 166 | + findViewById(R.id.button_sign_in).setEnabled((mCustomToken != null)); |
| 167 | + ((TextView) findViewById(R.id.text_token_status)).setText(status); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void onClick(View v) { |
| 172 | + int i = v.getId(); |
| 173 | + if (i == R.id.button_sign_in) { |
| 174 | + startSignIn(); |
| 175 | + |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments