1+ package io .fullstack .firestack ;
2+
3+ import android .content .Context ;
4+
5+ import android .content .Context ;
6+ import android .support .annotation .NonNull ;
7+ import android .widget .Toast ;
8+
9+ import com .facebook .react .bridge .Arguments ;
10+ import com .facebook .react .bridge .ReactApplicationContext ;
11+ import com .facebook .react .bridge .ReactContextBaseJavaModule ;
12+ import com .facebook .react .bridge .ReactMethod ;
13+ import com .facebook .react .bridge .Callback ;
14+ import com .facebook .react .bridge .WritableMap ;
15+ import com .facebook .react .modules .core .DeviceEventManagerModule ;
16+ import com .facebook .react .bridge .ReactContext ;
17+
18+ import com .google .android .gms .tasks .OnCompleteListener ;
19+ import com .google .android .gms .tasks .OnFailureListener ;
20+ import com .google .android .gms .tasks .Task ;
21+ import com .google .firebase .auth .AuthCredential ;
22+ import com .google .firebase .auth .AuthResult ;
23+ import com .google .firebase .auth .FacebookAuthProvider ;
24+ import com .google .firebase .auth .FirebaseAuth ;
25+ import com .google .firebase .auth .FirebaseUser ;
26+ import com .google .firebase .auth .GetTokenResult ;
27+ import com .google .firebase .auth .GoogleAuthProvider ;
28+
29+ class FirestackModule extends ReactContextBaseJavaModule {
30+ private Context context ;
31+ private FirebaseAuth mAuth ;
32+ private FirebaseUser user ;
33+ private FirebaseAuth .AuthStateListener mAuthListener ;
34+
35+ public FirestackModule (ReactApplicationContext reactContext ) {
36+ super (reactContext );
37+ this .context = reactContext ;
38+ }
39+
40+ @ Override
41+ public String getName () {
42+ return "Firestack" ;
43+ }
44+
45+ @ ReactMethod
46+ public void listenForAuth () {
47+ mAuthListener = new FirebaseAuth .AuthStateListener () {
48+ ReactContext mCtx = getReactApplicationContext ();
49+
50+ @ Override
51+ public void onAuthStateChanged (@ NonNull FirebaseAuth firebaseAuth ) {
52+ WritableMap msgMap = Arguments .createMap ();
53+ msgMap .putString ("eventName" , "listenForAuth" );
54+
55+ FirebaseUser user = firebaseAuth .getCurrentUser ();
56+ if (user != null ) {
57+ WritableMap userMap = getUserMap ();
58+
59+ msgMap .putBoolean ("authenticated" , true );
60+ msgMap .putMap ("user" , userMap );
61+
62+ sendEvent (mCtx , "listenForAuth" , msgMap );
63+ } else {
64+ msgMap .putBoolean ("authenticated" , false );
65+ sendEvent (mCtx , "listenForAuth" , msgMap );
66+ }
67+ }
68+ };
69+ }
70+
71+ @ ReactMethod
72+ public void createUserWithEmailAndPassword (final String email , final String password , final Callback onSuccess , final Callback onFail ) {
73+ mAuth = FirebaseAuth .getInstance ();
74+
75+ mAuth .createUserWithEmailAndPassword (email , password )
76+ .addOnCompleteListener (new OnCompleteListener <AuthResult >() {
77+ @ Override
78+ public void onComplete (@ NonNull Task <AuthResult > task ) {
79+ if (task .isSuccessful ()) {
80+ user = task .getResult ().getUser ();
81+ userCallback (onSuccess );
82+ }else {
83+ userErrorCallback (task , onFail );
84+ }
85+ }
86+ });
87+ }
88+
89+ @ ReactMethod
90+ public void signInWithEmailAndPassword (final String email , final String password , final Callback onSuccess , final Callback onFail ) {
91+ mAuth = FirebaseAuth .getInstance ();
92+
93+ mAuth .signInWithEmailAndPassword (email , password )
94+ .addOnCompleteListener (new OnCompleteListener <AuthResult >() {
95+ @ Override
96+ public void onComplete (@ NonNull Task <AuthResult > task ) {
97+ if (task .isSuccessful ()) {
98+ user = task .getResult ().getUser ();
99+ userCallback (onSuccess );
100+ } else {
101+ userErrorCallback (task , onFail );
102+ }
103+ }
104+ });
105+ }
106+
107+ @ ReactMethod
108+ public void getCurrentUser (final Callback onSuccess , final Callback onFail ) {
109+ mAuth = FirebaseAuth .getInstance ();
110+
111+ user = mAuth .getCurrentUser ();
112+ if (user == null ){
113+ onFail .invoke ("not logged in" );
114+ }else {
115+ userCallback (onSuccess );
116+ }
117+ }
118+
119+ @ ReactMethod
120+ public void sendPasswordResetEmail (String email , final Callback onSuccess , final Callback onFail ) {
121+ mAuth = FirebaseAuth .getInstance ();
122+
123+ mAuth .sendPasswordResetEmail (email )
124+ .addOnCompleteListener (new OnCompleteListener <Void >() {
125+ @ Override
126+ public void onComplete (@ NonNull Task <Void > task ) {
127+ if (task .isSuccessful ()){
128+ onSuccess .invoke ("complete" );
129+ }else {
130+ onFail .invoke (task .getException ().toString ());
131+ }
132+ }
133+ });
134+ }
135+
136+ @ ReactMethod
137+ public void googleLogin (String IdToken , final Callback onSuccess , final Callback onFail ) {
138+ mAuth = FirebaseAuth .getInstance ();
139+
140+ AuthCredential credential = GoogleAuthProvider .getCredential (IdToken , null );
141+ mAuth .signInWithCredential (credential )
142+ .addOnCompleteListener (new OnCompleteListener <AuthResult >() {
143+ @ Override
144+ public void onComplete (@ NonNull Task <AuthResult > task ) {
145+ if (task .isSuccessful ()) {
146+ user = task .getResult ().getUser ();
147+ userCallback (onSuccess );
148+ }else {
149+ userErrorCallback (task , onFail );
150+ }
151+ }
152+ });
153+ }
154+
155+ @ ReactMethod
156+ public void facebookLogin (String Token , final Callback onSuccess , final Callback onFail ) {
157+ mAuth = FirebaseAuth .getInstance ();
158+
159+ AuthCredential credential = FacebookAuthProvider .getCredential (Token );
160+ mAuth .signInWithCredential (credential )
161+ .addOnCompleteListener (new OnCompleteListener <AuthResult >() {
162+ @ Override
163+ public void onComplete (@ NonNull Task <AuthResult > task ) {
164+ if (task .isSuccessful ()) {
165+ user = task .getResult ().getUser ();
166+ userCallback (onSuccess );
167+ }else {
168+ userErrorCallback (task , onFail );
169+ }
170+ }
171+ });
172+ }
173+
174+ public void userCallback (final Callback onSuccess ) {
175+ mAuth = FirebaseAuth .getInstance ();
176+ WritableMap userMap = getUserMap ();
177+
178+ user .getToken (true ).addOnCompleteListener (new OnCompleteListener <GetTokenResult >() {
179+ @ Override
180+ public void onComplete (@ NonNull Task <GetTokenResult > task ) {
181+ WritableMap userMap = Arguments .createMap ();
182+
183+ userMap .putString ("token" , task .getResult ().getToken ());
184+ userMap .putString ("email" , user .getEmail ());
185+ userMap .putString ("uid" , user .getUid ());
186+ userMap .putString ("provider" , user .getProviderId ());
187+
188+ onSuccess .invoke (userMap );
189+ }
190+ });
191+ }
192+
193+ private WritableMap getUserMap () {
194+ WritableMap userMap = Arguments .createMap ();
195+
196+ userMap .putString ("email" , user .getEmail ());
197+ userMap .putString ("uid" , user .getUid ());
198+ userMap .putString ("provider" , user .getProviderId ());
199+
200+ return userMap ;
201+ }
202+
203+ /**
204+ * send a JS event
205+ **/
206+ private void sendEvent (ReactContext reactContext ,
207+ String eventName ,
208+ WritableMap params ) {
209+ reactContext
210+ .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
211+ .emit (eventName , params );
212+ }
213+
214+ public void noUserCallback (final Callback callback ) {
215+ WritableMap message = Arguments .createMap ();
216+
217+ message .putString ("errorMessage" , "no_user" );
218+ message .putString ("eventName" , "no_user" );
219+ message .putBoolean ("authenticated" , false );
220+
221+ callback .invoke (message );
222+ }
223+
224+ public void userErrorCallback (Task <AuthResult > task , final Callback onFail ) {
225+ WritableMap error = Arguments .createMap ();
226+ error .putInt ("errorCode" , task .getException ().hashCode ());
227+ error .putString ("errorMessage" , task .getException ().getMessage ());
228+ error .putString ("allErrorMessage" , task .getException ().toString ());
229+
230+ onFail .invoke (error );
231+ }
232+
233+ }
0 commit comments