Skip to content

Instantly share code, notes, and snippets.

@green3g
Last active November 18, 2022 00:40
Show Gist options
  • Save green3g/5f6b662388063a45d5218800cbb4e08e to your computer and use it in GitHub Desktop.
Save green3g/5f6b662388063a45d5218800cbb4e08e to your computer and use it in GitHub Desktop.
aurelia-routing
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dumber Gist</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<base href="/">
</head>
<!--
Dumber gist uses dumber bundler, the default bundle file
is /dist/entry-bundle.js.
The starting module is pointed to aurelia-bootstrapper
(data-main attribute on script) for Aurelia,
The aurelia bootstrapper then loads up user module "main"
(aurelia-app attribute on <body>) which is your src/main.ts.
-->
<body aurelia-app="main">
<script src="/dist/entry-bundle.js" data-main="aurelia-bootstrapper"></script>
</body>
</html>
{
"dependencies": {
"aurelia-bootstrapper": "latest"
}
}
<template>
<!-- Try to create a css/scss/sass/less file then require it here -->
<h1>${message}</h1>
<a route-href="private">Private</a>
<a route-href="login">login</a>
<a route-href="home">home</a>
<router-view></router-view>
</template>
import { PLATFORM } from 'aurelia-pal';
import {AuthService} from './services/auth'
import {AuthorizeStep} from './auth/auth-step';
export class App {
public message: string = 'Hello Aurelia!';
private router;
configureRouter(config, router){
this.router = router;
config.map([
{name: 'home', route: '', moduleId: PLATFORM.moduleName('pages/home'), title: 'Home'},
{name: 'login', route: 'login', moduleId: PLATFORM.moduleName('pages/login'), title: 'login'},
{name: 'private', route: 'private', moduleId: PLATFORM.moduleName('pages/private'), title: 'private'},
]);
config.addAuthorizeStep(AuthorizeStep);
}
}
import { autoinject } from 'aurelia-framework';
import {AuthService} from '../services/auth'
import { AppRouter, NavigationInstruction, Redirect, RedirectToRoute, Router, RouterConfiguration, RouterEvent } from 'aurelia-router';
@autoinject
export class AuthorizeStep {
constructor(private auth: AuthService){}
async run(navigationInstruction, next) {
console.log(navigationInstruction);
if(navigationInstruction.config.name === 'private'){
if(!this.auth.isLoggedIn){
return next.cancel(new RedirectToRoute('login'))
}
return next();
}
return next();
}
}
import {Aurelia} from 'aurelia-framework';
export function configure(aurelia: Aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging('info');
aurelia.start().then(() => aurelia.setRoot());
}
<template>
Home page
</template>
export class Home {}
<template>
<h2>Login page</h2>
<p>logged in: ${auth.isLoggedIn}</p>
<button click.delegate="auth.isLoggedIn = true">Login</button>
</template>
import {AuthService} from './services/auth'
export class Login {}
<template>
Private
</template>
export class Private {}
export class AuthService {
public isLoggedIn = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment