Skip to content

Commit

Permalink
[IncomeApp] creating user doc through firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
JCamiloo committed Jan 3, 2020
1 parent 211dbf1 commit ecc6dbc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 12 additions & 4 deletions 3. IncomeApp/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';
import { Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { map } from 'rxjs/operators';
import * as firebase from 'firebase';
import { User } from './user.model';

@Injectable({
providedIn: 'root'
})
export class AuthService {

constructor(private afAuth: AngularFireAuth,
private afDB: AngularFirestore,
private router: Router,
private toastr: ToastrService) { }

Expand All @@ -22,14 +25,19 @@ export class AuthService {

createUser(name: string, email: string, password: string) {
this.afAuth.auth.createUserWithEmailAndPassword(email, password)
.then(resp => this.router.navigate(['/']))
.catch(err => this.toastr.error(err['message'], 'Error'));
.then(resp => {
const user: User = { uid: resp.user.uid, name, email };
this.afDB.doc(`${user.uid}/user`).set(user).then(() => {
this.router.navigate(['/'])
});
})
.catch(err => this.toastr.error(err['message'], 'Error'));
}

login(email: string, password: string) {
this.afAuth.auth.signInWithEmailAndPassword(email, password)
.then(resp => this.router.navigate(['/']))
.catch(err => this.toastr.error(err['message'], 'Error'));
.then(resp => this.router.navigate(['/']))
.catch(err => this.toastr.error(err['message'], 'Error'));
}

isAuth() {
Expand Down
12 changes: 12 additions & 0 deletions 3. IncomeApp/src/app/auth/user.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class User {

public name: string;
public email: string;
public uid: string;

constructor(name: string, email: string, uid: string) {
this.name = name;
this.email = email;
this.uid = uid;
}
}

0 comments on commit ecc6dbc

Please sign in to comment.