refactor(event): no longer call prevent default by default (#1926)
BREAKING CHANGE: no longer calling preventDefault on all events, this can be switched back to v1 behavior via IListenerBindingOptions
[skip ci]
* BREAKING CHANGE: no longer reexport fetch plugin from aurelia package
* fix(router-lite): dont register config
* chore: cleanup unused methods, correct typings on test fixture
Auth0 integration
In this recipe, we will demonstrate how to integrate Auth0 with Aurelia 2 for authentication. Auth0 provides a flexible, drop-in solution to add authentication and authorization services to your applications.
Prerequisites
Before we begin, make sure you have:
An Auth0 account and a configured Auth0 application.
The @auth0/auth0-spa-js package installed in your Aurelia project.
Setting Up the Auth Service
First, we'll create an authentication service that will handle the interaction with Auth0.
After a successful login, Auth0 will redirect back to your application with the authentication result in the URL. We need to handle this in our application.
// src/my-app.ts
import { IRouter, ICustomElementViewModel, watch } from 'aurelia';
import { IAuthService } from './services/auth-service';
export class MyApp implements ICustomElementViewModel {
constructor(@IRouter private router: IRouter, @IAuthService private authService: IAuthService) {}
binding(): void {
this.handleAuthentication();
}
async handleAuthentication(): Promise<void> {
if (await this.authService.isAuthenticated()) {
// User is authenticated, redirect to the home route or perform other actions
} else {
await this.authService.handleAuthentication();
// Handle post-login actions
}
}
// ... Other component logic
}
Conclusion
You have now integrated Auth0 with Aurelia 2 using TypeScript. This setup provides a starting point to secure your application with Auth0. Be sure to handle tokens securely and implement proper error handling as needed. Remember to replace placeholders with your actual Auth0 domain and client ID in the AuthService.