Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Created January 28, 2020 00:49
Show Gist options
  • Save alexytiger/5f9a9eb0fcd5bb10c053da756287827a to your computer and use it in GitHub Desktop.
Save alexytiger/5f9a9eb0fcd5bb10c053da756287827a to your computer and use it in GitHub Desktop.
e-book
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { take, tap, filter, switchMap, catchError } from 'rxjs/operators';
import * as fromRoot from '../store';
@Injectable({
providedIn: 'root',
})
export class MetaMaskConnectGuard implements CanActivate {
constructor(private store: Store<fromRoot.AppState>) {}
canActivate(): Observable<boolean> {
return this.store.pipe(
select(fromRoot.getMetaMaskConnected),
tap(connected => {
if (!connected) {
this.store.dispatch(fromRoot.Web3ProviderActions.connectRedirect());
return false;
}
return true;
}),
take(1)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment