Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

canactivate angular

// ./guards/CanActivateViaAuth.guard.ts
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { CanActivate } from '@angular/router';

@Injectable()
export class CanActivateViaAuthGuard implements CanActivate {

    constructor(private router: Router) {}

    canActivate() {
        // If the user is not logged in we'll send them back to the home page
        try {
            let logged = false;
            // some check actions
            // logged = result of checks
            if (logged) {
              return true;
            } else {
              //localStorage.clear();
              console.log("notlogged");
              this.router.navigate(["/"]);
            }
        } catch (ex) {
          //localStorage.clear();
          console.log("notlogged");
          this.router.navigate(["/"]);
        }
    }
}


// app.module.ts
import { CanActivateViaAuthGuard } from "./guards/CanActivateViaAuth.guard";
/* other inports */

@NgModule({
  declarations: [
    /* app declarations */
  ],
  imports: [
    /* app imports */
  ],
  providers: [
    CanActivateViaAuthGuard,
     /* other app providers */
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}


// app-routeing.module
import { CanActivateViaAuthGuard } from './guards/CanActivateViaAuth.guard';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: 'dashboard',
    component: DashboardComponent,
    canActivate: [CanActivateViaAuthGuard],
  }
];

@NgModule({
  //{ enableTracing: true }
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule {}

Comment

PREVIOUS NEXT
Code Example
Javascript :: build apk from react native 
Javascript :: Link vs NavLink in react-router-dom 
Javascript :: next js image 
Javascript :: preloader 
Javascript :: create csv file nodejs 
Javascript :: dm command discord.js 
Javascript :: es6 method definition syntax 
Javascript :: get the state of a checkbox 
Javascript :: sort strings javascript alphabetically 
Javascript :: adding element to javascript object 
Javascript :: display console log in HTML 
Javascript :: call a function multiple times 
Javascript :: ts node cannot use import statement outside a module 
Javascript :: mongodb sort 
Javascript :: post method 
Javascript :: angularjs find and update object in array 
Javascript :: Mars Exploration problem in js 
Javascript :: document queryselectorall and map javacript 
Javascript :: normalize javascript 
Javascript :: js math function that returns smallest value 
Javascript :: node js run for loop asynchronously 
Javascript :: youtube set speed command 
Javascript :: use style in react 
Javascript :: how to add items in an array in js 
Javascript :: get element by xpath 
Javascript :: convert Float64Array to array in js 
Javascript :: node js while loop with settimeout 
Javascript :: how to convert seaconds into hh:mm:ss in javascript 
Javascript :: timeout 
Javascript :: angular transition animation 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =