Guest User

Untitled

a guest
Jan 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { Observable } from 'rxjs';
  3. import { AngularFireAuth } from '@angular/fire/auth';
  4.  
  5. @Component({
  6. selector: 'app-groceries',
  7. templateUrl: './groceries.component.html',
  8. styleUrls: ['./groceries.component.css']
  9. })
  10. export class GroceriesComponent {
  11.  
  12. createItem = '';
  13. showLoginUserInputForm = false;
  14. showCreateUserInputForm = false;
  15. loginEmail = '';
  16. loginPassword = '';
  17. createEmail = '';
  18. createPassword = '';
  19. userEmail = '';
  20.  
  21. constructor(public afAuth: AngularFireAuth) {
  22. this.afAuth.auth.onAuthStateChanged(user => {
  23. if (user) {
  24. // when logged in show user email here
  25. this.userEmail = user.email;
  26. }
  27. });
  28. }
  29.  
  30. showLoginUserForm() {
  31. this.showLoginUserInputForm = true;
  32. }
  33.  
  34. showCreateUserForm() {
  35. this.showCreateUserInputForm = true;
  36. }
  37.  
  38. createUser() {
  39. this.afAuth.auth.createUserWithEmailAndPassword(this.createEmail, this.createPassword)
  40. .then(
  41. function(success) {
  42. // on success hide create user input form and store variables in login
  43. // and then call the login method
  44. this.showCreateUserInputForm = false;
  45. this.loginEmail = this.createEmail;
  46. this.loginPassword = this.createPassword;
  47. this.createEmail = '';
  48. this.createPassword = '';
  49. this.loginUser();
  50. }.bind(this),
  51. function(error) {
  52. alert(error);
  53. });
  54. }
  55.  
  56. loginUser() {
  57. this.afAuth.auth.signInWithEmailAndPassword(this.loginEmail, this.loginPassword)
  58. .then(
  59. function(success) {
  60. // on success populate user variables and then select grocery items for that user
  61. this.userUiD = success.user.uid;
  62. this.userEmail = success.user.email;
  63. this.loginEmail = '';
  64. this.loginPassword = '';
  65. this.showLoginUserInputForm = false;
  66. this.showCreateUserInputForm = false;
  67. this.userEmail = this.afAuth.auth.currentUser.email;
  68. }.bind(this),
  69. function(error) {
  70. alert(error);
  71. });
  72. }
  73.  
  74. // async is not necessary here, just controlling the event loop
  75. async logoutUser() {
  76. await this.afAuth.auth.signOut()
  77. .catch(function(error) { alert(error); });
  78. }
  79.  
  80. cancelButton() {
  81. this.showLoginUserInputForm = false;
  82. this.showCreateUserInputForm = false;
  83. }
  84.  
  85. }
Add Comment
Please, Sign In to add comment