Skip to main content

Authentication, Authorization, and Route Protection System

For authentication and session management in the web application, the NextAuth library will be used. It is an open-source solution optimized for Next.js-based applications. NextAuth allows for easy integration of multiple authentication providers and efficient management of the user's session state on the client side.

The following authentication providers are considered:

  • Credentials Provider: authentication via email and password.
  • Okta: enterprise authentication using the OAuth 2.0 protocol.

Additionally, the integration of other providers such as Azure Active Directory is contemplated according to future requirements.

It is important to note that NextAuth does not include a native user registration mechanism. In the case of the credentials provider, data validation will be handled by a function that will query the backend service, which will be responsible for the creation and persistence of user accounts. NextAuth, in turn, will manage the authentication state and the session on the frontend.

The session strategy used will be JSON Web Tokens (JWT). This approach allows for maintaining secure sessions without the need for server-side persistence and provides greater efficiency by avoiding repetitive queries. The roles and permissions defined by the backend will be included within the JWT, allowing the frontend to access them securely to implement access controls at the interface and navigation level.

Access Control and Route Protection

A role-based authorization policy is planned, reinforced at several levels:

Middleware: will intercept requests to sensitive routes and validate that the user has an active session with the appropriate permissions for the route. Otherwise, the system will redirect the user to an access-denied or login page. This layer ensures that protected routes are not rendered by unauthorized users.

Server Actions Protection: since the application will use Server Actions in the context of the App Router, explicit validation will be implemented within each action to verify both the existence of a session and the authorized roles. This prevents users without permissions from executing sensitive actions from the client, even if they manage to bypass the interface or manipulate requests.

Client-side Control: at the interface level, mechanisms will be used to condition the visibility of components and functionalities according to the user's roles. This allows for hiding irrelevant or restricted UI sections, improving the user experience and reducing unnecessary exposure of administrative or sensitive options.

Separation of Responsibilities

This approach ensures a clear separation of responsibilities between the backend and the frontend:

  • The backend will be responsible for registration, authentication, credential validation, and role assignment.
  • The frontend, through NextAuth, will manage the session state and authorization based on the roles defined by the backend.

Thanks to this architecture, a flexible, secure, and modern authentication system aligned with best practices for decoupled web applications is achieved. The combination of protection in middleware, server components, Server Actions, and client views ensures comprehensive access control coverage throughout the application.

Appendices

Okta | NextAuth.js

Securing pages and API routes | NextAuth.js

File-system conventions: middleware.js

TanStack Query