Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I found another answer here here which leads me to this.

1) Since I am only referring to the current user, if this user is verified, it can be implied they are logged in.

2) I can use the angularfire2 authstate to simplify further.

my new verified.guard.ts file:

import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase} from 'angularfire2/database';
@Injectable()
export class VerifiedGuard implements CanActivate {
 constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase, private router: Router) {
 }
 canActivate(next: ActivatedRouteSnapshot,
 state: RouterStateSnapshot): Observable<boolean> {
 return new Observable<boolean>(observer => {
 this.afAuth.authState.subscribe((authState) => {
 if (authState) {
 this.checkVerified().subscribe((adminState) => {
 observer.next(adminState);
 });
 } else {
 this.router.navigate(['/login']);
 observer.next(false);
 }
 });
 });
 }
 checkVerified() {
 if (this.afAuth.auth.currentUser) {
 const uid = this.afAuth.auth.currentUser.uid;
 return this.db.object('/users/' + uid).map(userObj => {
 if (userObj.$exists()) {
 if (userObj.verified) {
 return true;
 } else {
 this.router.navigate(['/unverified']);
 return false;
 }
 } else {
 return false;
 }
 });
 }
 return Observable.of(false);
 }
}

I found another answer here which leads me to this.

1) Since I am only referring to the current user, if this user is verified, it can be implied they are logged in.

2) I can use the angularfire2 authstate to simplify further.

my new verified.guard.ts file:

import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase} from 'angularfire2/database';
@Injectable()
export class VerifiedGuard implements CanActivate {
 constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase, private router: Router) {
 }
 canActivate(next: ActivatedRouteSnapshot,
 state: RouterStateSnapshot): Observable<boolean> {
 return new Observable<boolean>(observer => {
 this.afAuth.authState.subscribe((authState) => {
 if (authState) {
 this.checkVerified().subscribe((adminState) => {
 observer.next(adminState);
 });
 } else {
 this.router.navigate(['/login']);
 observer.next(false);
 }
 });
 });
 }
 checkVerified() {
 if (this.afAuth.auth.currentUser) {
 const uid = this.afAuth.auth.currentUser.uid;
 return this.db.object('/users/' + uid).map(userObj => {
 if (userObj.$exists()) {
 if (userObj.verified) {
 return true;
 } else {
 this.router.navigate(['/unverified']);
 return false;
 }
 } else {
 return false;
 }
 });
 }
 return Observable.of(false);
 }
}

I found another answer here which leads me to this.

1) Since I am only referring to the current user, if this user is verified, it can be implied they are logged in.

2) I can use the angularfire2 authstate to simplify further.

my new verified.guard.ts file:

import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase} from 'angularfire2/database';
@Injectable()
export class VerifiedGuard implements CanActivate {
 constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase, private router: Router) {
 }
 canActivate(next: ActivatedRouteSnapshot,
 state: RouterStateSnapshot): Observable<boolean> {
 return new Observable<boolean>(observer => {
 this.afAuth.authState.subscribe((authState) => {
 if (authState) {
 this.checkVerified().subscribe((adminState) => {
 observer.next(adminState);
 });
 } else {
 this.router.navigate(['/login']);
 observer.next(false);
 }
 });
 });
 }
 checkVerified() {
 if (this.afAuth.auth.currentUser) {
 const uid = this.afAuth.auth.currentUser.uid;
 return this.db.object('/users/' + uid).map(userObj => {
 if (userObj.$exists()) {
 if (userObj.verified) {
 return true;
 } else {
 this.router.navigate(['/unverified']);
 return false;
 }
 } else {
 return false;
 }
 });
 }
 return Observable.of(false);
 }
}
Source Link
Jeff
  • 469
  • 3
  • 14

I found another answer here which leads me to this.

1) Since I am only referring to the current user, if this user is verified, it can be implied they are logged in.

2) I can use the angularfire2 authstate to simplify further.

my new verified.guard.ts file:

import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase} from 'angularfire2/database';
@Injectable()
export class VerifiedGuard implements CanActivate {
 constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase, private router: Router) {
 }
 canActivate(next: ActivatedRouteSnapshot,
 state: RouterStateSnapshot): Observable<boolean> {
 return new Observable<boolean>(observer => {
 this.afAuth.authState.subscribe((authState) => {
 if (authState) {
 this.checkVerified().subscribe((adminState) => {
 observer.next(adminState);
 });
 } else {
 this.router.navigate(['/login']);
 observer.next(false);
 }
 });
 });
 }
 checkVerified() {
 if (this.afAuth.auth.currentUser) {
 const uid = this.afAuth.auth.currentUser.uid;
 return this.db.object('/users/' + uid).map(userObj => {
 if (userObj.$exists()) {
 if (userObj.verified) {
 return true;
 } else {
 this.router.navigate(['/unverified']);
 return false;
 }
 } else {
 return false;
 }
 });
 }
 return Observable.of(false);
 }
}
lang-js

AltStyle によって変換されたページ (->オリジナル) /