Auth0 integration
Prerequisites
Setting Up the Auth Service
// src/services/auth-service.ts
import { IAuth0Client, Auth0Client, Auth0ClientOptions } from '@auth0/auth0-spa-js';
import { DI, IContainer } from 'aurelia';
export const IAuthService = DI.createInterface<IAuthService>('IAuthService', x => x.singleton(AuthService));
export type IAuthService = AuthService;
export class AuthService {
private auth0Client: IAuth0Client;
constructor(@IContainer private container: IContainer) {
const options: Auth0ClientOptions = {
domain: 'YOUR_AUTH0_DOMAIN',
client_id: 'YOUR_AUTH0_CLIENT_ID',
redirect_uri: window.location.origin,
// ...other Auth0 client options
};
this.auth0Client = new Auth0Client(options);
}
async login(): Promise<void> {
await this.auth0Client.loginWithRedirect();
}
async handleAuthentication(): Promise<void> {
const query = window.location.search;
if (query.includes('code=') && query.includes('state=')) {
await this.auth0Client.handleRedirectCallback();
window.history.replaceState({}, document.title, window.location.pathname);
}
}
isAuthenticated(): Promise<boolean> {
return this.auth0Client.isAuthenticated();
}
getUser(): Promise<any> {
return this.auth0Client.getUser();
}
logout(): void {
this.auth0Client.logout({
returnTo: window.location.origin,
});
}
}Configuring the Auth0 Client
Setting Up the Auth0 Client Provider
Using the Auth Service in Components
Handling Authentication Callbacks
Conclusion
Last updated
Was this helpful?