Skip to content

Instantly share code, notes, and snippets.

@chani24
Last active July 15, 2020 15:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chani24/4b9d75f79d14ec239d91975683573f2a to your computer and use it in GitHub Desktop.
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