Guest User

Untitled

a guest
Jun 17th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <form [formGroup]="forma" (ngSubmit)="emailSignUp()" novalidate>
  2. <!-- <div class="form-group">
  3. <input type="text" class="form-control" placeholder="Nombre" formControlName="displayName">
  4. </div> -->
  5. <div class="form-group">
  6. <input type="email" class="form-control" placeholder="Email" formControlName="email">
  7. </div>
  8. <div class="form-group">
  9. <input type="password" class="form-control" placeholder="Contraseña" formControlName="password">
  10. </div>
  11. <div class="my-2">
  12. <button class="btn btn-primary btn-block" type="submit" >Crear cuenta</button>
  13. </div>
  14. </form>
  15.  
  16. this.forma = fb.group ({
  17. //displayName: ['', Validators.required],
  18. email: [ '', [Validators.required, Validators.email] ],
  19. password: [ '', Validators.required ],
  20. })
  21.  
  22. emailSignUp() {
  23. this.auth.emailSignUp(this.forma.value.email, this.forma.value.password)
  24. }
  25.  
  26. interface User {
  27. uid: string;
  28. email: string;
  29. photoURL?: string;
  30. displayName?: string;
  31. favoriteColor?: string;
  32. fechaRegistro?: string;
  33. }
  34.  
  35. constructor( private afAuth: AngularFireAuth, private afs: AngularFirestore) {
  36. this.user = this.afAuth.authState.pipe(
  37. switchMap(user => {
  38. if (user) {
  39. return this.afs.doc<User>(`users/${user.uid}`).valueChanges()
  40. } else {
  41. return of(null)
  42. }
  43. })
  44. )
  45. }
  46.  
  47. emailSignUp(email: string, password: string) {
  48. return this.afAuth.auth
  49. .createUserWithEmailAndPassword(email, password)
  50. .then(credential => {
  51. this.pushUserData(credential.user);
  52. console.log('Usuario creado')
  53. this.router.navigate(['/login'])
  54. })
  55. .catch(error => {
  56. this.handleError(error)
  57. });
  58. }
  59.  
  60. private pushUserData( user: User ) {
  61. const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);
  62. const data: User = {
  63. uid: user.uid,
  64. email: user.email,
  65. };
  66. userRef.set(data, {merge: true});
  67. }
Add Comment
Please, Sign In to add comment