Skip to content

Commit dd9ea2e

Browse files
committed
Initial android implementation
1 parent 2180d25 commit dd9ea2e

11 files changed

+637
-290
lines changed

Diff for: .babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["react-native"]
3+
}

Diff for: android/build.gradle

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.1"
6+
7+
defaultConfig {
8+
minSdkVersion 16
9+
targetSdkVersion 23
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
}
17+
}
18+
}
19+
20+
dependencies {
21+
compile 'com.facebook.react:react-native:0.20.+'
22+
compile 'com.google.firebase:firebase-core:9.4.0'
23+
compile 'com.google.firebase:firebase-auth:9.4.0'
24+
}

Diff for: android/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="io.fullstack.firestack">
3+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.fullstack.firestack;
2+
3+
import com.facebook.react.ReactPackage;
4+
import com.facebook.react.bridge.JavaScriptModule;
5+
import com.facebook.react.bridge.NativeModule;
6+
import com.facebook.react.bridge.ReactApplicationContext;
7+
import com.facebook.react.uimanager.ViewManager;
8+
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
13+
public class FirestackPackage implements ReactPackage {
14+
/**
15+
* @param reactContext react application context that can be used to create modules
16+
* @return list of native modules to register with the newly created catalyst instance
17+
*/
18+
@Override
19+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
20+
List<NativeModule> modules = new ArrayList<>();
21+
modules.add(new FirestackModule(reactContext));
22+
23+
return modules;
24+
}
25+
26+
/**
27+
* @return list of JS modules to register with the newly created catalyst instance.
28+
* <p/>
29+
* IMPORTANT: Note that only modules that needs to be accessible from the native code should be
30+
* listed here. Also listing a native module here doesn't imply that the JS implementation of it
31+
* will be automatically included in the JS bundle.
32+
*/
33+
@Override
34+
public List<Class<? extends JavaScriptModule>> createJSModules() {
35+
return Collections.emptyList();
36+
}
37+
38+
/**
39+
* @param reactContext
40+
* @return a list of view managers that should be registered with {@link UIManagerModule}
41+
*/
42+
@Override
43+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
44+
return Collections.emptyList();
45+
}
46+
}

Diff for: firestack.android.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/**
2-
* Stub of Firestack for Android.
3-
*
42
* @providesModule Firestack
53
* @flow
64
*/
7-
'use strict';
5+
import Firestack from './lib/firestack'
86

9-
var warning = require('fbjs/lib/warning');
10-
11-
var Firestack = {
12-
test: function() {
13-
warning('Not yet implemented for Android.');
14-
}
15-
};
16-
17-
module.exports = Firestack;
7+
export default Firestack

0 commit comments

Comments
 (0)