Last active
July 15, 2020 15:48
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { BehaviorSubject, Observable } from 'rxjs'; | |
import { AngularFireAuth } from '@angular/fire/auth'; | |
import { auth } from 'firebase/app'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class AuthService { | |
constructor(public fireAuth: AngularFireAuth){ | |
this.authStatusListener(); | |
} | |
currentUser ; | |
private authStatusSub = new BehaviorSubject(this.currentUser); | |
currentAuthStatus = this.authStatusSub.asObservable(); | |
authStatusListener(){ | |
this.fireAuth.onAuthStateChanged((credential)=>{ | |
if(credential){ | |
console.log(credential); | |
this.authStatusSub.next(credential); | |
console.log('User is logged in'); | |
} | |
else{ | |
this.authStatusSub.next(null); | |
console.log('User is logged out'); | |
} | |
}) | |
} | |
signIn(email: string, | |
password: string){ | |
return this.fireAuth.signInWithEmailAndPassword(email, password) | |
.then(res => console.log('Logged in')) | |
.catch(err => console.log(err)); | |
} | |
signOut(){ | |
return this.fireAuth.signOut(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment