Я создаю систему аутентификации с email and password
в firebase
, при входе в систему и выходе из системы оба отлично работают в качестве ответа console.log(authState)
, но angular guard
всегда возвращает метод isLoggedIn()
is null
Я не знаю, почему это ?!В конструкторе я определил его значением userDetails:
Код только для конструктора auth.service:
public user: Observable<firebase.User>;
public userDetails: firebase.User = null;
constructor(
private af: AngularFireAuth,
private navCtrl: NavController,
private statusMessage: ToastMessagesService
) {
this.user = af.authState;
this.user.subscribe(
user => {
this.userDetails = user;
console.log(this.userDetails); // I get a response from this on the login page with user details ( user is logged in )
}
)
}
Код только одного метода:
isLoggedIn(): boolean {
return (this.userDetails != null) ? true : false;
}
Код всей службы (auth.service.ts):
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs/internal/observable';
import { NavController } from '@ionic/angular';
import { ToastMessagesService } from './toast-messages.service';
import * as firebase from 'firebase';
@Injectable({
providedIn: 'root'
})
export class AuthService {
public user: Observable<firebase.User>;
public userDetails: firebase.User = null;
constructor(
private af: AngularFireAuth,
private navCtrl: NavController,
private statusMessage: ToastMessagesService
) {
this.user = af.authState;
this.user.subscribe(
user => {
this.userDetails = user;
console.log(this.userDetails);
}
)
}
async siginInRegular(username: string, password: string) {
// const credentials = this.af.auth.email
const results = await this.af.auth.signInWithEmailAndPassword(username, password).then(
results => {
this.navCtrl.navigateForward('/home');
this.statusMessage.message(`Welcome ${results.user.email}`);
}
);
}
async register(username: string, password: string) {
try {
return await this.af.auth.createUserWithEmailAndPassword(username, password).then(
user => {
this.navCtrl.navigateForward('/profile');
this.statusMessage.message(`Welcome ${user.user.email}`);
}
);
} catch (error) {
console.dir(error);
}
}
signOut() {
return this.af.auth.signOut();
}
isLoggedIn(): boolean {
return (this.userDetails != null) ? true : false;
}
}
Код для охраны:
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from './auth.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(
private auth: AuthService
) {
console.log(auth.isLoggedIn());
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this.auth.isLoggedIn()) {
return true
}
console.log('Access denied!');
return false;
}
}