Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Router, Route ,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, CanLoad } from '@angular/router';
- import { Observable } from 'rxjs/Observable';
- import { SimpleAuthService } from './simple-auth.service';
- @Injectable()
- export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
- constructor(private authService: SimpleAuthService) { }
- canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
- const url: string = state.url;
- return this.checkLogin(url);
- }
- canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
- return this.canActivate(route, state);
- }
- canLoad(route: Route): Observable<boolean> {
- const url: string = route.path;
- return this.checkLogin(url);
- }
- checkLogin(url: string): Observable<boolean> {
- return this.authService.isLoggedIn(url);
- }
- }
- import { Component, OnInit, ElementRef } from '@angular/core';
- import { Router, ActivatedRoute } from '@angular/router';
- import { SimpleAuthService } from '../../core/simple-auth.service';
- declare var $: any;
- @Component({
- selector: 'app-login-cmp',
- templateUrl: './login.component.html'
- })
- export class LoginComponent implements OnInit {
- private toggleButton: any;
- private sidebarVisible: boolean;
- private nativeElement: Node;
- private email ='user@dev.org'
- private password ='useruser';
- constructor(private element: ElementRef, public authService: SimpleAuthService,
- private router: Router, private route: ActivatedRoute) {
- if (this.authService.login(this.email,this.password)) {
- this.router.navigate(['dashboard']);
- } else {
- this.nativeElement = element.nativeElement;
- this.sidebarVisible = false;
- }
- }
- ngOnInit() {
- this.login(this.email, this.password);
- var navbar : HTMLElement = this.element.nativeElement;
- this.toggleButton = navbar.getElementsByClassName('navbar-toggle')[0];
- setTimeout(function() {
- // after 1000 ms we add the class animated to the login/register card
- $('.card').removeClass('card-hidden');
- }, 700);
- }
- sidebarToggle() {
- var toggleButton = this.toggleButton;
- var body = document.getElementsByTagName('body')[0];
- var sidebar = document.getElementsByClassName('navbar-collapse')[0];
- if (this.sidebarVisible == false) {
- setTimeout(function() {
- toggleButton.classList.add('toggled');
- }, 500);
- body.classList.add('nav-open');
- this.sidebarVisible = true;
- } else {
- this.toggleButton.classList.remove('toggled');
- this.sidebarVisible = false;
- body.classList.remove('nav-open');
- }
- }
- login(username: string, password: string): void {
- this.authService.login(username, password).then(_ => {
- const redirectUrl: string = this.authService.redirectUrl || 'dashboard';
- this.router.navigate([redirectUrl]);
- });
- }
- }
- import { Injectable } from '@angular/core';
- import { Router, Route ,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, CanLoad } from '@angular/router';
- import { Observable } from 'rxjs/Observable';
- import { AngularFireAuth } from 'angularfire2/auth';
- import { User, UserCredential } from '@firebase/auth-types';
- import { take, map, tap } from 'rxjs/operators';
- @Injectable()
- export class SimpleAuthService {
- user: Observable<User>;
- redirectUrl: string;
- constructor(private afAuth: AngularFireAuth, private router: Router) {
- this.user = this.afAuth.authState;
- }
- getUser(): Observable<User> {
- return this.user.pipe(take(1));
- }
- isLoggedIn(redirectUrl: string): Observable<boolean> {
- return this.user.pipe(
- take(1),
- map(authState => !!authState),
- tap(authenticated => {
- if (!authenticated) {
- this.redirectUrl = redirectUrl;
- this.router.navigate(['/']);
- }
- })
- );
- }
- login(username: string, password: string): Promise<UserCredential> {
- return this.afAuth.auth.signInWithEmailAndPassword(username, password);
- }
- logout(): Promise<boolean> {
- return this.afAuth.auth.signOut().then(() => this.router.navigate(['/login']));
- }
- }
- function login(): Promise<boolean> {
- return Promise.resolve(true);
- }
- if(login()) {} // this is always true regardless of `catch()` is triggered sometime in the future
- import { AngularFireAuth } from 'angularfire2/auth';
- import { User, UserCredential } from '@firebase/auth-types';
- @Injectable()
- export class AuthService {
- user: Observable<User>;
- redirectUrl: string;
- constructor(private afAuth: AngularFireAuth, private router: Router) {
- this.user = this.afAuth.authState;
- }
- getUser(): Observable<User> {
- return this.user.pipe(take(1));
- }
- isLoggedIn(redirectUrl: string): Observable<boolean> {
- return this.user.pipe(
- take(1),
- map(authState => !!authState),
- tap(authenticated => {
- if (!authenticated) {
- this.redirectUrl = redirectUrl;
- this.router.navigate(['/']);
- }
- })
- );
- }
- login(username: string, password: string): Promise<UserCredential> {
- return this.afAuth.auth.signInWithEmailAndPassword(email, password);
- }
- logout(): Promise<boolean> {
- return this.afAuth.auth.signOut().then(() => this.router.navigate(['/login']));
- }
- }
- @Injectable()
- export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
- constructor(private authService: AuthService) { }
- canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
- const url: string = state.url;
- return this.checkLogin(url);
- }
- canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
- return this.canActivate(route, state);
- }
- canLoad(route: Route): Observable<boolean> {
- const url: string = route.path;
- return this.checkLogin(url);
- }
- checkLogin(url: string): Observable<boolean> {
- return this.authService.isLoggedIn(url);
- }
- }
- export class LoginComponent implements OnInit {
- constructor(private router: Router, public authService: AuthService) { }
- ngOnInit() {
- this.login(this.email, this.password);
- }
- login(username: string, password: string): void {
- this.authService.login(username, password).then(_ => {
- const redirectUrl: string = this.authService.redirectUrl || '/some-default-route';
- this.router.navigate([redirectUrl]);
- });
- }
- }
Add Comment
Please, Sign In to add comment