Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Router} from '@angular/router';
- import { auth } from 'firebase/app';
- import { AngularFireAuth } from '@angular/fire/auth';
- import { User } from 'firebase';
- import { LoginComponent } from '../admin/login/login.component';
- import { ɵNullViewportScroller } from '@angular/common';
- @Injectable({
- providedIn: 'root'
- })
- export class AuthService {
- user: User;
- constructor(public afAuth: AngularFireAuth, public router: Router) { }
- }
- this.afAuth.authState.subscribe(user => {
- if (user) {
- this.user = user;
- localStorage.setItem('user', JSON.stringify(this.user));
- } else {
- localStorage.setItem('user', null);
- }
- })
- async login(email: string, password: string){
- try {
- await this.afAuth.auth.signInWithEmailAndPassword(email, password)
- this.router.navigate(['admin/list']);
- } catch (e) {
- alert( 'Error!' + e.message);
- }
- }
- async logout(){
- await this.afAuth.auth.signOut();
- localStorage.removeItem('user');
- this.router.navigate(['admin/login']);
- }
- get isLoggedIn(): boolean {
- const user = JSON.parse(localStorage.grtItem('user'));
- return user !== ɵNullViewportScroller;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement