-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
FirebaseLoginBaseActivity.java
138 lines (112 loc) · 3.86 KB
/
FirebaseLoginBaseActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.firebase.ui.auth.core;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
public abstract class FirebaseLoginBaseActivity extends AppCompatActivity {
private final String TAG = "FirebaseLoginBaseAct";
private Firebase.AuthStateListener mAuthStateListener;
private FirebaseLoginDialog mDialog;
private TokenAuthHandler mHandler;
/**
* Subclasses of this activity may implement this method to handle when a user logs in.
*
* @return void
*/
protected abstract void onFirebaseLoggedIn(AuthData authData);
/**
* Subclasses of this activity may implement this method to handle when a user logs out.
*
* @return void
*/
protected abstract void onFirebaseLoggedOut();
/**
* Subclasses of this activity may implement this method to handle any potential provider errors
* like OAuth or other internal errors.
*
* @return void
*/
protected abstract void onFirebaseLoginProviderError(FirebaseLoginError firebaseError);
/**
* Subclasses of this activity may implement this method to handle any potential user errors
* like entering an incorrect password or closing the login dialog.
*
* @return void
*/
protected abstract void onFirebaseLoginUserError(FirebaseLoginError firebaseError);
/**
* Subclasses of this activity must implement this method and return a valid Firebase reference that
* can be used to call authentication related methods on.
*
* @return a Firebase reference that can be used to call authentication related methods on
*/
protected abstract Firebase getFirebaseRef();
public void logout() {
mDialog.logout();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mDialog.onActivityResult(requestCode, resultCode, data);
}
public void showFirebaseLoginPrompt() {
mDialog.show(getFragmentManager(), "");
}
public void dismissFirebaseLoginPrompt() {
mDialog.dismiss();
}
public void resetFirebaseLoginDialog() {
mDialog.reset();
}
public void setEnabledAuthProvider(SocialProvider provider) {
mDialog.setEnabledProvider(provider);
}
@Override
protected void onStart() {
super.onStart();
mHandler = new TokenAuthHandler() {
@Override
public void onSuccess(AuthData data) {
/* onFirebaseLoginSuccess is called by the AuthStateListener below */
}
@Override
public void onUserError(FirebaseLoginError err) {
onFirebaseLoginUserError(err);
}
@Override
public void onProviderError(FirebaseLoginError err) {
onFirebaseLoginProviderError(err);
}
};
mAuthStateListener = new Firebase.AuthStateListener() {
@Override
public void onAuthStateChanged(AuthData authData) {
if (authData != null) {
onFirebaseLoggedIn(authData);
} else {
onFirebaseLoggedOut();
}
}
};
mDialog = new FirebaseLoginDialog();
mDialog
.setContext(this)
.setRef(getFirebaseRef())
.setHandler(mHandler);
getFirebaseRef().addAuthStateListener(mAuthStateListener);
}
protected void onStop() {
super.onStop();
getFirebaseRef().removeAuthStateListener(mAuthStateListener);
mDialog.cleanUp();
}
@Override
protected void onPause() {
super.onPause();
mDialog.cleanUp();
}
@Override
protected void onDestroy() {
super.onDestroy();
mDialog.cleanUp();
}
}