Skip to content

Commit 9ae131a

Browse files
amandlesamtstern
authored andcommitted
Add reauthentication (#583)
1 parent 63df29c commit 9ae131a

File tree

14 files changed

+406
-84
lines changed

14 files changed

+406
-84
lines changed

app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,15 @@ public class AuthUiActivity extends AppCompatActivity {
126126
@Override
127127
public void onCreate(Bundle savedInstanceState) {
128128
super.onCreate(savedInstanceState);
129+
setContentView(R.layout.auth_ui_layout);
130+
ButterKnife.bind(this);
129131

130132
FirebaseAuth auth = FirebaseAuth.getInstance();
131133
if (auth.getCurrentUser() != null) {
132-
startActivity(SignedInActivity.createIntent(this, null));
134+
startSignedInActivity(null);
133135
finish();
134136
}
135137

136-
setContentView(R.layout.auth_ui_layout);
137-
ButterKnife.bind(this);
138-
139138
if (!isGoogleConfigured()) {
140139
mUseGoogleProvider.setChecked(false);
141140
mUseGoogleProvider.setEnabled(false);
@@ -208,7 +207,7 @@ private void handleSignInResponse(int resultCode, Intent data) {
208207

209208
// Successfully signed in
210209
if (resultCode == ResultCodes.OK) {
211-
startActivity(SignedInActivity.createIntent(this, response));
210+
startSignedInActivity(response);
212211
finish();
213212
return;
214213
} else {
@@ -233,6 +232,19 @@ private void handleSignInResponse(int resultCode, Intent data) {
233232
showSnackbar(R.string.unknown_sign_in_response);
234233
}
235234

235+
private void startSignedInActivity(IdpResponse response) {
236+
startActivity(
237+
SignedInActivity.createIntent(
238+
this,
239+
response,
240+
new SignedInActivity.SignedInConfig(
241+
getSelectedLogo(),
242+
getSelectedTheme(),
243+
getSelectedProviders(),
244+
getSelectedTosUrl(),
245+
mEnableSmartLock.isChecked())));
246+
}
247+
236248
@MainThread
237249
private void setGoogleScopesEnabled(boolean enabled) {
238250
mGoogleScopesLabel.setEnabled(enabled);

app/src/main/java/com/firebase/uidemo/auth/SignedInActivity.java

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import android.content.DialogInterface;
1919
import android.content.Intent;
2020
import android.os.Bundle;
21+
import android.os.Parcel;
22+
import android.os.Parcelable;
2123
import android.support.annotation.MainThread;
2224
import android.support.annotation.NonNull;
2325
import android.support.annotation.StringRes;
@@ -31,6 +33,7 @@
3133

3234
import com.bumptech.glide.Glide;
3335
import com.firebase.ui.auth.AuthUI;
36+
import com.firebase.ui.auth.AuthUI.IdpConfig;
3437
import com.firebase.ui.auth.IdpResponse;
3538
import com.firebase.uidemo.R;
3639
import com.google.android.gms.tasks.OnCompleteListener;
@@ -41,13 +44,19 @@
4144
import com.google.firebase.auth.FirebaseUser;
4245
import com.google.firebase.auth.GoogleAuthProvider;
4346

47+
import java.util.ArrayList;
4448
import java.util.Iterator;
4549

4650
import butterknife.BindView;
4751
import butterknife.ButterKnife;
4852
import butterknife.OnClick;
53+
import java.util.List;
4954

5055
public class SignedInActivity extends AppCompatActivity {
56+
private static final String EXTRA_SIGNED_IN_CONFIG = "extra_signed_in_config";
57+
58+
private static final int RC_REAUTH = 100;
59+
5160
@BindView(android.R.id.content)
5261
View mRootView;
5362

@@ -65,6 +74,8 @@ public class SignedInActivity extends AppCompatActivity {
6574

6675
private IdpResponse mIdpResponse;
6776

77+
private SignedInConfig mSignedInConfig;
78+
6879
@Override
6980
public void onCreate(Bundle savedInstanceState) {
7081
super.onCreate(savedInstanceState);
@@ -77,6 +88,7 @@ public void onCreate(Bundle savedInstanceState) {
7788
}
7889

7990
mIdpResponse = IdpResponse.fromResultIntent(getIntent());
91+
mSignedInConfig = getIntent().getParcelableExtra(EXTRA_SIGNED_IN_CONFIG);
8092

8193
setContentView(R.layout.signed_in_layout);
8294
ButterKnife.bind(this);
@@ -101,6 +113,21 @@ public void onComplete(@NonNull Task<Void> task) {
101113
});
102114
}
103115

116+
@OnClick(R.id.reauthenticate)
117+
public void reauthenticate() {
118+
Intent reauthIntent = AuthUI.getInstance()
119+
.createReauthIntentBuilder()
120+
.setProviders(mSignedInConfig.providerInfo)
121+
.setIsSmartLockEnabled(mSignedInConfig.isSmartLockEnabled)
122+
.setLogo(mSignedInConfig.logo)
123+
.setTheme(mSignedInConfig.theme)
124+
.setTosUrl(mSignedInConfig.tosUrl)
125+
.setReauthReason(getString(R.string.reauthentication_reason))
126+
.build();
127+
128+
startActivityForResult(reauthIntent, RC_REAUTH);
129+
}
130+
104131
@OnClick(R.id.delete_account)
105132
public void deleteAccountClicked() {
106133

@@ -185,14 +212,18 @@ private void populateIdpToken() {
185212
token = mIdpResponse.getIdpToken();
186213
secret = mIdpResponse.getIdpSecret();
187214
}
215+
View idpTokenLayout = findViewById(R.id.idp_token_layout);
188216
if (token == null) {
189-
findViewById(R.id.idp_token_layout).setVisibility(View.GONE);
217+
idpTokenLayout.setVisibility(View.GONE);
190218
} else {
219+
idpTokenLayout.setVisibility(View.VISIBLE);
191220
((TextView) findViewById(R.id.idp_token)).setText(token);
192221
}
222+
View idpSecretLayout = findViewById(R.id.idp_secret_layout);
193223
if (secret == null) {
194-
findViewById(R.id.idp_secret_layout).setVisibility(View.GONE);
224+
idpSecretLayout.setVisibility(View.GONE);
195225
} else {
226+
idpSecretLayout.setVisibility(View.VISIBLE);
196227
((TextView) findViewById(R.id.idp_secret)).setText(secret);
197228
}
198229
}
@@ -203,9 +234,79 @@ private void showSnackbar(@StringRes int errorMessageRes) {
203234
.show();
204235
}
205236

206-
public static Intent createIntent(Context context, IdpResponse idpResponse) {
237+
static final class SignedInConfig implements Parcelable {
238+
int logo;
239+
int theme;
240+
List<IdpConfig> providerInfo;
241+
String tosUrl;
242+
boolean isSmartLockEnabled;
243+
244+
SignedInConfig(
245+
int logo,
246+
int theme,
247+
List<IdpConfig> providerInfo,
248+
String tosUrl,
249+
boolean isSmartLockEnabled) {
250+
this.logo = logo;
251+
this.theme = theme;
252+
this.providerInfo = providerInfo;
253+
this.tosUrl = tosUrl;
254+
this.isSmartLockEnabled = isSmartLockEnabled;
255+
}
256+
257+
SignedInConfig(Parcel in) {
258+
logo = in.readInt();
259+
theme = in.readInt();
260+
providerInfo = new ArrayList<>();
261+
in.readList(providerInfo, IdpConfig.class.getClassLoader());
262+
tosUrl = in.readString();
263+
isSmartLockEnabled = in.readInt() != 0;
264+
}
265+
266+
public static final Creator<SignedInConfig> CREATOR = new Creator<SignedInConfig>() {
267+
@Override
268+
public SignedInConfig createFromParcel(Parcel in) {
269+
return new SignedInConfig(in);
270+
}
271+
272+
@Override
273+
public SignedInConfig[] newArray(int size) {
274+
return new SignedInConfig[size];
275+
}
276+
};
277+
278+
@Override
279+
public int describeContents() {
280+
return 0;
281+
}
282+
283+
@Override
284+
public void writeToParcel(Parcel dest, int flags) {
285+
dest.writeInt(logo);
286+
dest.writeInt(theme);
287+
dest.writeList(providerInfo);
288+
dest.writeString(tosUrl);
289+
dest.writeInt(isSmartLockEnabled ? 1 : 0);
290+
}
291+
}
292+
293+
public static Intent createIntent(
294+
Context context,
295+
IdpResponse idpResponse,
296+
SignedInConfig signedInConfig) {
207297
Intent in = IdpResponse.getIntent(idpResponse);
208298
in.setClass(context, SignedInActivity.class);
299+
in.putExtra(EXTRA_SIGNED_IN_CONFIG, signedInConfig);
209300
return in;
210301
}
302+
303+
@Override
304+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
305+
super.onActivityResult(requestCode, resultCode, data);
306+
if (requestCode == RC_REAUTH) {
307+
mIdpResponse = IdpResponse.fromResultIntent(data);
308+
populateIdpToken();
309+
populateProfile();
310+
}
311+
}
211312
}

app/src/main/res/layout/signed_in_layout.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
android:layout_margin="16dp"
4141
android:text="@string/sign_out"/>
4242

43+
<Button
44+
android:id="@+id/reauthenticate"
45+
style="@style/Widget.AppCompat.Button.Colored"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:layout_gravity="center"
49+
android:layout_margin="16dp"
50+
android:text="@string/reauthenticate"/>
51+
4352
<Button
4453
android:id="@+id/delete_account"
4554
android:layout_width="wrap_content"

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
<string name="accessibility_downloaded_image">Downloaded image</string>
6565
<string name="drive_file">Drive File</string>
6666
<string name="allow_new_email_acccount">Allow account creation if email does not exist.</string>
67+
<string name="reauthenticate">Reauth</string>
68+
<string name="reauthentication_reason">Reauth was requested from the test app. Please login to continue.</string>
6769

6870
<!-- strings for database demo activities -->
6971
<string name="start_chatting">No messages. Start chatting at the bottom!</string>

0 commit comments

Comments
 (0)