Guest User

Untitled

a guest
Aug 7th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1. buildscript {
  2.  
  3. repositories {
  4. google()
  5. jcenter()
  6. }
  7. dependencies {
  8. classpath 'com.android.tools.build:gradle:3.1.3'
  9. classpath 'com.google.gms:google-services:4.0.1'
  10.  
  11.  
  12. // NOTE: Do not place your application dependencies here; they belong
  13. // in the individual module build.gradle files
  14. }
  15. }
  16.  
  17. allprojects {
  18. repositories {
  19. google()
  20. jcenter()
  21. }
  22. }
  23.  
  24. task clean(type: Delete) {
  25. delete rootProject.buildDir
  26. }
  27.  
  28. apply plugin: 'com.android.application'
  29.  
  30. android {
  31. compileSdkVersion 27
  32. defaultConfig {
  33. applicationId "firebase.learning"
  34. minSdkVersion 16
  35. targetSdkVersion 27
  36. multiDexEnabled true
  37. versionCode 1
  38. versionName "1.0"
  39. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  40. }
  41. buildTypes {
  42. release {
  43. minifyEnabled false
  44. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  45. }
  46. }
  47. }
  48.  
  49. dependencies {
  50. implementation fileTree(dir: 'libs', include: ['*.jar'])
  51.  
  52. implementation 'com.android.support:appcompat-v7:27.1.1'
  53. implementation 'com.android.support:recyclerview-v7:27.1.1'
  54. implementation 'com.android.support:design:27.1.1'
  55.  
  56. implementation 'com.android.support:multidex:1.0.3'
  57.  
  58. implementation 'com.android.support.constraint:constraint-layout:1.1.2'
  59.  
  60. implementation 'com.google.firebase:firebase-firestore:17.0.4'
  61. implementation 'com.google.firebase:firebase-auth:16.0.2'
  62. implementation 'com.google.android.gms:play-services-auth:15.0.1'
  63.  
  64.  
  65. testImplementation 'junit:junit:4.12'
  66. androidTestImplementation 'com.android.support.test:runner:1.0.2'
  67. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  68. }
  69. // Add to the bottom of the file
  70. apply plugin: 'com.google.gms.google-services'
  71.  
  72. public class SignInActivity extends AppCompatActivity {
  73.  
  74. private static final String TAG = "SignInActivity";
  75. private EditText inputEmail, inputPassword;
  76. private ProgressBar progressBar;
  77. private Button btnSignup, btnLogin, btnReset;
  78.  
  79. private FirebaseAuth mAuth;
  80. private FirebaseAuth.AuthStateListener mAuthListener;
  81.  
  82. @Override
  83. protected void onCreate(Bundle savedInstanceState) {
  84. super.onCreate(savedInstanceState);
  85.  
  86. //Get Firebase auth instance
  87. mAuth = FirebaseAuth.getInstance();
  88.  
  89. if (mAuth.getCurrentUser() != null) {
  90. startActivity(new Intent(SignInActivity.this, AddQuestionActivity.class));
  91. finish();
  92. }
  93.  
  94. // set the view now
  95. setContentView(R.layout.activity_sign_in);
  96.  
  97. mAuthListener = new FirebaseAuth.AuthStateListener() {
  98. @Override
  99. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  100. FirebaseUser user = firebaseAuth.getCurrentUser();
  101.  
  102. if (user != null) {
  103. // User is signed in
  104. //redirect
  105. ///updateUI(user);
  106.  
  107. } else {
  108. // User is signed out
  109. Log.d(TAG, "onAuthStateChanged:signed_out");
  110. ///updateUI(null);
  111. }
  112.  
  113. }
  114. };
  115.  
  116. inputEmail = (EditText) findViewById(R.id.email);
  117. inputPassword = (EditText) findViewById(R.id.password);
  118. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  119. btnSignup = (Button) findViewById(R.id.btn_signup);
  120. btnLogin = (Button) findViewById(R.id.btn_login);
  121. btnReset = (Button) findViewById(R.id.btn_reset_password);
  122.  
  123. //Get Firebase auth instance
  124. mAuth = FirebaseAuth.getInstance();
  125.  
  126. btnSignup.setOnClickListener(new View.OnClickListener() {
  127. @Override
  128. public void onClick(View v) {
  129. ///startActivity(new Intent(SignInActivity.this, AddQuestionActivity.class));
  130. }
  131. });
  132.  
  133. btnReset.setOnClickListener(new View.OnClickListener() {
  134. @Override
  135. public void onClick(View v) {
  136. ///startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
  137. }
  138. });
  139.  
  140. btnLogin.setOnClickListener(new View.OnClickListener() {
  141. @Override
  142. public void onClick(View v) {
  143. String email = inputEmail.getText().toString();
  144. final String password = inputPassword.getText().toString();
  145.  
  146. if (TextUtils.isEmpty(email)) {
  147. Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
  148. return;
  149. }
  150.  
  151. if (TextUtils.isEmpty(password)) {
  152. Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
  153. return;
  154. }
  155.  
  156. progressBar.setVisibility(View.VISIBLE);
  157.  
  158.  
  159. mAuth.signInWithEmailAndPassword(email,password)
  160. .addOnFailureListener(SignInActivity.this, new OnFailureListener() {
  161. @Override
  162. public void onFailure(@NonNull Exception e) {
  163. Toast.makeText(SignInActivity.this,"Failure",Toast.LENGTH_LONG).show();
  164. }
  165. })
  166. .addOnCompleteListener(SignInActivity.this, new OnCompleteListener<AuthResult>() {
  167. @Override
  168. public void onComplete(@NonNull Task<AuthResult> task) {
  169. if (task.isSuccessful()) {
  170. // Sign in success, update UI with the signed-in user's information
  171. Log.d(TAG, "signInWithEmail:success");
  172. FirebaseUser user = mAuth.getCurrentUser();
  173. ///updateUI(user);
  174.  
  175. Intent intent = new Intent(SignInActivity.this, AddQuestionActivity.class);
  176. startActivity(intent);
  177. finish();
  178. } else {
  179. // If sign in fails, display a message to the user.
  180. Log.w(TAG, "signInWithEmail:failure", task.getException());
  181. Toast.makeText(SignInActivity.this, "Authentication failed.",
  182. Toast.LENGTH_SHORT).show();
  183. ///updateUI(null);
  184. }
  185. }
  186. });
  187.  
  188. //authenticate user
  189. /*auth.signInWithEmailAndPassword(email, password)
  190. .addOnCompleteListener(SignInActivity.this, new OnCompleteListener<AuthResult>() {
  191. @Override
  192. public void onComplete(@NonNull Task<AuthResult> task) {
  193. // If sign in fails, display a message to the user. If sign in succeeds
  194. // the auth state listener will be notified and logic to handle the
  195. // signed in user can be handled in the listener.
  196. progressBar.setVisibility(View.GONE);
  197. if (!task.isSuccessful()) {
  198. // there was an error
  199. if (password.length() < 6) {
  200. inputPassword.setError("Password too short, enter minimum 6 characters!");
  201. } else {
  202. Toast.makeText(SignInActivity.this, "Authentication failed, check your email and password or sign up", Toast.LENGTH_LONG).show();
  203. }
  204. } else {
  205. Intent intent = new Intent(SignInActivity.this, AddQuestionActivity.class);
  206. startActivity(intent);
  207. finish();
  208. }
  209. }
  210. });*/
  211. }
  212. });
  213. }
  214.  
  215. @Override
  216. protected void onStart() {
  217. super.onStart();
  218. mAuth.addAuthStateListener(mAuthListener);
  219. }
  220.  
  221. @Override
  222. protected void onStop() {
  223. super.onStop();
  224. if (mAuthListener != null) {
  225. mAuth.removeAuthStateListener(mAuthListener);
  226. }
  227. }
  228. }
Add Comment
Please, Sign In to add comment