
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Authentication related routing logics for an Angular 2 app like this:
This module helps managing the routing based on if the authentication token is available, if the user data has been loaded, and if any of these requests fail. The authentication token is stored in a configurable localStorage variable.
npm install
npm start
npm test
There are 3 base routes of you application
You have an "API service" with 3 methods (called login/logout/initialRequest) that return promises
In your root component, load your API service and the CoAuther module. Then configure routes and initialize the CoAuther in the constructor of the component. Also expose the logout function from the CoAuther where you will use that:
import apiService from './apiService'
import * as CoAuther from 'co-auther'
...
// The 3 basic routes
@RouteConfig([
{path: '/authenticate', as: 'Authenticate', component: AuthenticateCmp, useAsDefault: true},
{path: '/loggedIn', as: 'LoggedIn', component: LoggedInCmp},
{path: '/initialRequest', as: 'InitialRequest', component: InitialRequestCmp}
])
...
constructor () {
CoAuther.initialize(apiService, {
routes: {
loggedIn: 'LoggedIn',
authenticate: 'Authenticate',
initialRequest: 'InitialRequest'
},
authData: 'authData'
}, (routePath) => { // Register a routing function
this.router.navigate(['/' + routePath])
})
}
logout () {
CoAuther.getCoAuther().logoutWrap()
}
Use the authentication features in your authentication component:
import { activationHelper, getCoAuther } from 'co-auther'
...
@CanActivate(() => activationHelper('Authenticate'))
export class AuthenticateCmp {
login (username, login) {
getCoAuther().loginWrap(username, login)
}
}
Use the activationHelper in the initialRequest route:
import { activationHelper } from 'co-auther'
...
@CanActivate(() => activationHelper('InitialRequest'))
And finally use activationHelper in loggedIn route:
import { activationHelper } from 'co-auther'
...
@CanActivate(() => activationHelper('LoggedIn'))
In order to remember which terminal route you were aiming for when accessing the GUI, you need this 'hack' in the terminal routes:
import {setTerminal} from 'co-auther'
...
@CanActivate(setTerminal)
...
When writing the error handling in the apiService you will want to use the .catch() of the promises. The problem is that once you've caught a rejected promise, it will bubble up as a resolved promise, so the parent will get a .then(). You can fix it by creating a new promise in your apiService and "rethrow" the error. In the file "example-common/api-service.ts" there is an example of this for the "initialRequest" function.
let apiService = {
login () {
return new Promise((resolve, reject) => {
return myRequest
.then((data) => {
resolve(data)
})
.catch((err) => {
myCustomErrorHandler(err)
reject(err)
})
})
}
}
FAQs
Angular 2 routing helper
The npm package co-auther receives a total of 7 weekly downloads. As such, co-auther popularity was classified as not popular.
We found that co-auther demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.