Authentication and Authorization

Building secure applications requires proper authentication (verifying user identity) and authorization (controlling access to resources). This tutorial demonstrates how to implement authentication and route guards using Aurelia 2's router (@aurelia/router).

Table of Contents

Overview

In this tutorial, you'll learn how to:

  • Create an authentication service with login/logout functionality

  • Manage JWT tokens securely

  • Protect routes using the canLoad lifecycle hook

  • Implement global authentication guards

  • Handle unauthorized access and redirects

  • Persist authentication state across page refreshes

Setting Up Authentication Service

First, let's create an authentication service that manages user state.

JWT Token Management

For production applications, use JWT tokens for authentication. Create a token service to handle token storage and validation.

Now update the AuthService to use JWT tokens:

Creating Login and Logout

Create login and logout components that use the authentication service.

Login Component

Protecting Routes with canLoad

The canLoad lifecycle hook allows you to protect individual routes by preventing navigation if certain conditions aren't met.

Component-Level Route Guard

Preserving Intended Route

When redirecting to login, you often want to return to the intended page after successful authentication:

Update login to handle return URL:

Global Authentication Hooks

For applications with many protected routes, create a reusable authentication hook that can be applied globally.

Applying Global Hook to Routes

Register the hook in your main file:

Handling Unauthorized Access

Handle 401 Unauthorized responses from your API by automatically logging out and redirecting to login.

Persisting Authentication State

Load the user's authentication state when the application starts.

Complete Example

Here's a complete working example that ties everything together:

Project Structure

Main Configuration

App Root with Routes

Conclusion

You now have a complete authentication and authorization system with:

  • ✅ Secure JWT token management

  • ✅ Login and logout functionality

  • ✅ Protected routes using canLoad lifecycle hooks

  • ✅ Global authentication guards

  • ✅ Automatic handling of unauthorized requests

  • ✅ Persistent authentication state

  • ✅ Return URL support after login

Security Best Practices

  1. Never store sensitive data in localStorage - Only store tokens, not passwords

  2. Always use HTTPS in production - Protect tokens in transit

  3. Implement token refresh - Use refresh tokens for better security

  4. Set appropriate token expiration - Balance security and user experience

  5. Validate tokens server-side - Never trust client-side validation alone

  6. Use HTTP-only cookies - For even better security, consider HTTP-only cookies instead of localStorage

  7. Implement CSRF protection - Protect against cross-site request forgery

  8. Rate limit authentication endpoints - Prevent brute force attacks

Next Steps

  • Implement role-based authorization

  • Add password reset functionality

  • Implement multi-factor authentication (MFA)

  • Add social login (OAuth)

  • Implement refresh token rotation

For more information on the Aurelia router, see the official router documentation.

Last updated

Was this helpful?