This repository was archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): add AuthenticationStep and deprecate AuthorizeStep
DEPRECATION: AuthorizeStep replaced by AuthenticationStep AuthenticationStep uses {settings:authentication:true}} of a route's configuration
- Loading branch information
1 parent
ca89656
commit baeb35c
Showing
7 changed files
with
173 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {inject} from 'aurelia-dependency-injection'; | ||
import {Authentication} from './authentication'; | ||
import {Redirect} from 'aurelia-router'; | ||
|
||
@inject(Authentication) | ||
export class AuthenticateStep { | ||
constructor(authentication) { | ||
this.authentication = authentication; | ||
} | ||
|
||
run(routingContext, next) { | ||
const isLoggedIn = this.authentication.isAuthenticated(); | ||
const loginRoute = this.authentication.config.loginRoute; | ||
|
||
if (routingContext.getAllInstructions().some(i => i.config.settings.authenticate === true)) { | ||
if (!isLoggedIn) { | ||
return next.cancel(new Redirect(loginRoute)); | ||
} | ||
} else if (isLoggedIn && routingContext.getAllInstructions().some(i => i.fragment === loginRoute)) { | ||
return next.cancel(new Redirect( this.authentication.config.loginRedirect )); | ||
} | ||
|
||
return next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import {Container} from 'aurelia-dependency-injection'; | ||
|
||
import {AuthenticateStep} from '../src/authenticateStep'; | ||
|
||
|
||
const routes = { | ||
onLoginRoute : [ | ||
{name: 'parent', fragment: '/login', config: {settings: {}}}, | ||
{name: 'child', fragment: 'childUrl', config: {settings: {}}} | ||
], | ||
authenticateNone : [ | ||
{name: 'parent', fragment: 'parentUrl', config: {settings: {}}}, | ||
{name: 'child', fragment: 'childUrl', config: {settings: {}}} | ||
], | ||
authenticateChild : [ | ||
{name: 'parent', fragment: 'parentUrl', config: {settings: {}}}, | ||
{name: 'child', fragment: 'childUrl', config: {settings: {authenticate: true}}} | ||
], | ||
authenticateParent : [ | ||
{name: 'parent', fragment: 'parentUrl', config: {settings: {authenticate: true}}}, | ||
{name: 'child', fragment: 'childUrl', config: {settings: {}}} | ||
]}; | ||
|
||
describe('AuthenticateStep', () => { | ||
describe('.run()', () => { | ||
const authenticateStep = new Container().get(AuthenticateStep); | ||
function next() {return;} | ||
let loginRoute = authenticateStep.authentication.config.loginRoute; | ||
|
||
it('should not redirect when not authenticated and no route requires it', () => { | ||
let routingContext = { | ||
getAllInstructions: () => routes.authenticateNone | ||
}; | ||
|
||
next.cancel = redirect => {throw new Error();}; | ||
|
||
spyOn(next, 'cancel'); | ||
|
||
authenticateStep.run(routingContext, next); | ||
|
||
expect(next.cancel).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should redirect to login when not authenticated and child route requires it', done => { | ||
let routingContext = { | ||
getAllInstructions: () => routes.authenticateChild | ||
}; | ||
|
||
next.cancel = redirect => { | ||
expect(redirect.url).toBe(loginRoute); | ||
done(); | ||
}; | ||
|
||
authenticateStep.run(routingContext, next); | ||
}); | ||
|
||
it('should redirect to login when not authenticated and parent route requires it', done => { | ||
let routingContext = { | ||
getAllInstructions: () => routes.authenticateParent | ||
}; | ||
|
||
next.cancel = redirect => { | ||
expect(redirect.url).toBe(loginRoute); | ||
done(); | ||
}; | ||
|
||
authenticateStep.run(routingContext, next); | ||
}); | ||
|
||
it('should not redirect to login when authenticated and no route requires it', () => { | ||
let routingContext = { | ||
getAllInstructions: () => routes.authenticateNone | ||
}; | ||
|
||
next.cancel = redirect => {throw new Error();}; | ||
|
||
spyOn(next, 'cancel'); | ||
|
||
authenticateStep.authentication.isAuthenticated = () => true; | ||
|
||
authenticateStep.run(routingContext, next); | ||
|
||
expect(next.cancel).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not redirect when authenticated and child route requires it', () => { | ||
let routingContext = { | ||
getAllInstructions: () => routes.authenticateChild | ||
}; | ||
|
||
next.cancel = redirect => {throw new Error();}; | ||
|
||
spyOn(next, 'cancel'); | ||
|
||
authenticateStep.authentication.isAuthenticated = () => true; | ||
|
||
authenticateStep.run(routingContext, next); | ||
|
||
expect(next.cancel).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not redirect when not authenticated and parent route requires it', () => { | ||
let routingContext = { | ||
getAllInstructions: () => routes.authenticateParent | ||
}; | ||
|
||
next.cancel = redirect => {throw new Error();}; | ||
|
||
spyOn(next, 'cancel'); | ||
|
||
authenticateStep.authentication.isAuthenticated = () => true; | ||
|
||
authenticateStep.run(routingContext, next); | ||
|
||
expect(next.cancel).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should redirect when authenticated and parent route is login route', done => { | ||
let routingContext = { | ||
getAllInstructions: () => routes.onLoginRoute | ||
}; | ||
|
||
next.cancel = redirect => { | ||
expect(redirect.url).toBe(authenticateStep.authentication.config.loginRedirect); | ||
done(); | ||
}; | ||
|
||
authenticateStep.authentication.isAuthenticated = () => true; | ||
|
||
authenticateStep.run(routingContext, next); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import {initialize} from 'aurelia-pal-browser'; | ||
import 'aurelia-polyfills'; | ||
import 'fetch'; | ||
|
||
initialize(); |