Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package de.appwerft.firebase;
  2.  
  3. import org.appcelerator.kroll.KrollDict;
  4. import org.appcelerator.kroll.KrollFunction;
  5. import org.appcelerator.kroll.KrollProxy;
  6. import org.appcelerator.kroll.annotations.Kroll;
  7. import org.appcelerator.kroll.common.Log;
  8. import org.appcelerator.titanium.TiApplication;
  9. import org.appcelerator.titanium.TiContext.OnLifecycleEvent;
  10. import org.appcelerator.titanium.TiBaseActivity;
  11. //import android.app.Activity;
  12.  
  13. import android.app.Activity;
  14. import android.content.Context;
  15. import android.os.Bundle;
  16. //import android.support.v7.app.AppCompatActivity;
  17.  
  18. import com.google.android.gms.tasks.OnCompleteListener;
  19. import com.google.android.gms.tasks.Task;
  20. import com.google.firebase.auth.AuthCredential;
  21. import com.google.firebase.auth.AuthResult;
  22. import com.google.firebase.auth.FirebaseAuth;
  23. import com.google.firebase.auth.FirebaseUser;
  24. import com.google.firebase.auth.GithubAuthProvider;
  25.  
  26. //import org.appcelerator.titanium.TiBaseActivity;
  27.  
  28. @Kroll.proxy(creatableInModule = FirebaseModule.class)
  29. public class AuthenticationProxy extends KrollProxy implements OnLifecycleEvent {
  30. private final class OnCompleteHandler implements
  31. OnCompleteListener<AuthResult> {
  32. @Override
  33. public void onComplete(Task<AuthResult> task) {
  34. if (onComplete != null) {
  35. KrollDict kd = new KrollDict();
  36. kd.put("success", task.isSuccessful());
  37. onComplete.call(getKrollObject(), kd);
  38. }
  39. }
  40. }
  41.  
  42. KrollProxy proxy;
  43. private FirebaseAuth auth;
  44. Activity activity;
  45. private FirebaseAuth.AuthStateListener authListener;
  46. private static final String LCAT = "FiBa 🚝🚝";
  47. KrollFunction onComplete;
  48.  
  49. public AuthenticationProxy() {
  50. super();
  51. }
  52.  
  53. @Kroll.method
  54. public void signInAnonymously(KrollDict opts) {
  55. Log.d(LCAT, " signInAnonymously");
  56. if (opts.containsKeyAndNotNull("onComplete")) {
  57. Object o = opts.get("onComplete");
  58. if (o instanceof KrollFunction) {
  59. onComplete = (KrollFunction) o;
  60. }
  61. }
  62.  
  63. if (auth != null) {
  64. auth.signInAnonymously().addOnCompleteListener(activity,
  65. new OnCompleteHandler());
  66. } else
  67. Log.e(LCAT, "auth was null, cannot do some magic");
  68. }
  69.  
  70. @Kroll.method
  71. public void createUserWithEmailAndPassword(String email, String password) {
  72. auth.createUserWithEmailAndPassword(email, password)
  73. .addOnCompleteListener(activity, new OnCompleteHandler());
  74.  
  75. }
  76.  
  77. @Kroll.method
  78. public void signInWithEmailAndPassword(String email, String password) {
  79. auth.signInWithEmailAndPassword(email, password).addOnCompleteListener(
  80. activity, new OnCompleteHandler());
  81. }
  82.  
  83. @Kroll.method
  84. public void signInGithub(String token) {
  85. AuthCredential credential = GithubAuthProvider.getCredential(token);
  86. auth.signInWithCredential(credential).addOnCompleteListener(activity,
  87. new OnCompleteHandler());
  88. }
  89.  
  90. private void init() {
  91. auth = FirebaseAuth.getInstance();
  92. Log.d(LCAT,
  93. "auth created, try to add AuthStateListener " + auth.toString());
  94. authListener = new FirebaseAuth.AuthStateListener() {
  95. @Override
  96. public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
  97. FirebaseUser user = firebaseAuth.getCurrentUser();
  98. KrollDict result = new KrollDict();
  99. if (proxy.hasListeners("onAuthStateChanged"))
  100. if (user != null) {
  101. result.put("displayName", user.getDisplayName());
  102. result.put("uid", user.getUid());
  103. result.put("email", user.getEmail());
  104. result.put("photoUrl", user.getPhotoUrl());
  105. proxy.fireEvent("onAuthStateChanged", result);
  106. // User is signed in
  107. } else {
  108. // User is signed out
  109. }
  110. }
  111. };
  112. auth.addAuthStateListener(authListener);
  113. }
  114.  
  115. @Override
  116. public void onCreate(Activity activity, Bundle unused) {
  117. Context ctx = TiApplication.getAppCurrentActivity()
  118. .getApplicationContext();
  119. // ((TiBaseActivity) activity).addOnLifecycleEventListener(this);
  120. this.activity = activity;
  121. Log.e(LCAT, "try to get instance of FirebaseAuth. ");
  122. init();
  123. }
  124.  
  125. @Override
  126. public void onStart(Activity activity) {
  127. super.onStart(activity);
  128. Log.d(LCAT, "addAuthStateListener");
  129.  
  130. }
  131.  
  132. @Override
  133. public void onStop(Activity activity) {
  134. if (authListener != null) {
  135. auth.removeAuthStateListener(authListener);
  136. }
  137. super.onStop(activity);
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement