{
    "componentChunkName": "component---src-pages-blog-markdown-remark-fields-slug-js",
    "path": "/blog/angular-authentication",
    "result": {"data":{"markdownRemark":{"html":"<h2 id=\"what-is-authentication-in-angular\" style=\"position:relative;\"><a href=\"#what-is-authentication-in-angular\" aria-label=\"what is authentication in angular permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What Is Authentication in Angular?</h2>\n<p>Before a user can access protected components, routes, or APIs Angular must first Authenticate them. This verification is fundamental to web security, creating a barrier that prevents unauthorized users from accessing sensitive information or performing restricted actions.</p>\n<h2 id=\"authentication-from-a-high-level\" style=\"position:relative;\"><a href=\"#authentication-from-a-high-level\" aria-label=\"authentication from a high level permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Authentication From A High Level</h2>\n<p>Authentication is essentially about confirming “who you are” before granting access. In Angular applications, this involves validating users, like usernames and passwords, or verifying tokens from a trusted identity provider. When implemented properly, authentication creates a secure foundation for your application by ensuring only verified users can enter protected areas.</p>\n<h2 id=\"why-it-matters\" style=\"position:relative;\"><a href=\"#why-it-matters\" aria-label=\"why it matters permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why It Matters</h2>\n<ul>\n<li><strong>Route Protection:</strong> Prevents unauthorized access to sensitive parts of your application</li>\n<li><strong>Data Security:</strong> Safeguards account information</li>\n<li><strong>API Protection:</strong> Ensures only authenticated users can make requests to specific backend services</li>\n<li><strong>Regulatory Compliance:</strong> Many industries require proper authentication for legal standard, e.g. SOC 2</li>\n<li><strong>User Trust:</strong> A secure systems builds confidence in your platform</li>\n</ul>\n<p>Without robust authentication, your Angular application becomes vulnerable to countless security threats, it’s only a matter of time until user data is compromised.</p>\n<h2 id=\"angulars-role\" style=\"position:relative;\"><a href=\"#angulars-role\" aria-label=\"angulars role permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Angular’s Role</h2>\n<p>Angular itself doesn’t implement authentication protocols directly. Instead, it provides a framework that makes integration with authentication systems a simple reality.</p>\n<ul>\n<li><strong>Client-side Routing:</strong> Angular’s router enables protection of routes through guards that can prevent navigation to restricted routes.</li>\n<li><strong>HTTP Interceptors:</strong>  Automatically attach authorization tokens, handle token refreshes, and manage authentication errors.</li>\n<li><strong>Reactive Forms:</strong> Handle secure collection of user credentials.</li>\n<li><strong>Services Pattern:</strong> Provides a clean battle tested architecture for authentication.</li>\n</ul>\n<p>Remember for the actual identity verification, Angular relies on external authentication providers. These can range from your own custom backend solution to open-source solutions like SuperTokens.</p>\n<p>The separation of concerns allows Angular applications to integrate with virtually any authentication system while maintaining a clean architecture focused on delivering the core user experience.</p>\n<h2 id=\"authentication-vs-authorization-in-angular\" style=\"position:relative;\"><a href=\"#authentication-vs-authorization-in-angular\" aria-label=\"authentication vs authorization in angular permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Authentication vs Authorization in Angular</h2>\n<p>People often use authentication and authorization interchangeably; this is a mistake, both serve distinct security functions in Angular applications. For a more detailed breakdown read this blog: <a href=\"https://supertokens.com/blog/authentication-vs-authorization\" target=\"_blank\" rel=\"nofollow\">Authentication vs Authorization: What’s the difference?</a></p>\n<h3 id=\"verifying-who-the-user-is\" style=\"position:relative;\"><a href=\"#verifying-who-the-user-is\" aria-label=\"verifying who the user is permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Verifying Who The User Is:</h3>\n<p>Authentication is fundamentally about identity verification, confirming that users are who they claim to be; this involves:</p>\n<ul>\n<li>Username and password validation</li>\n<li>Social login integration</li>\n<li>Biometric Verification</li>\n<li>Multi-factor authentication (MFA)</li>\n<li>Single sign-on (SSO) systems</li>\n</ul>\n<p>The auth process usually results in a security token being issued that serves as digital proof for subsequent requests.</p>\n<h2 id=\"authorization--determining-what-they-can-access\" style=\"position:relative;\"><a href=\"#authorization--determining-what-they-can-access\" aria-label=\"authorization  determining what they can access permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Authorization – Determining What They Can Access</h2>\n<p>Authorization, on the other hand, determines what authenticated users can do within your application. This involves:</p>\n<ul>\n<li>Defines and enforces permission boundaries</li>\n<li>Controls access to specific routes, components, and features</li>\n<li>Restricts data visibility based on user roles</li>\n<li>Manages feature availability according to subscription levels</li>\n<li>Prevents unauthorized operations on backend resources</li>\n</ul>\n<p>Even after a user authenticates successfully, authorization ensures they can only access the resources appropriate for their role or permission level.</p>\n<h2 id=\"common-confusion-in-angular\" style=\"position:relative;\"><a href=\"#common-confusion-in-angular\" aria-label=\"common confusion in angular permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Common Confusion in Angular</h2>\n<p>A prevalent misconception among Angular developers is treating route guards as complete authentication solutions, rather than authorization mechanisms. This confusion can lead to security gaps in application design.</p>\n<p>Route guards like <code class=\"language-text\">CanActivate</code> and <code class=\"language-text\">CanLoad</code> are primarily authorization tools that enforce access rules, but they rely on a proper authentication system to function correctly. The common points of confusion include:</p>\n<ul>\n<li><strong>Guards Don’t Authenticate</strong>: Route guards check if a user is authorized to access a route but don’t perform the actual authentication. They typically consult an authentication service that manages the user’s authenticated state.</li>\n<li><strong>Client-Side Vulnerability</strong>: Since route guards run in the browser, they can be bypassed by savvy users if not backed by server-side authorization checks. Guards should be viewed as a UX enhancement rather than the sole security measure.</li>\n<li><strong>Token Validation</strong>: Many developers mistakenly assume checking for the presence of a token in local storage is sufficient for authentication. Proper implementation requires validating the token’s integrity, expiration, and permissions.</li>\n</ul>\n<p>For secure Angular applications, authentication and authorization should work in harmony—authentication services verify identity and manage tokens, while route guards use this authentication state to enforce access policies throughout the application. The backend must always validate both authentication and authorization independently, as client-side checks can be circumvented.</p>\n<h2 id=\"core-components-of-angular-authentication\" style=\"position:relative;\"><a href=\"#core-components-of-angular-authentication\" aria-label=\"core components of angular authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Core Components of Angular Authentication</h2>\n<p>A robust authentication system in Angular relies on several key components working together to create a secure user experience.</p>\n<h3 id=\"login-ui-form\" style=\"position:relative;\"><a href=\"#login-ui-form\" aria-label=\"login ui form permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Login UI Form</h3>\n<p>The login form is your authentication system’s entry point, available in two main implementations:</p>\n<p><strong>Custom-built forms</strong> offer maximum flexibility, with reactive forms providing type safety and robust validation. These forms can be precisely tailored to match your application’s specific requirements and user flows.</p>\n<p><strong>Component library-based forms</strong> accelerate development through pre-built, tested components from libraries like Angular Material or NgBootstrap. These ready-made solutions handle common authentication patterns while allowing customization to match your design.</p>\n<p>An effective login interface balances security with usability, incorporating elements like password strength indicators and meaningful error messages.</p>\n<h3 id=\"http-interceptors\" style=\"position:relative;\"><a href=\"#http-interceptors\" aria-label=\"http interceptors permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>HTTP Interceptors</h3>\n<p>Interceptors are middleware components that transparently manage authentication across API requests by:</p>\n<ul>\n<li>Automatically adding authorization tokens to outgoing requests</li>\n<li>Managing token refresh flows when credentials expire</li>\n<li>Handling authentication-related errors</li>\n<li>Implementing logout logic when authentication fails</li>\n</ul>\n<p>This centralized token management eliminates the need to manually handle authentication headers throughout your codebase, improving security and reducing code duplication.</p>\n<h3 id=\"route-guards\" style=\"position:relative;\"><a href=\"#route-guards\" aria-label=\"route guards permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Route Guards</h3>\n<p>Guards protect navigation paths based on authentication status, connecting your auth system to Angular’s routing framework:</p>\n<ul>\n<li><strong>CanActivate</strong>: Controls access to routes based on authentication</li>\n<li><strong>CanLoad</strong>: Prevents unauthorized loading of lazy-loaded modules</li>\n<li><strong>CanActivateChild</strong>: Protects child routes within a configuration</li>\n<li><strong>CanDeactivate</strong>: Controls navigation away from protected content</li>\n</ul>\n<p>These guards create a coherent security layer that directs users to appropriate content while preventing unauthorized access to protected resources.</p>\n<h3 id=\"state-management\" style=\"position:relative;\"><a href=\"#state-management\" aria-label=\"state management permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>State Management</h3>\n<p>Authentication state must be securely maintained throughout the user session:</p>\n<p><strong>Browser storage</strong> (localStorage/sessionStorage) offers simple persistence but with security limitations regarding XSS vulnerabilities.</p>\n<p><strong>Cookies</strong> provide enhanced security options through HttpOnly, Secure, and SameSite flags that protect against common attack vectors.</p>\n<p><strong>State management libraries</strong> like NgRx or NGXS offer robust solutions for complex authentication flows in larger applications, with features like centralized state and action tracking.</p>\n<p><strong>In-memory services</strong> balance security and simplicity by keeping authentication state in application memory, reducing exposure to client-side attacks.</p>\n<p>The right combination of these components creates an authentication system that effectively protects your application while providing a seamless experience for legitimate users.</p>\n<h2 id=\"popular-authentication-methods-in-angular\" style=\"position:relative;\"><a href=\"#popular-authentication-methods-in-angular\" aria-label=\"popular authentication methods in angular permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Popular Authentication Methods in Angular</h2>\n<h3 id=\"token-based-jwt\" style=\"position:relative;\"><a href=\"#token-based-jwt\" aria-label=\"token based jwt permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Token-Based (JWT)</h3>\n<p>JWT (JSON Web Token) authentication is the most widely adopted method in modern Angular applications. After successful login, the server generates a signed JWT containing encoded user information and permissions. This token is then:</p>\n<ul>\n<li>Stored on the client (localStorage, sessionStorage, or memory)</li>\n<li>Attached to subsequent API requests via HTTP interceptors</li>\n<li>Validated by the server without requiring database lookups</li>\n</ul>\n<p>JWTs are self-contained and stateless, making them ideal for scalable applications. However, they require careful implementation regarding token expiration, refresh strategies, and secure storage to prevent XSS attacks.</p>\n<h3 id=\"session-based-auth\" style=\"position:relative;\"><a href=\"#session-based-auth\" aria-label=\"session based auth permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Session-Based Auth</h3>\n<p>The traditional authentication approach uses server-side sessions paired with client cookies. When implementing this in Angular:</p>\n<ul>\n<li>Server creates a session ID upon successful login</li>\n<li>Session ID is stored in an HttpOnly cookie</li>\n<li>Server maintains session state and validates cookies on each request</li>\n<li>Angular handles CSRF protection through interceptors</li>\n</ul>\n<p>This method offers stronger security against XSS attacks through HttpOnly cookies but creates server-side state that can complicate scaling. Modern implementations often use Redis or similar technologies to manage distributed session storage.</p>\n<h3 id=\"oauth-20--oidc-integration\" style=\"position:relative;\"><a href=\"#oauth-20--oidc-integration\" aria-label=\"oauth 20  oidc integration permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>OAuth 2.0 + OIDC Integration</h3>\n<p>For applications requiring social logins or enterprise single sign-on (SSO), OAuth 2.0 with OpenID Connect provides a standardized authentication framework:</p>\n<ul>\n<li>Angular delegates authentication to trusted providers (Google, Microsoft, Auth0)</li>\n<li>Libraries like <code class=\"language-text\">angular-oauth2-oidc</code> simplify integration</li>\n<li>Authorization code flow with PKCE is recommended for SPA security</li>\n<li>Access tokens, ID tokens, and refresh tokens are managed separately</li>\n</ul>\n<p>This approach offloads complex security concerns to specialized providers while enabling seamless integration with existing identity ecosystems. It’s particularly valuable for enterprise applications requiring compliance with security standards.</p>\n<h3 id=\"passwordless\" style=\"position:relative;\"><a href=\"#passwordless\" aria-label=\"passwordless permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Passwordless</h3>\n<p>Increasingly popular passwordless authentication eliminates password vulnerabilities through alternative verification methods:</p>\n<ul>\n<li>Magic links sent to verified email addresses</li>\n<li>One-time passcodes (OTPs) delivered via SMS or email</li>\n<li>WebAuthn/FIDO2 for biometric and security key authentication</li>\n</ul>\n<p>Libraries like SuperTokens provide Angular-compatible implementations that handle the complex workflows involved in secure passwordless authentication. This method improves security by eliminating password-related vulnerabilities while often enhancing user experience through simplified login flows.</p>\n<p>Each authentication method has distinct security characteristics and implementation requirements. Many modern Angular applications implement hybrid approaches, combining multiple methods to balance security with user experience based on specific application requirements.</p>\n<h2 id=\"step-by-step-guide--implementing-jwt-auth-in-angular\" style=\"position:relative;\"><a href=\"#step-by-step-guide--implementing-jwt-auth-in-angular\" aria-label=\"step by step guide  implementing jwt auth in angular permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step-by-Step Guide – Implementing JWT Auth in Angular</h2>\n<p>Before we begin let’s make sure we have a project structure<br>\n<code class=\"language-text\">mkdir jwt-auth-project</code></p>\n<blockquote>\n<p>NOTE: All code for this project can be found at this <a href=\"https://github.com/msaldivar/SuperTokens-Blog-Related-Code/tree/main/angular_authentication\" target=\"_blank\" rel=\"nofollow\">repo</a></p>\n</blockquote>\n<ol>\n<li>\n<h3 id=\"set-up-the-backend-to-issue-jwts\" style=\"position:relative;\"><a href=\"#set-up-the-backend-to-issue-jwts\" aria-label=\"set up the backend to issue jwts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Set up the Backend to Issue JWTs</h3>\n<p>We’ll be making use of Node.js for our backend</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"> cd jwt-auth-project\n mkdir backend\n cd backend</code></pre></div>\n<p>use <code class=\"language-text\">npm</code> to init our project</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">npm init -y\nnpm install express jsonwebtoken cors dotenv cookie-parser</code></pre></div>\n<p>Create a simple js server that can issue JWT tokens, provides public and private api routes, and some traceability so we can see what’s happening in our system.</p>\n<p>File: <code class=\"language-text\">backend/server.js</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"51622783775459280000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const express = require('express');\nconst cors = require('cors');\nconst jwt = require('jsonwebtoken');\nconst cookieParser = require('cookie-parser');\nrequire('dotenv').config();\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\nconst JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key';\n\napp.use(cors({\norigin: 'http://localhost:4200',\ncredentials: true // Important for cookies\n}));\napp.use(express.json());\napp.use(cookieParser()); // Needed to parse cookies from requests\n\n// Mock user database\n// IMPORTANT: In a production environment, NEVER store passwords in plain text.\n// Always use a hashing library like bcrypt to hash passwords securely.\nconst users = [{\n    id: 1,\n    username: 'miles@web.com',\n    password: 'e-1610'\n}];\n\napp.post('/api/login', (req, res) => {\nconsole.log('Login attempt received:', req.body);\n\nconst { username, password } = req.body;\nconsole.log('Credentials extracted:', { username, password });\n\nconst user = users.find(u => u.username === username);\nconsole.log('User found:', user ? 'Yes' : 'No');\n\nif (!user) {\n    console.log('User not found, returning 401');\n    return res.status(401).json({ message: 'Invalid credentials' });\n}\n\nconst isPasswordValid = password === user.password;\nconsole.log('Password valid:', isPasswordValid);\nconsole.log('Received password:', password);\nconsole.log('Stored password:', user.password);\n\nif (!isPasswordValid) {\n    console.log('Password invalid, returning 401');\n    return res.status(401).json({ message: 'Invalid credentials' });\n}\nconsole.log('Authentication successful, generating token');\n\n// Generate JWT\nconst token = jwt.sign(\n    { id: user.id, username: user.username },\n    JWT_SECRET,\n    { expiresIn: '1h' }\n);\n\n// Option 1: Send JWT as HttpOnly cookie (more secure)\nres.cookie('jwt_token', token, {\n    httpOnly: true,\n    secure: process.env.NODE_ENV === 'production', // HTTPS only in production\n    sameSite: 'strict',\n    maxAge: 3600000 // 1 hour\n});\nconsole.log('Token generated and cookie set');\n\n// Option 2: Send JWT in response body (for sessionStorage)\nres.json({ \n    message: 'Login successful',\n    token, // Remove this in production if using HttpOnly cookies\n    user: { id: user.id, username: user.username }\n});\nconsole.log('Login response sent');\n});\n\n// User must be authd for this route\napp.get('/api/protected', verifyToken, (req, res) => {\nconsole.log('Protected route accessed by:', req.user?.username);\nres.json({ \n    message: 'This is protected data', \n    user: req.user,\n    timestamp: new Date().toISOString()\n});\n});\n\n// Token verification middleware\nfunction verifyToken(req, res, next) {\nconsole.log('Verifying token...');\n\n// Option 1: Get token from cookies\nconst tokenFromCookie = req.cookies?.jwt_token;\nconsole.log('Token from cookie:', tokenFromCookie ? 'Present' : 'Not present');\n\n// Option 2: Get token from Authorization header\nconst authHeader = req.headers.authorization;\nconst tokenFromHeader = authHeader && authHeader.split(' ')[1];\nconsole.log('Token from header:', tokenFromHeader ? 'Present' : 'Not present');\n\nconst token = tokenFromCookie || tokenFromHeader;\n\nif (!token) {\n    console.log('No token found, access denied');\n    return res.status(401).json({ message: 'Access denied' });\n}\n\ntry {\n    const decoded = jwt.verify(token, JWT_SECRET);\n    console.log('Token verified successfully for user:', decoded.username);\n    req.user = decoded;\n    next();\n} catch (error) {\n    console.error('Token verification failed:', error.message);\n    res.status(401).json({ message: 'Invalid token' });\n}\n}\n\napp.post('/api/logout', (req, res) => {\nconsole.log('Logout request received');\n\nres.clearCookie('jwt_token');\nconsole.log('JWT cookie cleared');\nres.json({ message: 'Logged out successfully' });\n});\n\napp.get('/api/hello', (req, res) => {\nconsole.log('Hello endpoint accessed');\nres.json({ \n    message: 'Hello from the server!',\n    timestamp: new Date().toISOString()\n});\n});\n\napp.listen(PORT, () => {\nconsole.log(\\`Server running on http://localhost:\\${PORT}\\`);\nconsole.log('Test credentials: user@example.com / auth123');\nconsole.log('Available endpoints:');\nconsole.log('  POST /api/login - Login endpoint');\nconsole.log('  GET /api/protected - Protected endpoint (requires authentication)');\nconsole.log('  POST /api/logout - Logout endpoint');\nconsole.log('  GET /api/hello - Public test endpoint');\n});`, `51622783775459280000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> express <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'express'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> cors <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'cors'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> jwt <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'jsonwebtoken'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> cookieParser <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'cookie-parser'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'dotenv'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">config</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> app <span class=\"token operator\">=</span> <span class=\"token function\">express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> <span class=\"token constant\">PORT</span> <span class=\"token operator\">=</span> process<span class=\"token punctuation\">.</span>env<span class=\"token punctuation\">.</span><span class=\"token constant\">PORT</span> <span class=\"token operator\">||</span> <span class=\"token number\">3000</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> <span class=\"token constant\">JWT_SECRET</span> <span class=\"token operator\">=</span> process<span class=\"token punctuation\">.</span>env<span class=\"token punctuation\">.</span><span class=\"token constant\">JWT_SECRET</span> <span class=\"token operator\">||</span> <span class=\"token string\">'your-secret-key'</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span><span class=\"token function\">cors</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n<span class=\"token literal-property property\">origin</span><span class=\"token operator\">:</span> <span class=\"token string\">'http://localhost:4200'</span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">credentials</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token comment\">// Important for cookies</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>express<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span><span class=\"token function\">cookieParser</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Needed to parse cookies from requests</span>\n\n<span class=\"token comment\">// Mock user database</span>\n<span class=\"token comment\">// IMPORTANT: In a production environment, NEVER store passwords in plain text.</span>\n<span class=\"token comment\">// Always use a hashing library like bcrypt to hash passwords securely.</span>\n<span class=\"token keyword\">const</span> users <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">id</span><span class=\"token operator\">:</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">username</span><span class=\"token operator\">:</span> <span class=\"token string\">'miles@web.com'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">password</span><span class=\"token operator\">:</span> <span class=\"token string\">'e-1610'</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">post</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/api/login'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Login attempt received:'</span><span class=\"token punctuation\">,</span> req<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> username<span class=\"token punctuation\">,</span> password <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> req<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Credentials extracted:'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> username<span class=\"token punctuation\">,</span> password <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> user <span class=\"token operator\">=</span> users<span class=\"token punctuation\">.</span><span class=\"token function\">find</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">u</span> <span class=\"token operator\">=></span> u<span class=\"token punctuation\">.</span>username <span class=\"token operator\">===</span> username<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'User found:'</span><span class=\"token punctuation\">,</span> user <span class=\"token operator\">?</span> <span class=\"token string\">'Yes'</span> <span class=\"token operator\">:</span> <span class=\"token string\">'No'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>user<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'User not found, returning 401'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">401</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Invalid credentials'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">const</span> isPasswordValid <span class=\"token operator\">=</span> password <span class=\"token operator\">===</span> user<span class=\"token punctuation\">.</span>password<span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Password valid:'</span><span class=\"token punctuation\">,</span> isPasswordValid<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Received password:'</span><span class=\"token punctuation\">,</span> password<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Stored password:'</span><span class=\"token punctuation\">,</span> user<span class=\"token punctuation\">.</span>password<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>isPasswordValid<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Password invalid, returning 401'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">401</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Invalid credentials'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Authentication successful, generating token'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// Generate JWT</span>\n<span class=\"token keyword\">const</span> token <span class=\"token operator\">=</span> jwt<span class=\"token punctuation\">.</span><span class=\"token function\">sign</span><span class=\"token punctuation\">(</span>\n    <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">id</span><span class=\"token operator\">:</span> user<span class=\"token punctuation\">.</span>id<span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">username</span><span class=\"token operator\">:</span> user<span class=\"token punctuation\">.</span>username <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token constant\">JWT_SECRET</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">expiresIn</span><span class=\"token operator\">:</span> <span class=\"token string\">'1h'</span> <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// Option 1: Send JWT as HttpOnly cookie (more secure)</span>\nres<span class=\"token punctuation\">.</span><span class=\"token function\">cookie</span><span class=\"token punctuation\">(</span><span class=\"token string\">'jwt_token'</span><span class=\"token punctuation\">,</span> token<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">httpOnly</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">secure</span><span class=\"token operator\">:</span> process<span class=\"token punctuation\">.</span>env<span class=\"token punctuation\">.</span><span class=\"token constant\">NODE_ENV</span> <span class=\"token operator\">===</span> <span class=\"token string\">'production'</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// HTTPS only in production</span>\n    <span class=\"token literal-property property\">sameSite</span><span class=\"token operator\">:</span> <span class=\"token string\">'strict'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">maxAge</span><span class=\"token operator\">:</span> <span class=\"token number\">3600000</span> <span class=\"token comment\">// 1 hour</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Token generated and cookie set'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// Option 2: Send JWT in response body (for sessionStorage)</span>\nres<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> \n    <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Login successful'</span><span class=\"token punctuation\">,</span>\n    token<span class=\"token punctuation\">,</span> <span class=\"token comment\">// Remove this in production if using HttpOnly cookies</span>\n    <span class=\"token literal-property property\">user</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">id</span><span class=\"token operator\">:</span> user<span class=\"token punctuation\">.</span>id<span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">username</span><span class=\"token operator\">:</span> user<span class=\"token punctuation\">.</span>username <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Login response sent'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// User must be authd for this route</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/api/protected'</span><span class=\"token punctuation\">,</span> verifyToken<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Protected route accessed by:'</span><span class=\"token punctuation\">,</span> req<span class=\"token punctuation\">.</span>user<span class=\"token operator\">?.</span>username<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nres<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> \n    <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'This is protected data'</span><span class=\"token punctuation\">,</span> \n    <span class=\"token literal-property property\">user</span><span class=\"token operator\">:</span> req<span class=\"token punctuation\">.</span>user<span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">timestamp</span><span class=\"token operator\">:</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Date</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">toISOString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// Token verification middleware</span>\n<span class=\"token keyword\">function</span> <span class=\"token function\">verifyToken</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res<span class=\"token punctuation\">,</span> next</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Verifying token...'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// Option 1: Get token from cookies</span>\n<span class=\"token keyword\">const</span> tokenFromCookie <span class=\"token operator\">=</span> req<span class=\"token punctuation\">.</span>cookies<span class=\"token operator\">?.</span>jwt_token<span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Token from cookie:'</span><span class=\"token punctuation\">,</span> tokenFromCookie <span class=\"token operator\">?</span> <span class=\"token string\">'Present'</span> <span class=\"token operator\">:</span> <span class=\"token string\">'Not present'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// Option 2: Get token from Authorization header</span>\n<span class=\"token keyword\">const</span> authHeader <span class=\"token operator\">=</span> req<span class=\"token punctuation\">.</span>headers<span class=\"token punctuation\">.</span>authorization<span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> tokenFromHeader <span class=\"token operator\">=</span> authHeader <span class=\"token operator\">&amp;&amp;</span> authHeader<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">' '</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Token from header:'</span><span class=\"token punctuation\">,</span> tokenFromHeader <span class=\"token operator\">?</span> <span class=\"token string\">'Present'</span> <span class=\"token operator\">:</span> <span class=\"token string\">'Not present'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> token <span class=\"token operator\">=</span> tokenFromCookie <span class=\"token operator\">||</span> tokenFromHeader<span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>token<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'No token found, access denied'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">401</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Access denied'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">try</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">const</span> decoded <span class=\"token operator\">=</span> jwt<span class=\"token punctuation\">.</span><span class=\"token function\">verify</span><span class=\"token punctuation\">(</span>token<span class=\"token punctuation\">,</span> <span class=\"token constant\">JWT_SECRET</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Token verified successfully for user:'</span><span class=\"token punctuation\">,</span> decoded<span class=\"token punctuation\">.</span>username<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    req<span class=\"token punctuation\">.</span>user <span class=\"token operator\">=</span> decoded<span class=\"token punctuation\">;</span>\n    <span class=\"token function\">next</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">catch</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Token verification failed:'</span><span class=\"token punctuation\">,</span> error<span class=\"token punctuation\">.</span>message<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">401</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Invalid token'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">post</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/api/logout'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Logout request received'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\nres<span class=\"token punctuation\">.</span><span class=\"token function\">clearCookie</span><span class=\"token punctuation\">(</span><span class=\"token string\">'jwt_token'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'JWT cookie cleared'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nres<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Logged out successfully'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/api/hello'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Hello endpoint accessed'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nres<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> \n    <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Hello from the server!'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">timestamp</span><span class=\"token operator\">:</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Date</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">toISOString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span><span class=\"token constant\">PORT</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Server running on http://localhost:</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span><span class=\"token constant\">PORT</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Test credentials: user@example.com / auth123'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Available endpoints:'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'  POST /api/login - Login endpoint'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'  GET /api/protected - Protected endpoint (requires authentication)'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'  POST /api/logout - Logout endpoint'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'  GET /api/hello - Public test endpoint'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>File: <code class=\"language-text\">backend/.env</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"72320443055444980000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`JWT_SECRET=your-secret-key \nPORT=3000`, `72320443055444980000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token constant\">JWT_SECRET</span><span class=\"token operator\">=</span>your<span class=\"token operator\">-</span>secret<span class=\"token operator\">-</span>key \n<span class=\"token constant\">PORT</span><span class=\"token operator\">=</span><span class=\"token number\">3000</span></code></pre></div>\n<p>The <code class=\"language-text\">.env</code> file contains environment variables that your server uses at runtime.</p>\n<p><strong>Note:</strong> With the <code class=\"language-text\">.env</code> file the default values in server.js will be used, which is fine for development but not secure for production.</p>\n</li>\n<li>\n<h3 id=\"login-component-with-form-submission\" style=\"position:relative;\"><a href=\"#login-component-with-form-submission\" aria-label=\"login component with form submission permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Login Component with Form Submission</h3>\n<p>Let’s create the Angular Authentication Components, navigate to the top level project directory and run the <code class=\"language-text\">ng new</code> cmd. This will create a <code class=\"language-text\">frontend</code> directory.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">ng new frontend --routing=true --style=\"scss\" --package-manager=\"npm\" --skip-git=true  --skip-tests=true</code></pre></div>\n<p>File: <code class=\"language-text\">frontend/src/app/auth/auth.service.ts</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"26446253001565843000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { Injectable } from '@angular/core';\nimport { HttpClient } from '@angular/common/http';\nimport { BehaviorSubject, Observable, tap } from 'rxjs';\n\n@Injectable({\nprovidedIn: 'root'\n})\nexport class AuthService {\nprivate apiUrl = 'http://localhost:3000/api';\nprivate currentUserSubject = new BehaviorSubject<any>(null);\npublic currentUser\\$ = this.currentUserSubject.asObservable();\n\nconstructor(private http: HttpClient) {\n    // Check sessionStorage for existing user on service init\n    const storedUser = sessionStorage.getItem('currentUser');\n    if (storedUser) {\n    this.currentUserSubject.next(JSON.parse(storedUser));\n    }\n}\n\nlogin(username: string, password: string): Observable<any> {\n    console.log('Login attempt:', username);\n    \n    return this.http.post<any>(\\`\\${this.apiUrl}/login\\`, { username, password }, {\n    withCredentials: true // Important for cookies\n    }).pipe(\n    tap(response => {\n        console.log('Login response received:', response);\n        \n        // If using sessionStorage (Option 2)\n        if (response.token) {\n        const user = {\n            ...response.user,\n            token: response.token\n        };\n        \n        // Store user details in sessionStorage\n        sessionStorage.setItem('currentUser', JSON.stringify(user));\n        this.currentUserSubject.next(user);\n        console.log('User stored in session storage');\n        }\n        // If using HttpOnly cookies (Option 1)\n        else if (response.user) {\n        this.currentUserSubject.next(response.user);\n        }\n    })\n    );\n}\n\nlogout(): Observable<any> {\n    console.log('Logout service method called');\n    \n    return this.http.post<any>(\\`\\${this.apiUrl}/logout\\`, {}, {\n    withCredentials: true // Important for cookies\n    }).pipe(\n    tap(() => {\n        console.log('Logout response received, clearing session storage');\n        // Clear sessionStorage\n        sessionStorage.removeItem('currentUser');\n        this.currentUserSubject.next(null);\n        console.log('Current user reset to null');\n    })\n    );\n}\n\nget currentUserValue() {\n    return this.currentUserSubject.value;\n}\n\nisLoggedIn(): boolean {\n    return !!this.currentUserValue;\n}\n}`, `26446253001565843000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Injectable <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/core'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> HttpClient <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/common/http'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> BehaviorSubject<span class=\"token punctuation\">,</span> Observable<span class=\"token punctuation\">,</span> tap <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'rxjs'</span><span class=\"token punctuation\">;</span>\n\n@<span class=\"token function\">Injectable</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n<span class=\"token literal-property property\">providedIn</span><span class=\"token operator\">:</span> <span class=\"token string\">'root'</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">AuthService</span> <span class=\"token punctuation\">{</span>\n<span class=\"token keyword\">private</span> apiUrl <span class=\"token operator\">=</span> <span class=\"token string\">'http://localhost:3000/api'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">private</span> currentUserSubject <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">BehaviorSubject</span><span class=\"token operator\">&lt;</span>any<span class=\"token operator\">></span><span class=\"token punctuation\">(</span><span class=\"token keyword\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">public</span> currentUser$ <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>currentUserSubject<span class=\"token punctuation\">.</span><span class=\"token function\">asObservable</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token keyword\">private</span> <span class=\"token literal-property property\">http</span><span class=\"token operator\">:</span> HttpClient</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// Check sessionStorage for existing user on service init</span>\n    <span class=\"token keyword\">const</span> storedUser <span class=\"token operator\">=</span> sessionStorage<span class=\"token punctuation\">.</span><span class=\"token function\">getItem</span><span class=\"token punctuation\">(</span><span class=\"token string\">'currentUser'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>storedUser<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>currentUserSubject<span class=\"token punctuation\">.</span><span class=\"token function\">next</span><span class=\"token punctuation\">(</span><span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">parse</span><span class=\"token punctuation\">(</span>storedUser<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">login</span><span class=\"token punctuation\">(</span>username<span class=\"token operator\">:</span> string<span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">password</span><span class=\"token operator\">:</span> string<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> Observable<span class=\"token operator\">&lt;</span>any<span class=\"token operator\">></span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Login attempt:'</span><span class=\"token punctuation\">,</span> username<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    \n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>http<span class=\"token punctuation\">.</span>post<span class=\"token operator\">&lt;</span>any<span class=\"token operator\">></span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>apiUrl<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">/login</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> username<span class=\"token punctuation\">,</span> password <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">withCredentials</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token comment\">// Important for cookies</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">pipe</span><span class=\"token punctuation\">(</span>\n    <span class=\"token function\">tap</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">response</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Login response received:'</span><span class=\"token punctuation\">,</span> response<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        \n        <span class=\"token comment\">// If using sessionStorage (Option 2)</span>\n        <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>response<span class=\"token punctuation\">.</span>token<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">const</span> user <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n            <span class=\"token operator\">...</span>response<span class=\"token punctuation\">.</span>user<span class=\"token punctuation\">,</span>\n            <span class=\"token literal-property property\">token</span><span class=\"token operator\">:</span> response<span class=\"token punctuation\">.</span>token\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n        \n        <span class=\"token comment\">// Store user details in sessionStorage</span>\n        sessionStorage<span class=\"token punctuation\">.</span><span class=\"token function\">setItem</span><span class=\"token punctuation\">(</span><span class=\"token string\">'currentUser'</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span>user<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>currentUserSubject<span class=\"token punctuation\">.</span><span class=\"token function\">next</span><span class=\"token punctuation\">(</span>user<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'User stored in session storage'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span>\n        <span class=\"token comment\">// If using HttpOnly cookies (Option 1)</span>\n        <span class=\"token keyword\">else</span> <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>response<span class=\"token punctuation\">.</span>user<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>currentUserSubject<span class=\"token punctuation\">.</span><span class=\"token function\">next</span><span class=\"token punctuation\">(</span>response<span class=\"token punctuation\">.</span>user<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">logout</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> Observable<span class=\"token operator\">&lt;</span>any<span class=\"token operator\">></span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Logout service method called'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    \n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>http<span class=\"token punctuation\">.</span>post<span class=\"token operator\">&lt;</span>any<span class=\"token operator\">></span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>apiUrl<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">/logout</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">withCredentials</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token comment\">// Important for cookies</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">pipe</span><span class=\"token punctuation\">(</span>\n    <span class=\"token function\">tap</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Logout response received, clearing session storage'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token comment\">// Clear sessionStorage</span>\n        sessionStorage<span class=\"token punctuation\">.</span><span class=\"token function\">removeItem</span><span class=\"token punctuation\">(</span><span class=\"token string\">'currentUser'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>currentUserSubject<span class=\"token punctuation\">.</span><span class=\"token function\">next</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Current user reset to null'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">get</span> <span class=\"token function\">currentUserValue</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>currentUserSubject<span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">isLoggedIn</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> boolean <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token operator\">!</span><span class=\"token operator\">!</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>currentUserValue<span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>File: <code class=\"language-text\">frontend/src/app/auth/login/login.component.ts</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"20071527825043313000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { Component, OnInit } from '@angular/core';\nimport { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';\nimport { Router } from '@angular/router';\nimport { CommonModule } from '@angular/common';\nimport { HttpClient } from '@angular/common/http';\nimport { AuthService } from '../auth.service';\n\n@Component({\nselector: 'app-login',\nstandalone: true,\nimports: [CommonModule, ReactiveFormsModule], // These are critical!\ntemplateUrl: './login.component.html',\nstyleUrls: ['./login.component.scss']\n})\nexport class LoginComponent implements OnInit {\nloginForm!: FormGroup;\nloading = false;\nerror = '';\napiResponse: any = null; // Property for API test results\n\nconstructor(\n    private formBuilder: FormBuilder,\n    private authService: AuthService,\n    private router: Router,\n    private http: HttpClient // Required for API testing\n) { }\n\nngOnInit(): void {\n    this.loginForm = this.formBuilder.group({\n    username: ['', [Validators.required, Validators.email]],\n    password: ['', Validators.required]\n    });\n    \n    // Redirect if already logged in\n    if (this.authService.isLoggedIn()) {\n    this.router.navigate(['/dashboard']);\n    }\n}\n\nonSubmit(): void {\n    if (this.loginForm.invalid) {\n    return;\n    }\n    \n    this.loading = true;\n    this.error = '';\n    \n    const { username, password } = this.loginForm.value;\n    \n    this.authService.login(username, password)\n    .subscribe({\n        next: () => {\n        this.router.navigate(['/dashboard']);\n        },\n        error: err => {\n        this.error = err.error?.message || 'Login failed';\n        this.loading = false;\n        }\n    });\n}\n\n// Add testApi method for API testing\ntestApi(endpoint: 'hello' | 'protected'): void {\n    console.log(\\`Testing \\${endpoint} API endpoint\\`);\n    this.apiResponse = null; // Clear previous response\n    \n    this.http.get(\\`http://localhost:3000/api/\\${endpoint}\\`, {\n    withCredentials: true // Important for cookies\n    }).subscribe({\n    next: (response) => {\n        console.log(\\`\\${endpoint} API response:\\`, response);\n        this.apiResponse = response;\n    },\n    error: (error) => {\n        console.error(\\`\\${endpoint} API error:\\`, error);\n        this.apiResponse = {\n        error: true,\n        message: error.error?.message || error.statusText || 'Unknown error',\n        status: error.status\n        };\n    }\n    });\n}\n}`, `20071527825043313000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Component<span class=\"token punctuation\">,</span> OnInit <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/core'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> FormBuilder<span class=\"token punctuation\">,</span> FormGroup<span class=\"token punctuation\">,</span> Validators<span class=\"token punctuation\">,</span> ReactiveFormsModule <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/forms'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Router <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/router'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> CommonModule <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/common'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> HttpClient <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/common/http'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> AuthService <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'../auth.service'</span><span class=\"token punctuation\">;</span>\n\n@<span class=\"token function\">Component</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n<span class=\"token literal-property property\">selector</span><span class=\"token operator\">:</span> <span class=\"token string\">'app-login'</span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">standalone</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">imports</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>CommonModule<span class=\"token punctuation\">,</span> ReactiveFormsModule<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// These are critical!</span>\n<span class=\"token literal-property property\">templateUrl</span><span class=\"token operator\">:</span> <span class=\"token string\">'./login.component.html'</span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">styleUrls</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'./login.component.scss'</span><span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">LoginComponent</span> <span class=\"token keyword\">implements</span> <span class=\"token class-name\">OnInit</span> <span class=\"token punctuation\">{</span>\nloginForm<span class=\"token operator\">!</span><span class=\"token operator\">:</span> FormGroup<span class=\"token punctuation\">;</span>\nloading <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span>\nerror <span class=\"token operator\">=</span> <span class=\"token string\">''</span><span class=\"token punctuation\">;</span>\n<span class=\"token literal-property property\">apiResponse</span><span class=\"token operator\">:</span> any <span class=\"token operator\">=</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Property for API test results</span>\n\n<span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span>\n    <span class=\"token keyword\">private</span> <span class=\"token literal-property property\">formBuilder</span><span class=\"token operator\">:</span> FormBuilder<span class=\"token punctuation\">,</span>\n    <span class=\"token keyword\">private</span> <span class=\"token literal-property property\">authService</span><span class=\"token operator\">:</span> AuthService<span class=\"token punctuation\">,</span>\n    <span class=\"token keyword\">private</span> <span class=\"token literal-property property\">router</span><span class=\"token operator\">:</span> Router<span class=\"token punctuation\">,</span>\n    <span class=\"token keyword\">private</span> <span class=\"token literal-property property\">http</span><span class=\"token operator\">:</span> HttpClient <span class=\"token comment\">// Required for API testing</span>\n<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span> <span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">ngOnInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token keyword\">void</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>loginForm <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>formBuilder<span class=\"token punctuation\">.</span><span class=\"token function\">group</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">username</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token string\">''</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span>Validators<span class=\"token punctuation\">.</span>required<span class=\"token punctuation\">,</span> Validators<span class=\"token punctuation\">.</span>email<span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">password</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token string\">''</span><span class=\"token punctuation\">,</span> Validators<span class=\"token punctuation\">.</span>required<span class=\"token punctuation\">]</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    \n    <span class=\"token comment\">// Redirect if already logged in</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>authService<span class=\"token punctuation\">.</span><span class=\"token function\">isLoggedIn</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>router<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">'/dashboard'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">onSubmit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token keyword\">void</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>loginForm<span class=\"token punctuation\">.</span>invalid<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    \n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>loading <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>error <span class=\"token operator\">=</span> <span class=\"token string\">''</span><span class=\"token punctuation\">;</span>\n    \n    <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> username<span class=\"token punctuation\">,</span> password <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>loginForm<span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">;</span>\n    \n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>authService<span class=\"token punctuation\">.</span><span class=\"token function\">login</span><span class=\"token punctuation\">(</span>username<span class=\"token punctuation\">,</span> password<span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">subscribe</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n        <span class=\"token function-variable function\">next</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>router<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">'/dashboard'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n        <span class=\"token function-variable function\">error</span><span class=\"token operator\">:</span> <span class=\"token parameter\">err</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>error <span class=\"token operator\">=</span> err<span class=\"token punctuation\">.</span>error<span class=\"token operator\">?.</span>message <span class=\"token operator\">||</span> <span class=\"token string\">'Login failed'</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>loading <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// Add testApi method for API testing</span>\n<span class=\"token function\">testApi</span><span class=\"token punctuation\">(</span>endpoint<span class=\"token operator\">:</span> <span class=\"token string\">'hello'</span> <span class=\"token operator\">|</span> <span class=\"token string\">'protected'</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token keyword\">void</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Testing </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endpoint<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> API endpoint</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>apiResponse <span class=\"token operator\">=</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Clear previous response</span>\n    \n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>http<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">http://localhost:3000/api/</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endpoint<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">withCredentials</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token comment\">// Important for cookies</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">subscribe</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">next</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">response</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endpoint<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> API response:</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> response<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>apiResponse <span class=\"token operator\">=</span> response<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function-variable function\">error</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">error</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endpoint<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> API error:</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> error<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>apiResponse <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token literal-property property\">error</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span>\n        <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> error<span class=\"token punctuation\">.</span>error<span class=\"token operator\">?.</span>message <span class=\"token operator\">||</span> error<span class=\"token punctuation\">.</span>statusText <span class=\"token operator\">||</span> <span class=\"token string\">'Unknown error'</span><span class=\"token punctuation\">,</span>\n        <span class=\"token literal-property property\">status</span><span class=\"token operator\">:</span> error<span class=\"token punctuation\">.</span>status\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>File: <code class=\"language-text\">frontend/src/app/auth/login/login.component.html</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"32397522131567260000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<div class=&quot;login-container&quot;>\n<h2>Login</h2>\n\n<form [formGroup]=&quot;loginForm&quot; (ngSubmit)=&quot;onSubmit()&quot;>\n    <div class=&quot;form-group&quot;>\n    <label for=&quot;username&quot;>Email</label>\n    <input\n        type=&quot;email&quot;\n        id=&quot;username&quot;\n        formControlName=&quot;username&quot;\n        class=&quot;form-control&quot;\n        [ngClass]=&quot;{ 'is-invalid': loginForm.get('username')?.touched && loginForm.get('username')?.invalid }&quot;\n    />\n    <div *ngIf=&quot;loginForm.get('username')?.touched && loginForm.get('username')?.invalid&quot; class=&quot;invalid-feedback&quot;>\n        <div *ngIf=&quot;loginForm.get('username')?.errors?.['required']&quot;>Email is required</div>\n        <div *ngIf=&quot;loginForm.get('username')?.errors?.['email']&quot;>Please enter a valid email</div>\n    </div>\n    </div>\n    \n    <div class=&quot;form-group&quot;>\n    <label for=&quot;password&quot;>Password</label>\n    <input\n        type=&quot;password&quot;\n        id=&quot;password&quot;\n        formControlName=&quot;password&quot;\n        class=&quot;form-control&quot;\n        [ngClass]=&quot;{ 'is-invalid': loginForm.get('password')?.touched && loginForm.get('password')?.invalid }&quot;\n    />\n    <div *ngIf=&quot;loginForm.get('password')?.touched && loginForm.get('password')?.invalid&quot; class=&quot;invalid-feedback&quot;>\n        <div *ngIf=&quot;loginForm.get('password')?.errors?.['required']&quot;>Password is required</div>\n    </div>\n    </div>\n    \n    <div class=&quot;form-group&quot;>\n    <button [disabled]=&quot;loading || loginForm.invalid&quot; class=&quot;btn btn-primary&quot;>\n        {{ loading ? 'Logging in...' : 'Login' }}\n    </button>\n    </div>\n    \n    <div *ngIf=&quot;error&quot; class=&quot;alert alert-danger mt-3&quot;>\n    {{ error }}\n    </div>\n</form>\n\n<div class=&quot;api-test&quot;>\n    <h3>API Test</h3>\n    <p class=&quot;test-description&quot;>You can test the API endpoints before and after login to see how authentication works.</p>\n    <button (click)=&quot;testApi('hello')&quot; class=&quot;test-btn&quot;>Test Public API</button>\n    <button (click)=&quot;testApi('protected')&quot; class=&quot;test-btn&quot;>Test Protected API</button>\n    \n    <div *ngIf=&quot;apiResponse&quot; class=&quot;response-box&quot;>\n    <h4>API Response:</h4>\n    <pre>{{ apiResponse | json }}</pre>\n    </div>\n</div>\n</div>\n`, `32397522131567260000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token operator\">&lt;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"login-container\"</span><span class=\"token operator\">></span>\n<span class=\"token operator\">&lt;</span>h2<span class=\"token operator\">></span>Login<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h2<span class=\"token operator\">></span>\n\n<span class=\"token operator\">&lt;</span>form <span class=\"token punctuation\">[</span>formGroup<span class=\"token punctuation\">]</span><span class=\"token operator\">=</span><span class=\"token string\">\"loginForm\"</span> <span class=\"token punctuation\">(</span>ngSubmit<span class=\"token punctuation\">)</span><span class=\"token operator\">=</span><span class=\"token string\">\"onSubmit()\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"form-group\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>label <span class=\"token keyword\">for</span><span class=\"token operator\">=</span><span class=\"token string\">\"username\"</span><span class=\"token operator\">></span>Email<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>label<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>input\n        type<span class=\"token operator\">=</span><span class=\"token string\">\"email\"</span>\n        id<span class=\"token operator\">=</span><span class=\"token string\">\"username\"</span>\n        formControlName<span class=\"token operator\">=</span><span class=\"token string\">\"username\"</span>\n        <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"form-control\"</span>\n        <span class=\"token punctuation\">[</span>ngClass<span class=\"token punctuation\">]</span><span class=\"token operator\">=</span><span class=\"token string\">\"{ 'is-invalid': loginForm.get('username')?.touched &amp;&amp; loginForm.get('username')?.invalid }\"</span>\n    <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>div <span class=\"token operator\">*</span>ngIf<span class=\"token operator\">=</span><span class=\"token string\">\"loginForm.get('username')?.touched &amp;&amp; loginForm.get('username')?.invalid\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"invalid-feedback\"</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>div <span class=\"token operator\">*</span>ngIf<span class=\"token operator\">=</span><span class=\"token string\">\"loginForm.get('username')?.errors?.['required']\"</span><span class=\"token operator\">></span>Email is required<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>div <span class=\"token operator\">*</span>ngIf<span class=\"token operator\">=</span><span class=\"token string\">\"loginForm.get('username')?.errors?.['email']\"</span><span class=\"token operator\">></span>Please enter a valid email<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    \n    <span class=\"token operator\">&lt;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"form-group\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>label <span class=\"token keyword\">for</span><span class=\"token operator\">=</span><span class=\"token string\">\"password\"</span><span class=\"token operator\">></span>Password<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>label<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>input\n        type<span class=\"token operator\">=</span><span class=\"token string\">\"password\"</span>\n        id<span class=\"token operator\">=</span><span class=\"token string\">\"password\"</span>\n        formControlName<span class=\"token operator\">=</span><span class=\"token string\">\"password\"</span>\n        <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"form-control\"</span>\n        <span class=\"token punctuation\">[</span>ngClass<span class=\"token punctuation\">]</span><span class=\"token operator\">=</span><span class=\"token string\">\"{ 'is-invalid': loginForm.get('password')?.touched &amp;&amp; loginForm.get('password')?.invalid }\"</span>\n    <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>div <span class=\"token operator\">*</span>ngIf<span class=\"token operator\">=</span><span class=\"token string\">\"loginForm.get('password')?.touched &amp;&amp; loginForm.get('password')?.invalid\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"invalid-feedback\"</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>div <span class=\"token operator\">*</span>ngIf<span class=\"token operator\">=</span><span class=\"token string\">\"loginForm.get('password')?.errors?.['required']\"</span><span class=\"token operator\">></span>Password is required<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    \n    <span class=\"token operator\">&lt;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"form-group\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>button <span class=\"token punctuation\">[</span>disabled<span class=\"token punctuation\">]</span><span class=\"token operator\">=</span><span class=\"token string\">\"loading || loginForm.invalid\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"btn btn-primary\"</span><span class=\"token operator\">></span>\n        <span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> loading <span class=\"token operator\">?</span> <span class=\"token string\">'Logging in...'</span> <span class=\"token operator\">:</span> <span class=\"token string\">'Login'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    \n    <span class=\"token operator\">&lt;</span>div <span class=\"token operator\">*</span>ngIf<span class=\"token operator\">=</span><span class=\"token string\">\"error\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"alert alert-danger mt-3\"</span><span class=\"token operator\">></span>\n    <span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> error <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>form<span class=\"token operator\">></span>\n\n<span class=\"token operator\">&lt;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"api-test\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>h3<span class=\"token operator\">></span><span class=\"token constant\">API</span> Test<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h3<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>p <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"test-description\"</span><span class=\"token operator\">></span>You can test the <span class=\"token constant\">API</span> endpoints before and after login to see how authentication works<span class=\"token punctuation\">.</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>p<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token function\">button</span> <span class=\"token punctuation\">(</span>click<span class=\"token punctuation\">)</span><span class=\"token operator\">=</span><span class=\"token string\">\"testApi('hello')\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"test-btn\"</span><span class=\"token operator\">></span>Test Public <span class=\"token constant\">API</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token function\">button</span> <span class=\"token punctuation\">(</span>click<span class=\"token punctuation\">)</span><span class=\"token operator\">=</span><span class=\"token string\">\"testApi('protected')\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"test-btn\"</span><span class=\"token operator\">></span>Test Protected <span class=\"token constant\">API</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n    \n    <span class=\"token operator\">&lt;</span>div <span class=\"token operator\">*</span>ngIf<span class=\"token operator\">=</span><span class=\"token string\">\"apiResponse\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string\">\"response-box\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>h4<span class=\"token operator\">></span><span class=\"token constant\">API</span> <span class=\"token literal-property property\">Response</span><span class=\"token operator\">:</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h4<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>pre<span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> apiResponse <span class=\"token operator\">|</span> json <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>pre<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n</code></pre></div>\n<p>File: <code class=\"language-text\">frontend/src/app/auth/login/login.component.scss</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"77237375274194340000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`.login-container {\nmax-width: 400px;\nmargin: 50px auto;\npadding: 20px;\nbox-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\nborder-radius: 5px;\n\nh2 {\n    text-align: center;\n    margin-bottom: 20px;\n}\n\n.form-group {\n    margin-bottom: 15px;\n    \n    label {\n    display: block;\n    margin-bottom: 5px;\n    font-weight: 500;\n    }\n    \n    .form-control {\n    width: 100%;\n    padding: 8px;\n    border: 1px solid #ddd;\n    border-radius: 4px;\n    \n    &.is-invalid {\n        border-color: #dc3545;\n    }\n    }\n    \n    .invalid-feedback {\n    color: #dc3545;\n    font-size: 12px;\n    margin-top: 5px;\n    }\n    \n    .btn {\n    width: 100%;\n    padding: 10px;\n    background-color: #007bff;\n    color: white;\n    border: none;\n    border-radius: 4px;\n    cursor: pointer;\n    \n    &:disabled {\n        background-color: #7abaff;\n        cursor: not-allowed;\n    }\n    }\n}\n\n.alert {\n    padding: 10px;\n    border-radius: 4px;\n    \n    &.alert-danger {\n    background-color: #f8d7da;\n    color: #721c24;\n    border: 1px solid #f5c6cb;\n    }\n}\n\n.api-test {\n    margin-top: 30px;\n    padding-top: 20px;\n    border-top: 1px solid #eee;\n    \n    h3 {\n    margin-bottom: 10px;\n    font-size: 18px;\n    }\n    \n    .test-description {\n    font-size: 14px;\n    color: #666;\n    margin-bottom: 15px;\n    }\n    \n    .test-btn {\n    background-color: #007bff;\n    color: white;\n    border: none;\n    padding: 8px 12px;\n    margin-right: 10px;\n    margin-bottom: 10px;\n    border-radius: 4px;\n    cursor: pointer;\n    }\n    \n    .test-btn:hover {\n    background-color: #0069d9;\n    }\n    \n    .response-box {\n    margin-top: 15px;\n    padding: 15px;\n    background-color: #f1f1f1;\n    border-radius: 4px;\n    overflow: auto;\n    \n    h4 {\n        margin-top: 0;\n        margin-bottom: 10px;\n        font-size: 16px;\n    }\n    \n    pre {\n        margin: 0;\n        white-space: pre-wrap;\n        font-size: 13px;\n    }\n    }\n}\n}`, `77237375274194340000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token punctuation\">.</span>login<span class=\"token operator\">-</span>container <span class=\"token punctuation\">{</span>\nmax<span class=\"token operator\">-</span>width<span class=\"token operator\">:</span> 400px<span class=\"token punctuation\">;</span>\n<span class=\"token literal-property property\">margin</span><span class=\"token operator\">:</span> 50px auto<span class=\"token punctuation\">;</span>\n<span class=\"token literal-property property\">padding</span><span class=\"token operator\">:</span> 20px<span class=\"token punctuation\">;</span>\nbox<span class=\"token operator\">-</span>shadow<span class=\"token operator\">:</span> <span class=\"token number\">0</span> <span class=\"token number\">0</span> 10px <span class=\"token function\">rgba</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nborder<span class=\"token operator\">-</span>radius<span class=\"token operator\">:</span> 5px<span class=\"token punctuation\">;</span>\n\nh2 <span class=\"token punctuation\">{</span>\n    text<span class=\"token operator\">-</span>align<span class=\"token operator\">:</span> center<span class=\"token punctuation\">;</span>\n    margin<span class=\"token operator\">-</span>bottom<span class=\"token operator\">:</span> 20px<span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token punctuation\">.</span>form<span class=\"token operator\">-</span>group <span class=\"token punctuation\">{</span>\n    margin<span class=\"token operator\">-</span>bottom<span class=\"token operator\">:</span> 15px<span class=\"token punctuation\">;</span>\n    \n    label <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">display</span><span class=\"token operator\">:</span> block<span class=\"token punctuation\">;</span>\n    margin<span class=\"token operator\">-</span>bottom<span class=\"token operator\">:</span> 5px<span class=\"token punctuation\">;</span>\n    font<span class=\"token operator\">-</span>weight<span class=\"token operator\">:</span> <span class=\"token number\">500</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    \n    <span class=\"token punctuation\">.</span>form<span class=\"token operator\">-</span>control <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">width</span><span class=\"token operator\">:</span> <span class=\"token number\">100</span><span class=\"token operator\">%</span><span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">padding</span><span class=\"token operator\">:</span> 8px<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">border</span><span class=\"token operator\">:</span> 1px solid #ddd<span class=\"token punctuation\">;</span>\n    border<span class=\"token operator\">-</span>radius<span class=\"token operator\">:</span> 4px<span class=\"token punctuation\">;</span>\n    \n    <span class=\"token operator\">&amp;</span><span class=\"token punctuation\">.</span>is<span class=\"token operator\">-</span>invalid <span class=\"token punctuation\">{</span>\n        border<span class=\"token operator\">-</span>color<span class=\"token operator\">:</span> #dc3545<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n    \n    <span class=\"token punctuation\">.</span>invalid<span class=\"token operator\">-</span>feedback <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">color</span><span class=\"token operator\">:</span> #dc3545<span class=\"token punctuation\">;</span>\n    font<span class=\"token operator\">-</span>size<span class=\"token operator\">:</span> 12px<span class=\"token punctuation\">;</span>\n    margin<span class=\"token operator\">-</span>top<span class=\"token operator\">:</span> 5px<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    \n    <span class=\"token punctuation\">.</span>btn <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">width</span><span class=\"token operator\">:</span> <span class=\"token number\">100</span><span class=\"token operator\">%</span><span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">padding</span><span class=\"token operator\">:</span> 10px<span class=\"token punctuation\">;</span>\n    background<span class=\"token operator\">-</span>color<span class=\"token operator\">:</span> #007bff<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">color</span><span class=\"token operator\">:</span> white<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">border</span><span class=\"token operator\">:</span> none<span class=\"token punctuation\">;</span>\n    border<span class=\"token operator\">-</span>radius<span class=\"token operator\">:</span> 4px<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">cursor</span><span class=\"token operator\">:</span> pointer<span class=\"token punctuation\">;</span>\n    \n    <span class=\"token operator\">&amp;</span><span class=\"token operator\">:</span>disabled <span class=\"token punctuation\">{</span>\n        background<span class=\"token operator\">-</span>color<span class=\"token operator\">:</span> #7abaff<span class=\"token punctuation\">;</span>\n        <span class=\"token literal-property property\">cursor</span><span class=\"token operator\">:</span> not<span class=\"token operator\">-</span>allowed<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token punctuation\">.</span>alert <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">padding</span><span class=\"token operator\">:</span> 10px<span class=\"token punctuation\">;</span>\n    border<span class=\"token operator\">-</span>radius<span class=\"token operator\">:</span> 4px<span class=\"token punctuation\">;</span>\n    \n    <span class=\"token operator\">&amp;</span><span class=\"token punctuation\">.</span>alert<span class=\"token operator\">-</span>danger <span class=\"token punctuation\">{</span>\n    background<span class=\"token operator\">-</span>color<span class=\"token operator\">:</span> #f8d7da<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">color</span><span class=\"token operator\">:</span> #721c24<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">border</span><span class=\"token operator\">:</span> 1px solid #f5c6cb<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token punctuation\">.</span>api<span class=\"token operator\">-</span>test <span class=\"token punctuation\">{</span>\n    margin<span class=\"token operator\">-</span>top<span class=\"token operator\">:</span> 30px<span class=\"token punctuation\">;</span>\n    padding<span class=\"token operator\">-</span>top<span class=\"token operator\">:</span> 20px<span class=\"token punctuation\">;</span>\n    border<span class=\"token operator\">-</span>top<span class=\"token operator\">:</span> 1px solid #eee<span class=\"token punctuation\">;</span>\n    \n    h3 <span class=\"token punctuation\">{</span>\n    margin<span class=\"token operator\">-</span>bottom<span class=\"token operator\">:</span> 10px<span class=\"token punctuation\">;</span>\n    font<span class=\"token operator\">-</span>size<span class=\"token operator\">:</span> 18px<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    \n    <span class=\"token punctuation\">.</span>test<span class=\"token operator\">-</span>description <span class=\"token punctuation\">{</span>\n    font<span class=\"token operator\">-</span>size<span class=\"token operator\">:</span> 14px<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">color</span><span class=\"token operator\">:</span> #<span class=\"token number\">666</span><span class=\"token punctuation\">;</span>\n    margin<span class=\"token operator\">-</span>bottom<span class=\"token operator\">:</span> 15px<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    \n    <span class=\"token punctuation\">.</span>test<span class=\"token operator\">-</span>btn <span class=\"token punctuation\">{</span>\n    background<span class=\"token operator\">-</span>color<span class=\"token operator\">:</span> #007bff<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">color</span><span class=\"token operator\">:</span> white<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">border</span><span class=\"token operator\">:</span> none<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">padding</span><span class=\"token operator\">:</span> 8px 12px<span class=\"token punctuation\">;</span>\n    margin<span class=\"token operator\">-</span>right<span class=\"token operator\">:</span> 10px<span class=\"token punctuation\">;</span>\n    margin<span class=\"token operator\">-</span>bottom<span class=\"token operator\">:</span> 10px<span class=\"token punctuation\">;</span>\n    border<span class=\"token operator\">-</span>radius<span class=\"token operator\">:</span> 4px<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">cursor</span><span class=\"token operator\">:</span> pointer<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    \n    <span class=\"token punctuation\">.</span>test<span class=\"token operator\">-</span>btn<span class=\"token operator\">:</span>hover <span class=\"token punctuation\">{</span>\n    background<span class=\"token operator\">-</span>color<span class=\"token operator\">:</span> #0069d9<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    \n    <span class=\"token punctuation\">.</span>response<span class=\"token operator\">-</span>box <span class=\"token punctuation\">{</span>\n    margin<span class=\"token operator\">-</span>top<span class=\"token operator\">:</span> 15px<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">padding</span><span class=\"token operator\">:</span> 15px<span class=\"token punctuation\">;</span>\n    background<span class=\"token operator\">-</span>color<span class=\"token operator\">:</span> #f1f1f1<span class=\"token punctuation\">;</span>\n    border<span class=\"token operator\">-</span>radius<span class=\"token operator\">:</span> 4px<span class=\"token punctuation\">;</span>\n    <span class=\"token literal-property property\">overflow</span><span class=\"token operator\">:</span> auto<span class=\"token punctuation\">;</span>\n    \n    h4 <span class=\"token punctuation\">{</span>\n        margin<span class=\"token operator\">-</span>top<span class=\"token operator\">:</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n        margin<span class=\"token operator\">-</span>bottom<span class=\"token operator\">:</span> 10px<span class=\"token punctuation\">;</span>\n        font<span class=\"token operator\">-</span>size<span class=\"token operator\">:</span> 16px<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    \n    pre <span class=\"token punctuation\">{</span>\n        <span class=\"token literal-property property\">margin</span><span class=\"token operator\">:</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n        white<span class=\"token operator\">-</span>space<span class=\"token operator\">:</span> pre<span class=\"token operator\">-</span>wrap<span class=\"token punctuation\">;</span>\n        font<span class=\"token operator\">-</span>size<span class=\"token operator\">:</span> 13px<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n</li>\n<li>\n<h3 id=\"store-jwt-securely\" style=\"position:relative;\"><a href=\"#store-jwt-securely\" aria-label=\"store jwt securely permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Store JWT Securely</h3>\n<p>In our implementation, we’ve provided two options for storing JWTs, with HttpOnly cookies as the recommended approach and sessionStorage as a fallback:</p>\n<p><strong>Option 1:</strong> HttpOnly Cookies (Primary Recommended Approach)</p>\n<ul>\n<li>Cookies with the HttpOnly flag cannot be accessed by JavaScript</li>\n<li>Provides protection against XSS attacks</li>\n<li>Automatically sent with every request to the same domain</li>\n<li>Requires proper CORS configuration on the server</li>\n</ul>\n<p><strong>Option 2:</strong> sessionStorage (Fallback Option)</p>\n<ul>\n<li>Clears when the browser tab is closed (unlike localStorage)</li>\n<li>Still vulnerable to XSS attacks, but preferable to localStorage</li>\n<li>Must be manually attached to each request</li>\n</ul>\n<p>Our implementation supports both approaches simultaneously:</p>\n<ul>\n<li>The backend sets an HttpOnly cookie for optimal security</li>\n<li>The frontend also stores the token in sessionStorage as a fallback</li>\n<li>The JWT interceptor uses whichever method is available</li>\n</ul>\n<p><strong>Why SessionStorage over LocalStorage?</strong></p>\n<ul>\n<li>Security: sessionStorage is cleared when the browser tab is closed, reducing the window of vulnerability</li>\n<li>Session-based: Better aligns with the temporary nature of authentication tokens</li>\n<li>Isolation: Each tab gets its own sessionStorage, preventing session leakage between tabs</li>\n</ul>\n</li>\n<li>\n<h3 id=\"attach-jwt-to-requests\" style=\"position:relative;\"><a href=\"#attach-jwt-to-requests\" aria-label=\"attach jwt to requests permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Attach JWT to Requests</h3>\n<p>Let’s create an HTTP interceptor to automatically attach the JWT to all outgoing requests. Angular offers two ways to create interceptors: class-based and function-based.</p>\n<p>File: <code class=\"language-text\">frontend/src/app/interceptors/jwt.interceptor.ts</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"77571813104349170000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { HttpInterceptorFn } from '@angular/common/http';\nimport { inject } from '@angular/core';\nimport { AuthService } from '../auth/auth.service';\n\nexport const jwtInterceptor: HttpInterceptorFn = (request, next) => {\nconst authService = inject(AuthService);\n\n// Get the current user from the auth service\nconst currentUser = authService.currentUserValue;\n\n// If using sessionStorage and we have a token, add it to the Authorization header\nif (currentUser && currentUser.token) {\n    request = request.clone({\n    setHeaders: {\n        Authorization: \\`Bearer \\${currentUser.token}\\`\n    }\n    });\n}\n\n// If the endpoint requires credentials (for HttpOnly cookies), add withCredentials\nif (request.url.includes('/api/')) {\n    request = request.clone({\n    withCredentials: true\n    });\n}\n\nreturn next(request);\n};`, `77571813104349170000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> HttpInterceptorFn <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/common/http'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> inject <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/core'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> AuthService <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'../auth/auth.service'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> <span class=\"token literal-property property\">jwtInterceptor</span><span class=\"token operator\">:</span> <span class=\"token function-variable function\">HttpInterceptorFn</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">request<span class=\"token punctuation\">,</span> next</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n<span class=\"token keyword\">const</span> authService <span class=\"token operator\">=</span> <span class=\"token function\">inject</span><span class=\"token punctuation\">(</span>AuthService<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// Get the current user from the auth service</span>\n<span class=\"token keyword\">const</span> currentUser <span class=\"token operator\">=</span> authService<span class=\"token punctuation\">.</span>currentUserValue<span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// If using sessionStorage and we have a token, add it to the Authorization header</span>\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>currentUser <span class=\"token operator\">&amp;&amp;</span> currentUser<span class=\"token punctuation\">.</span>token<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    request <span class=\"token operator\">=</span> request<span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">setHeaders</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token literal-property property\">Authorization</span><span class=\"token operator\">:</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Bearer </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>currentUser<span class=\"token punctuation\">.</span>token<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// If the endpoint requires credentials (for HttpOnly cookies), add withCredentials</span>\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>request<span class=\"token punctuation\">.</span>url<span class=\"token punctuation\">.</span><span class=\"token function\">includes</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/api/'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    request <span class=\"token operator\">=</span> request<span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">withCredentials</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">return</span> <span class=\"token function\">next</span><span class=\"token punctuation\">(</span>request<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Register the function-based interceptor in <code class=\"language-text\">frontend/src/app/app.config.ts</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"78799614803054280000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { ApplicationConfig } from '@angular/core';\nimport { provideRouter } from '@angular/router';\nimport { provideHttpClient, withInterceptors } from '@angular/common/http';\n\nimport { routes } from './app.routes';\nimport { jwtInterceptor } from './interceptors/jwt.interceptor';\n\nexport const appConfig: ApplicationConfig = {\nproviders: [\n    provideRouter(routes),\n    provideHttpClient(withInterceptors([jwtInterceptor]))\n]\n};\n`, `78799614803054280000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> ApplicationConfig <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/core'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> provideRouter <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/router'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> provideHttpClient<span class=\"token punctuation\">,</span> withInterceptors <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/common/http'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> routes <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./app.routes'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> jwtInterceptor <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./interceptors/jwt.interceptor'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> <span class=\"token literal-property property\">appConfig</span><span class=\"token operator\">:</span> ApplicationConfig <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n<span class=\"token literal-property property\">providers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token function\">provideRouter</span><span class=\"token punctuation\">(</span>routes<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function\">provideHttpClient</span><span class=\"token punctuation\">(</span><span class=\"token function\">withInterceptors</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span>jwtInterceptor<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n</code></pre></div>\n</li>\n<li>\n<h3 id=\"protect-routes-w-guards\" style=\"position:relative;\"><a href=\"#protect-routes-w-guards\" aria-label=\"protect routes w guards permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Protect Routes w/ Guards</h3>\n<p>Create an authentication guard to protect routes.</p>\n<p>File: <code class=\"language-text\">frontend/src/app/guards/auth.guard.ts</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"10621037651012633000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { Routes } from '@angular/router';\nimport { LoginComponent } from './auth/login/login.component';\nimport { DashboardComponent } from './dashboard/dashboard.component';\nimport { AuthGuard } from './guards/auth.guard';\n\nexport const routes: Routes = [\n{ path: 'login', component: LoginComponent },\n{ \n    path: 'dashboard', \n    component: DashboardComponent,\n    canActivate: [AuthGuard] \n},\n{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },\n{ path: '**', redirectTo: '/dashboard' }\n];`, `10621037651012633000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Routes <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/router'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> LoginComponent <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./auth/login/login.component'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> DashboardComponent <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./dashboard/dashboard.component'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> AuthGuard <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./guards/auth.guard'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> <span class=\"token literal-property property\">routes</span><span class=\"token operator\">:</span> Routes <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>\n<span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">path</span><span class=\"token operator\">:</span> <span class=\"token string\">'login'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">component</span><span class=\"token operator\">:</span> LoginComponent <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">{</span> \n    <span class=\"token literal-property property\">path</span><span class=\"token operator\">:</span> <span class=\"token string\">'dashboard'</span><span class=\"token punctuation\">,</span> \n    <span class=\"token literal-property property\">component</span><span class=\"token operator\">:</span> DashboardComponent<span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">canActivate</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>AuthGuard<span class=\"token punctuation\">]</span> \n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">path</span><span class=\"token operator\">:</span> <span class=\"token string\">''</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">redirectTo</span><span class=\"token operator\">:</span> <span class=\"token string\">'/dashboard'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">pathMatch</span><span class=\"token operator\">:</span> <span class=\"token string\">'full'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">path</span><span class=\"token operator\">:</span> <span class=\"token string\">'**'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">redirectTo</span><span class=\"token operator\">:</span> <span class=\"token string\">'/dashboard'</span> <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n<li>\n<h3 id=\"logout-mechanism\" style=\"position:relative;\"><a href=\"#logout-mechanism\" aria-label=\"logout mechanism permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Logout Mechanism</h3>\n<p>We already implemented the logout method in our auth service. Let’s add a simple component to use it.</p>\n<p>File: <code class=\"language-text\">frontend/src/app/auth/logout/logout.component.ts</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"88719304725235370000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { Component } from '@angular/core';\nimport { Router } from '@angular/router';\nimport { AuthService } from '../auth.service';\n\n@Component({\nselector: 'app-logout',\ntemplate: \\`\n    <button class=&quot;logout-btn&quot; (click)=&quot;logout()&quot;>\n    Logout\n    </button>\n\\`,\nstyles: [\\`\n    .logout-btn {\n    background: none;\n    border: none;\n    color: #007bff;\n    cursor: pointer;\n    font-size: 16px;\n    padding: 5px 10px;\n    }\n    .logout-btn:hover {\n    text-decoration: underline;\n    }\n\\`]\n})\nexport class LogoutComponent {\n\nconstructor(\n    private authService: AuthService,\n    private router: Router\n) {}\n\nlogout(): void {\n    this.authService.logout().subscribe({\n    next: () => {\n        this.router.navigate(['/login']);\n    },\n    error: err => {\n        console.error('Logout error:', err);\n        // Even if the server request fails, we want to clear local state\n        sessionStorage.removeItem('currentUser');\n        this.router.navigate(['/login']);\n    }\n    });\n}\n}`, `88719304725235370000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Component <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/core'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Router <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/router'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> AuthService <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'../auth.service'</span><span class=\"token punctuation\">;</span>\n\n@<span class=\"token function\">Component</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n<span class=\"token literal-property property\">selector</span><span class=\"token operator\">:</span> <span class=\"token string\">'app-logout'</span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">template</span><span class=\"token operator\">:</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">\n    &lt;button class=\"logout-btn\" (click)=\"logout()\">\n    Logout\n    &lt;/button>\n</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">styles</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">\n    .logout-btn {\n    background: none;\n    border: none;\n    color: #007bff;\n    cursor: pointer;\n    font-size: 16px;\n    padding: 5px 10px;\n    }\n    .logout-btn:hover {\n    text-decoration: underline;\n    }\n</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">LogoutComponent</span> <span class=\"token punctuation\">{</span>\n\n<span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span>\n    <span class=\"token parameter\"><span class=\"token keyword\">private</span> <span class=\"token literal-property property\">authService</span><span class=\"token operator\">:</span> AuthService<span class=\"token punctuation\">,</span>\n    <span class=\"token keyword\">private</span> <span class=\"token literal-property property\">router</span><span class=\"token operator\">:</span> Router</span>\n<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">logout</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token keyword\">void</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>authService<span class=\"token punctuation\">.</span><span class=\"token function\">logout</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">subscribe</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">next</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>router<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">'/login'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function-variable function\">error</span><span class=\"token operator\">:</span> <span class=\"token parameter\">err</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Logout error:'</span><span class=\"token punctuation\">,</span> err<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token comment\">// Even if the server request fails, we want to clear local state</span>\n        sessionStorage<span class=\"token punctuation\">.</span><span class=\"token function\">removeItem</span><span class=\"token punctuation\">(</span><span class=\"token string\">'currentUser'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>router<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">'/login'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>File: <code class=\"language-text\">frontend/src/app/dashboard/dashboard.component.ts</code></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"45915669838126560000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { Component, OnInit } from '@angular/core';\nimport { CommonModule } from '@angular/common';\nimport { HttpClient } from '@angular/common/http';\nimport { Router } from '@angular/router';\nimport { AuthService } from '../auth/auth.service';\n\n@Component({\nselector: 'app-dashboard',\nstandalone: true,\nimports: [CommonModule],\ntemplate: \\`\n    <div class=&quot;dashboard&quot;>\n    <h2>Welcome to your Dashboard</h2>\n    <p>Hello, {{ currentUser?.username }}</p>\n    \n    <div class=&quot;auth-status&quot; *ngIf=&quot;currentUser&quot;>\n        <h3>Authentication Status</h3>\n        <p><strong>Status:</strong> Authenticated</p>\n        <p><strong>Auth Method:</strong> {{ hasToken ? 'JWT in sessionStorage' : 'HttpOnly Cookie' }}</p>\n        \n        <div class=&quot;api-test&quot;>\n        <h3>API Test</h3>\n        <button (click)=&quot;testApi('hello')&quot; class=&quot;test-btn&quot;>Test Public API</button>\n        <button (click)=&quot;testApi('protected')&quot; class=&quot;test-btn&quot;>Test Protected API</button>\n        \n        <div *ngIf=&quot;apiResponse&quot; class=&quot;response-box&quot;>\n            <h4>API Response:</h4>\n            <pre>{{ apiResponse | json }}</pre>\n        </div>\n        </div>\n    </div>\n    \n    <div class=&quot;actions&quot;>\n        <button class=&quot;logout-btn&quot; (click)=&quot;logout()&quot;>Logout</button>\n    </div>\n    </div>\n\\`,\nstyles: [\\`\n    .dashboard {\n    max-width: 800px;\n    margin: 50px auto;\n    padding: 20px;\n    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n    border-radius: 5px;\n    }\n    \n    .auth-status {\n    margin: 20px 0;\n    padding: 15px;\n    background-color: #f8f9fa;\n    border-radius: 5px;\n    }\n    \n    .api-test {\n    margin-top: 20px;\n    padding-top: 15px;\n    border-top: 1px solid #eee;\n    }\n    \n    .test-btn {\n    background-color: #007bff;\n    color: white;\n    border: none;\n    padding: 8px 12px;\n    margin-right: 10px;\n    border-radius: 4px;\n    cursor: pointer;\n    }\n    \n    .test-btn:hover {\n    background-color: #0069d9;\n    }\n    \n    .response-box {\n    margin-top: 15px;\n    padding: 15px;\n    background-color: #f1f1f1;\n    border-radius: 4px;\n    overflow: auto;\n    }\n    \n    pre {\n    margin: 0;\n    white-space: pre-wrap;\n    }\n    \n    .actions {\n    margin-top: 20px;\n    padding-top: 20px;\n    border-top: 1px solid #eee;\n    text-align: right;\n    }\n    \n    .logout-btn {\n    background: none;\n    border: none;\n    color: #007bff;\n    cursor: pointer;\n    font-size: 16px;\n    padding: 5px 10px;\n    }\n    \n    .logout-btn:hover {\n    text-decoration: underline;\n    }\n\\`]\n})\nexport class DashboardComponent implements OnInit {\ncurrentUser: any;\nhasToken: boolean = false;\napiResponse: any = null;\n\nconstructor(\n    private authService: AuthService,\n    private http: HttpClient,\n    private router: Router\n) {}\n\nngOnInit(): void {\n    this.authService.currentUser\\$.subscribe(user => {\n    console.log('Dashboard received user update:', user);\n    this.currentUser = user;\n    \n    // Check if we're using token-based auth (sessionStorage)\n    const storedUser = sessionStorage.getItem('currentUser');\n    this.hasToken = storedUser ? true : false;\n    });\n}\n\ntestApi(endpoint: 'hello' | 'protected'): void {\n    console.log(\\`Testing \\${endpoint} API endpoint\\`);\n    this.apiResponse = null; // Clear previous response\n    \n    this.http.get(\\`http://localhost:3000/api/\\${endpoint}\\`, {\n    withCredentials: true // Important for cookies\n    }).subscribe({\n    next: (response) => {\n        console.log(\\`\\${endpoint} API response:\\`, response);\n        this.apiResponse = response;\n    },\n    error: (error) => {\n        console.error(\\`\\${endpoint} API error:\\`, error);\n        this.apiResponse = {\n        error: true,\n        message: error.error?.message || error.statusText || 'Unknown error',\n        status: error.status\n        };\n    }\n    });\n}\n\nlogout(): void {\n    console.log('Logout button clicked');\n    \n    this.authService.logout().subscribe({\n    next: () => {\n        console.log('Logout successful, redirecting to login page');\n        this.router.navigate(['/login']);\n    },\n    error: err => {\n        console.error('Logout error:', err);\n        // Even if the server request fails, we want to clear local state\n        sessionStorage.removeItem('currentUser');\n        console.log('Session storage cleared, redirecting to login page');\n        this.router.navigate(['/login']);\n    }\n    });\n}\n}`, `45915669838126560000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Component<span class=\"token punctuation\">,</span> OnInit <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/core'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> CommonModule <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/common'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> HttpClient <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/common/http'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Router <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/router'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> AuthService <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'../auth/auth.service'</span><span class=\"token punctuation\">;</span>\n\n@<span class=\"token function\">Component</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n<span class=\"token literal-property property\">selector</span><span class=\"token operator\">:</span> <span class=\"token string\">'app-dashboard'</span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">standalone</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">imports</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>CommonModule<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">template</span><span class=\"token operator\">:</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">\n    &lt;div class=\"dashboard\">\n    &lt;h2>Welcome to your Dashboard&lt;/h2>\n    &lt;p>Hello, {{ currentUser?.username }}&lt;/p>\n    \n    &lt;div class=\"auth-status\" *ngIf=\"currentUser\">\n        &lt;h3>Authentication Status&lt;/h3>\n        &lt;p>&lt;strong>Status:&lt;/strong> Authenticated&lt;/p>\n        &lt;p>&lt;strong>Auth Method:&lt;/strong> {{ hasToken ? 'JWT in sessionStorage' : 'HttpOnly Cookie' }}&lt;/p>\n        \n        &lt;div class=\"api-test\">\n        &lt;h3>API Test&lt;/h3>\n        &lt;button (click)=\"testApi('hello')\" class=\"test-btn\">Test Public API&lt;/button>\n        &lt;button (click)=\"testApi('protected')\" class=\"test-btn\">Test Protected API&lt;/button>\n        \n        &lt;div *ngIf=\"apiResponse\" class=\"response-box\">\n            &lt;h4>API Response:&lt;/h4>\n            &lt;pre>{{ apiResponse | json }}&lt;/pre>\n        &lt;/div>\n        &lt;/div>\n    &lt;/div>\n    \n    &lt;div class=\"actions\">\n        &lt;button class=\"logout-btn\" (click)=\"logout()\">Logout&lt;/button>\n    &lt;/div>\n    &lt;/div>\n</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span>\n<span class=\"token literal-property property\">styles</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">\n    .dashboard {\n    max-width: 800px;\n    margin: 50px auto;\n    padding: 20px;\n    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n    border-radius: 5px;\n    }\n    \n    .auth-status {\n    margin: 20px 0;\n    padding: 15px;\n    background-color: #f8f9fa;\n    border-radius: 5px;\n    }\n    \n    .api-test {\n    margin-top: 20px;\n    padding-top: 15px;\n    border-top: 1px solid #eee;\n    }\n    \n    .test-btn {\n    background-color: #007bff;\n    color: white;\n    border: none;\n    padding: 8px 12px;\n    margin-right: 10px;\n    border-radius: 4px;\n    cursor: pointer;\n    }\n    \n    .test-btn:hover {\n    background-color: #0069d9;\n    }\n    \n    .response-box {\n    margin-top: 15px;\n    padding: 15px;\n    background-color: #f1f1f1;\n    border-radius: 4px;\n    overflow: auto;\n    }\n    \n    pre {\n    margin: 0;\n    white-space: pre-wrap;\n    }\n    \n    .actions {\n    margin-top: 20px;\n    padding-top: 20px;\n    border-top: 1px solid #eee;\n    text-align: right;\n    }\n    \n    .logout-btn {\n    background: none;\n    border: none;\n    color: #007bff;\n    cursor: pointer;\n    font-size: 16px;\n    padding: 5px 10px;\n    }\n    \n    .logout-btn:hover {\n    text-decoration: underline;\n    }\n</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">DashboardComponent</span> <span class=\"token keyword\">implements</span> <span class=\"token class-name\">OnInit</span> <span class=\"token punctuation\">{</span>\n<span class=\"token literal-property property\">currentUser</span><span class=\"token operator\">:</span> any<span class=\"token punctuation\">;</span>\n<span class=\"token literal-property property\">hasToken</span><span class=\"token operator\">:</span> boolean <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span>\n<span class=\"token literal-property property\">apiResponse</span><span class=\"token operator\">:</span> any <span class=\"token operator\">=</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span>\n    <span class=\"token parameter\"><span class=\"token keyword\">private</span> <span class=\"token literal-property property\">authService</span><span class=\"token operator\">:</span> AuthService<span class=\"token punctuation\">,</span>\n    <span class=\"token keyword\">private</span> <span class=\"token literal-property property\">http</span><span class=\"token operator\">:</span> HttpClient<span class=\"token punctuation\">,</span>\n    <span class=\"token keyword\">private</span> <span class=\"token literal-property property\">router</span><span class=\"token operator\">:</span> Router</span>\n<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">ngOnInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token keyword\">void</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>authService<span class=\"token punctuation\">.</span>currentUser$<span class=\"token punctuation\">.</span><span class=\"token function\">subscribe</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">user</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Dashboard received user update:'</span><span class=\"token punctuation\">,</span> user<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>currentUser <span class=\"token operator\">=</span> user<span class=\"token punctuation\">;</span>\n    \n    <span class=\"token comment\">// Check if we're using token-based auth (sessionStorage)</span>\n    <span class=\"token keyword\">const</span> storedUser <span class=\"token operator\">=</span> sessionStorage<span class=\"token punctuation\">.</span><span class=\"token function\">getItem</span><span class=\"token punctuation\">(</span><span class=\"token string\">'currentUser'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>hasToken <span class=\"token operator\">=</span> storedUser <span class=\"token operator\">?</span> <span class=\"token boolean\">true</span> <span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">testApi</span><span class=\"token punctuation\">(</span>endpoint<span class=\"token operator\">:</span> <span class=\"token string\">'hello'</span> <span class=\"token operator\">|</span> <span class=\"token string\">'protected'</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token keyword\">void</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Testing </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endpoint<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> API endpoint</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>apiResponse <span class=\"token operator\">=</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Clear previous response</span>\n    \n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>http<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">http://localhost:3000/api/</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endpoint<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">withCredentials</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token comment\">// Important for cookies</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">subscribe</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">next</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">response</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endpoint<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> API response:</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> response<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>apiResponse <span class=\"token operator\">=</span> response<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function-variable function\">error</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">error</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endpoint<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> API error:</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> error<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>apiResponse <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token literal-property property\">error</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span>\n        <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> error<span class=\"token punctuation\">.</span>error<span class=\"token operator\">?.</span>message <span class=\"token operator\">||</span> error<span class=\"token punctuation\">.</span>statusText <span class=\"token operator\">||</span> <span class=\"token string\">'Unknown error'</span><span class=\"token punctuation\">,</span>\n        <span class=\"token literal-property property\">status</span><span class=\"token operator\">:</span> error<span class=\"token punctuation\">.</span>status\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">logout</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token keyword\">void</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Logout button clicked'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    \n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>authService<span class=\"token punctuation\">.</span><span class=\"token function\">logout</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">subscribe</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">next</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Logout successful, redirecting to login page'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>router<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">'/login'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function-variable function\">error</span><span class=\"token operator\">:</span> <span class=\"token parameter\">err</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Logout error:'</span><span class=\"token punctuation\">,</span> err<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token comment\">// Even if the server request fails, we want to clear local state</span>\n        sessionStorage<span class=\"token punctuation\">.</span><span class=\"token function\">removeItem</span><span class=\"token punctuation\">(</span><span class=\"token string\">'currentUser'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Session storage cleared, redirecting to login page'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>router<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">'/login'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n</li>\n</ol>\n<h3 id=\"running-the-application\" style=\"position:relative;\"><a href=\"#running-the-application\" aria-label=\"running the application permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Running the Application</h3>\n<ul>\n<li>Start the backend server</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">cd jwt-auth-project/backend\nnode server.js</code></pre></div>\n<p>You should see output like:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Server running on http://localhost:3000\nTest credentials: miles@web.com / e-1610\nAvailable endpoints:\n  POST /api/login - Login endpoint\n  GET /api/protected - Protected endpoint (requires authentication)\n  POST /api/logout - Logout endpoint\n  GET /api/hello - Public test endpoint</code></pre></div>\n<p><strong>Note:</strong> In a different terminal start the frontend server</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">cd jwt-auth-project/frontend\nng serve</code></pre></div>\n<p>Navigate to <a href=\"http://localhost:4200\" target=\"_blank\" rel=\"nofollow\">http://localhost:4200</a> in your browser, notice the backend server will print out what’s happening. Attempt to access the protected api before and after getting a jwt.</p>\n<h2 id=\"how-supertokens-simplifies-angular-authentication\" style=\"position:relative;\"><a href=\"#how-supertokens-simplifies-angular-authentication\" aria-label=\"how supertokens simplifies angular authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How SuperTokens Simplifies Angular Authentication</h2>\n<p>After implementing JWT authentication from scratch, you’ve seen firsthand the complexities involved: token storage decisions, interceptor setup, session management, and this is before even considering long-term support. While building it yourself provides valuable learning, production applications often benefit from specialized authentication libraries. This is where SuperTokens shines:</p>\n<h2 id=\"1-passwordless-or-password-based-authentication\" style=\"position:relative;\"><a href=\"#1-passwordless-or-password-based-authentication\" aria-label=\"1 passwordless or password based authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Passwordless or Password-Based Authentication</h2>\n<p>SuperTokens supports multiple authentication methods out of the box:</p>\n<h3 id=\"email-password-authentication\" style=\"position:relative;\"><a href=\"#email-password-authentication\" aria-label=\"email password authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Email-Password Authentication</h3>\n<p>Traditional email and password authentication with built-in:</p>\n<ul>\n<li>Form validation</li>\n<li>Password strength requirements</li>\n<li>Secure password hashing</li>\n<li>Account verification flows</li>\n<li>Password reset functionality</li>\n</ul>\n<h3 id=\"passwordless-authentication\" style=\"position:relative;\"><a href=\"#passwordless-authentication\" aria-label=\"passwordless authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Passwordless Authentication</h3>\n<p>Modern authentication without passwords:</p>\n<ul>\n<li>Email One-Time Passwords (OTPs)</li>\n<li>Magic links sent via email</li>\n<li>Phone number verification with SMS codes</li>\n<li>Multi-factor authentication support</li>\n</ul>\n<p>The beauty of SuperTokens is that these authentication methods require minimal boilerplate. The library provides pre-built UI components that you can use as-is or customize to match your application’s design.</p>\n<h2 id=\"2-frontend-sdk-for-angular\" style=\"position:relative;\"><a href=\"#2-frontend-sdk-for-angular\" aria-label=\"2 frontend sdk for angular permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Frontend SDK for Angular</h2>\n<p>SuperTokens provides a dedicated Angular SDK that integrates smoothly with Angular’s architecture:</p>\n<h3 id=\"key-features\" style=\"position:relative;\"><a href=\"#key-features\" aria-label=\"key features permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Key Features</h3>\n<ul>\n<li>Pre-built UI Components: Ready-to-use login, signup, and password reset screens</li>\n<li>Session Management: Automatic handling of tokens and session state</li>\n<li>Redirection Handling: Smart redirects based on authentication status</li>\n<li>Route Protection: Angular route guards to protect private routes</li>\n<li>Typescript Support: Full type safety with TypeScript definitions</li>\n</ul>\n<h3 id=\"session-management\" style=\"position:relative;\"><a href=\"#session-management\" aria-label=\"session management permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Session Management</h3>\n<p>SuperTokens automatically handles complex session-related tasks:</p>\n<ul>\n<li>Token storage in HttpOnly cookies</li>\n<li>Token refreshing in the background</li>\n<li>Session expiry management</li>\n<li>Secure logout across all devices</li>\n<li>User session information access</li>\n</ul>\n<h3 id=\"route-protection\" style=\"position:relative;\"><a href=\"#route-protection\" aria-label=\"route protection permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Route Protection</h3>\n<p>With SuperTokens’ built-in route guards, protecting routes in your Angular application becomes straightforward. The guards integrate with Angular’s router to check authentication status before allowing access to protected routes.</p>\n<h2 id=\"3-security-benefits\" style=\"position:relative;\"><a href=\"#3-security-benefits\" aria-label=\"3 security benefits permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Security Benefits</h2>\n<p>SuperTokens provides several security advantages over a custom implementation:</p>\n<h3 id=\"httponly-cookies\" style=\"position:relative;\"><a href=\"#httponly-cookies\" aria-label=\"httponly cookies permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>HttpOnly Cookies</h3>\n<p>SuperTokens uses HttpOnly cookies by default, protecting tokens from XSS attacks by making them inaccessible to JavaScript running in the browser.</p>\n<h3 id=\"anti-csrf-protection\" style=\"position:relative;\"><a href=\"#anti-csrf-protection\" aria-label=\"anti csrf protection permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Anti-CSRF Protection</h3>\n<p>Built-in Cross-Site Request Forgery protection is enabled automatically when using cookies, preventing attackers from tricking users into making unintended requests.</p>\n<h3 id=\"automatic-token-refreshing\" style=\"position:relative;\"><a href=\"#automatic-token-refreshing\" aria-label=\"automatic token refreshing permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Automatic Token Refreshing</h3>\n<p>Sessions are refreshed in the background without disrupting the user experience, ensuring a smooth user experience while maintaining security.</p>\n<h3 id=\"rotating-refresh-tokens\" style=\"position:relative;\"><a href=\"#rotating-refresh-tokens\" aria-label=\"rotating refresh tokens permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Rotating Refresh Tokens</h3>\n<p>SuperTokens implements rotating refresh tokens, enhancing security by making stolen tokens invalid after use. This approach significantly reduces the risk of session hijacking.</p>\n<h3 id=\"revocation\" style=\"position:relative;\"><a href=\"#revocation\" aria-label=\"revocation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Revocation</h3>\n<p>SuperTokens supports immediate session revocation, allowing you to log out users from all devices in case of a security breach or suspicious activity.</p>\n<h2 id=\"4-integration-process-overview\" style=\"position:relative;\"><a href=\"#4-integration-process-overview\" aria-label=\"4 integration process overview permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. Integration Process Overview</h2>\n<p>Integrating SuperTokens into an Angular application involves a few high-level steps:</p>\n<h3 id=\"backend-setup\" style=\"position:relative;\"><a href=\"#backend-setup\" aria-label=\"backend setup permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Backend Setup</h3>\n<ul>\n<li>Install the SuperTokens server SDK</li>\n<li>Configure authentication methods and session management</li>\n<li>Set up CORS to allow communication with your Angular frontend</li>\n<li>Define protected API endpoints</li>\n</ul>\n<h3 id=\"frontend-setup\" style=\"position:relative;\"><a href=\"#frontend-setup\" aria-label=\"frontend setup permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Frontend Setup</h3>\n<ul>\n<li>Install the SuperTokens Angular SDK</li>\n<li>Configure authentication UI and recipes</li>\n<li>Set up routing to include authentication pages</li>\n<li>Implement route guards for protected routes</li>\n<li>Add login/logout functionality to your application</li>\n</ul>\n<p>The entire process typically takes hours rather than days, with most of the security complexities handled automatically by SuperTokens.</p>\n<h2 id=\"5-supertokens-documentation\" style=\"position:relative;\"><a href=\"#5-supertokens-documentation\" aria-label=\"5 supertokens documentation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5. SuperTokens Documentation</h2>\n<p>More information can be found at the SuperTokens documentation:</p>\n<p><a href=\"https://supertokens.com/docs/quickstart/frontend-setup\" target=\"_blank\" rel=\"nofollow\">SuperTokens Frontend Angular setup</a></p>\n<p><a href=\"https://supertokens.com/docs/authentication/overview\" target=\"_blank\" rel=\"nofollow\">SuperTokens Authentication Overview</a></p>\n<p><a href=\"https://supertokens.com/docs/migration/overview\" target=\"_blank\" rel=\"nofollow\">SuperTokens Migration Overview</a></p>\n<h2 id=\"common-angular-authentication-pitfalls-and-fixes\" style=\"position:relative;\"><a href=\"#common-angular-authentication-pitfalls-and-fixes\" aria-label=\"common angular authentication pitfalls and fixes permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Common Angular Authentication Pitfalls and Fixes</h2>\n<p>Even experienced developers encounter challenges when implementing authentication in Angular applications. Here are the most common pitfalls and practical solutions.</p>\n<h3 id=\"1-token-expiry-and-refresh-handling\" style=\"position:relative;\"><a href=\"#1-token-expiry-and-refresh-handling\" aria-label=\"1 token expiry and refresh handling permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Token Expiry and Refresh Handling</h3>\n<h3 id=\"the-problem\" style=\"position:relative;\"><a href=\"#the-problem\" aria-label=\"the problem permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Problem</h3>\n<p>JWTs typically expire after 15-60 minutes. When this happens during an active session, users suddenly receive 401 errors and get logged out, creating a poor experience.</p>\n<h3 id=\"the-solution\" style=\"position:relative;\"><a href=\"#the-solution\" aria-label=\"the solution permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Solution</h3>\n<p>Implement a silent refresh strategy:</p>\n<ul>\n<li>Proactive refresh: Set up a timer to refresh tokens at 75-80% of their lifetime</li>\n<li>Reactive interceptor: Catch 401 errors and refresh tokens automatically, then retry the original request</li>\n<li>Request queueing: Queue requests during refresh to prevent race conditions</li>\n<li>Fallback flow: Redirect to login only when refresh attempts fail</li>\n</ul>\n<p>This approach maintains seamless user sessions while preserving security benefits of short-lived tokens.</p>\n<h3 id=\"2-improper-token-storage\" style=\"position:relative;\"><a href=\"#2-improper-token-storage\" aria-label=\"2 improper token storage permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Improper Token Storage</h3>\n<h3 id=\"the-problem-1\" style=\"position:relative;\"><a href=\"#the-problem-1\" aria-label=\"the problem 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Problem</h3>\n<p>Many tutorials suggest storing JWTs in localStorage for simplicity, but this exposes tokens to XSS attacks since any JavaScript on your site can access localStorage.</p>\n<h3 id=\"the-solution-1\" style=\"position:relative;\"><a href=\"#the-solution-1\" aria-label=\"the solution 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Solution</h3>\n<p>Use more secure storage approaches:</p>\n<ul>\n<li>HttpOnly cookies: Server-set cookies that JavaScript cannot access</li>\n<li>Memory storage: Keep tokens in JavaScript variables (not persistent, but secure during the session)</li>\n<li>Refresh token pattern: Store short-lived access token in memory and longer-lived refresh token in an HttpOnly cookie</li>\n</ul>\n<p>When using cookies, ensure proper security flags are set (HttpOnly, Secure, SameSite=Strict).</p>\n<h3 id=\"3-missing-csrf-protection\" style=\"position:relative;\"><a href=\"#3-missing-csrf-protection\" aria-label=\"3 missing csrf protection permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Missing CSRF Protection</h3>\n<h3 id=\"the-problem-2\" style=\"position:relative;\"><a href=\"#the-problem-2\" aria-label=\"the problem 2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Problem</h3>\n<p>Cookie-based authentication is vulnerable to Cross-Site Request Forgery (CSRF) attacks where malicious sites trick users into making unwanted actions.</p>\n<h3 id=\"the-solution-2\" style=\"position:relative;\"><a href=\"#the-solution-2\" aria-label=\"the solution 2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Solution</h3>\n<p>Add appropriate CSRF protection:</p>\n<ul>\n<li>Modern approach: Use SameSite=Strict or SameSite=Lax cookie attribute.</li>\n<li>Traditional approach: Implement the Double-Submit Cookie pattern.</li>\n<li>Authorization header: If using token-based auth with Authorization headers (not cookies), CSRF protection isn’t necessary.</li>\n</ul>\n<p>Choose the right protection based on your authentication method: cookie-based auth needs CSRF protection, token-based auth with Authorization headers typically doesn’t.</p>\n<h3 id=\"4-race-conditions-on-initialization\" style=\"position:relative;\"><a href=\"#4-race-conditions-on-initialization\" aria-label=\"4 race conditions on initialization permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. Race Conditions on Initialization</h3>\n<h3 id=\"the-problem-3\" style=\"position:relative;\"><a href=\"#the-problem-3\" aria-label=\"the problem 3 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Problem</h3>\n<p>Angular applications often face timing issues during initialization where protected routes may render before authentication state is determined.</p>\n<h3 id=\"the-solution-3\" style=\"position:relative;\"><a href=\"#the-solution-3\" aria-label=\"the solution 3 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Solution</h3>\n<p>Ensure auth state is resolved before navigation:</p>\n<ul>\n<li>Route guards: Create guards that return observables and only complete when auth state is determined</li>\n<li>APP_INITIALIZER: Resolve authentication status before the application fully loads</li>\n<li>Loading states: Implement proper loading indicators during authentication checks</li>\n<li>Central auth service: Create a single source of truth for authentication state</li>\n</ul>\n<p>This prevents flickering UI, unauthorized access, and redirect loops in your application.</p>\n<h2 id=\"best-practices-for-secure-angular-authentication\" style=\"position:relative;\"><a href=\"#best-practices-for-secure-angular-authentication\" aria-label=\"best practices for secure angular authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Best Practices for Secure Angular Authentication</h2>\n<p>Implementing a secure authentication system in Angular requires attention to several key areas. Here are five essential practices to ensure your application remains secure while providing a smooth user experience.</p>\n<h3 id=\"1-use-secure-storage-mechanisms\" style=\"position:relative;\"><a href=\"#1-use-secure-storage-mechanisms\" aria-label=\"1 use secure storage mechanisms permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Use Secure Storage Mechanisms</h3>\n<p>How you store authentication tokens directly impacts your application’s security.</p>\n<h3 id=\"best-practices\" style=\"position:relative;\"><a href=\"#best-practices\" aria-label=\"best practices permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Best Practices:</h3>\n<ul>\n<li><strong>Prefer HttpOnly cookies</strong> for token storage\n<ul>\n<li>Not accessible via JavaScript, protecting against XSS attacks</li>\n<li>Set with <code class=\"language-text\">Secure</code> and <code class=\"language-text\">SameSite=Strict</code> flags for additional protection</li>\n</ul>\n</li>\n<li><strong>Avoid localStorage and sessionStorage</strong> for sensitive tokens\n<ul>\n<li>Vulnerable to cross-site scripting (XSS) attacks</li>\n<li>Any JavaScript on your page can access these storage options</li>\n</ul>\n</li>\n<li><strong>Consider in-memory storage</strong> as a compromise\n<ul>\n<li>Store access tokens in Angular services</li>\n<li>Use with refresh tokens in HttpOnly cookies for persistence</li>\n</ul>\n</li>\n</ul>\n<p>Tokens stored in HttpOnly cookies provide significant security advantages over browser storage options, especially for production applications.</p>\n<h3 id=\"2-protect-all-routes\" style=\"position:relative;\"><a href=\"#2-protect-all-routes\" aria-label=\"2 protect all routes permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Protect All Routes</h3>\n<p>Ensure users can only access the content they’re authorized to see.</p>\n<h3 id=\"best-practices-1\" style=\"position:relative;\"><a href=\"#best-practices-1\" aria-label=\"best practices 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Best Practices:</h3>\n<ul>\n<li><strong>Implement Angular route guards</strong>\n<ul>\n<li>Apply <code class=\"language-text\">CanActivate</code> guards to all protected routes</li>\n<li>Chain multiple guards for complex authorization rules</li>\n</ul>\n</li>\n<li><strong>Create role-based protection</strong>\n<ul>\n<li>Check user roles and permissions in guards</li>\n<li>Return to requested URL after successful authentication</li>\n</ul>\n</li>\n<li><strong>Apply protection consistently</strong>\n<ul>\n<li>Don’t rely only on hiding UI elements</li>\n<li>Always complement client-side guards with server-side authorization</li>\n</ul>\n</li>\n</ul>\n<p>Remember that route guards are a convenience feature, not a security boundary. Always validate authorization on your server as well.</p>\n<h3 id=\"3-token-revocation-support\" style=\"position:relative;\"><a href=\"#3-token-revocation-support\" aria-label=\"3 token revocation support permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Token Revocation Support</h3>\n<p>The ability to invalidate active sessions is crucial for maintaining security.</p>\n<h3 id=\"best-practices-2\" style=\"position:relative;\"><a href=\"#best-practices-2\" aria-label=\"best practices 2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Best Practices:</h3>\n<ul>\n<li><strong>Implement complete logout functionality</strong>\n<ul>\n<li>Clear tokens from client storage and memory</li>\n<li>Send revocation request to the server</li>\n</ul>\n</li>\n<li><strong>Enable server-side session invalidation</strong>\n<ul>\n<li>Maintain a blacklist of revoked tokens or session IDs</li>\n<li>Or use short-lived tokens with refresh token rotation</li>\n</ul>\n</li>\n<li><strong>Support multi-device logout</strong>\n<ul>\n<li>Allow users to terminate sessions on other devices</li>\n<li>Useful for “forgot password” scenarios</li>\n</ul>\n</li>\n</ul>\n<p>Proper revocation ensures that compromised credentials can be quickly invalidated, minimizing the damage from security breaches.</p>\n<h3 id=\"4-handle-refresh-tokens-gracefully\" style=\"position:relative;\"><a href=\"#4-handle-refresh-tokens-gracefully\" aria-label=\"4 handle refresh tokens gracefully permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. Handle Refresh Tokens Gracefully</h3>\n<p>A well-implemented token refresh mechanism balances security with user experience.</p>\n<h3 id=\"best-practices-3\" style=\"position:relative;\"><a href=\"#best-practices-3\" aria-label=\"best practices 3 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Best Practices:</h3>\n<ul>\n<li><strong>Implement automatic token refresh</strong>\n<ul>\n<li>Use HTTP interceptors to handle 401 responses</li>\n<li>Queue pending requests during refresh</li>\n</ul>\n</li>\n<li><strong>Use token rotation for refresh tokens</strong>\n<ul>\n<li>Issue new refresh tokens with each use</li>\n<li>Invalidate old refresh tokens after use</li>\n</ul>\n</li>\n<li><strong>Handle refresh failures appropriately</strong>\n<ul>\n<li>Redirect to login when refresh fails</li>\n<li>Provide clear feedback to users</li>\n</ul>\n</li>\n</ul>\n<p>Libraries like SuperTokens handle this complexity automatically. If building from scratch, carefully implement refresh logic to avoid race conditions.</p>\n<h3 id=\"5-enable-cors-and-csrf-safeguards\" style=\"position:relative;\"><a href=\"#5-enable-cors-and-csrf-safeguards\" aria-label=\"5 enable cors and csrf safeguards permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5. Enable CORS and CSRF Safeguards</h3>\n<p>Proper cross-origin configuration is essential for secure authentication.</p>\n<h3 id=\"best-practices-4\" style=\"position:relative;\"><a href=\"#best-practices-4\" aria-label=\"best practices 4 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Best Practices:</h3>\n<ul>\n<li><strong>Configure CORS correctly</strong>\n<ul>\n<li>Specify exact allowed origins (avoid wildcards)</li>\n<li>Enable credentials for authentication cookies</li>\n</ul>\n</li>\n<li><strong>Implement CSRF protection</strong> for cookie-based auth\n<ul>\n<li>Use <code class=\"language-text\">SameSite=Strict</code> cookie setting</li>\n<li>Apply Double-Submit Cookie pattern for older browsers</li>\n</ul>\n</li>\n<li><strong>Add security headers</strong>\n<ul>\n<li>Content-Security-Policy</li>\n<li>X-XSS-Protection</li>\n<li>X-Frame-Options</li>\n</ul>\n</li>\n</ul>\n<p>CORS misconfiguration is a common source of security vulnerabilities in modern web applications. Test your configuration thoroughly with cross-domain requests.</p>\n<h2 id=\"angular-authentication-with-oauth-providers\" style=\"position:relative;\"><a href=\"#angular-authentication-with-oauth-providers\" aria-label=\"angular authentication with oauth providers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Angular Authentication with OAuth Providers</h2>\n<p>Modern applications often support authentication through popular OAuth providers like Google, GitHub, and Microsoft. This approach allows users to log in with existing accounts while delegating security to established providers.</p>\n<h3 id=\"1-google-github-microsoft-integration\" style=\"position:relative;\"><a href=\"#1-google-github-microsoft-integration\" aria-label=\"1 google github microsoft integration permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Google, GitHub, Microsoft Integration</h3>\n<p>Angular applications can integrate with OAuth providers using specialized libraries.</p>\n<h3 id=\"using-angular-oauth2-oidc\" style=\"position:relative;\"><a href=\"#using-angular-oauth2-oidc\" aria-label=\"using angular oauth2 oidc permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using angular-oauth2-oidc</h3>\n<p>The <a href=\"https://github.com/manfredsteyer/angular-oauth2-oidc\" target=\"_blank\" rel=\"nofollow\">angular-oauth2-oidc</a> library is a popular choice for implementing OAuth in Angular:</p>\n<p><strong>Key Features:</strong></p>\n<ul>\n<li>Support for multiple authentication flows</li>\n<li>Token validation and refresh capabilities</li>\n<li>TypeScript interfaces for type safety</li>\n<li>Compatible with Angular’s dependency injection</li>\n</ul>\n<p><strong>Basic Setup Process:</strong></p>\n<ol>\n<li>Install the library: <code class=\"language-text\">npm install angular-oauth2-oidc</code></li>\n<li>Configure with your OAuth provider details</li>\n<li>Initialize the authentication flow</li>\n<li>Handle callbacks and token storage</li>\n</ol>\n<h3 id=\"2-oauth-authentication-flow-explained\" style=\"position:relative;\"><a href=\"#2-oauth-authentication-flow-explained\" aria-label=\"2 oauth authentication flow explained permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. OAuth Authentication Flow Explained</h3>\n<p>The standard OAuth flow follows these steps:</p>\n<h3 id=\"redirect-to-provider\" style=\"position:relative;\"><a href=\"#redirect-to-provider\" aria-label=\"redirect to provider permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Redirect to Provider</h3>\n<p>When a user clicks “Login with Google,” your app redirects to the provider’s authentication page.</p>\n<h3 id=\"user-consent\" style=\"position:relative;\"><a href=\"#user-consent\" aria-label=\"user consent permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>User Consent</h3>\n<p>The user authenticates with the provider and grants permission for your app to access their information.</p>\n<h3 id=\"authorization-code-exchange\" style=\"position:relative;\"><a href=\"#authorization-code-exchange\" aria-label=\"authorization code exchange permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Authorization Code Exchange</h3>\n<p>The provider redirects back to your application with an authorization code that your app exchanges for tokens.</p>\n<h3 id=\"receive-and-store-tokens\" style=\"position:relative;\"><a href=\"#receive-and-store-tokens\" aria-label=\"receive and store tokens permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Receive and Store Tokens</h3>\n<p>The provider returns several tokens:</p>\n<ul>\n<li><strong>Access Token</strong>: For accessing provider APIs</li>\n<li><strong>ID Token</strong>: Contains user information (JWT format)</li>\n<li><strong>Refresh Token</strong>: For obtaining new access tokens</li>\n</ul>\n<h3 id=\"use-tokens-for-authentication\" style=\"position:relative;\"><a href=\"#use-tokens-for-authentication\" aria-label=\"use tokens for authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use Tokens for Authentication</h3>\n<p>Your application extracts user information from the tokens and establishes a session.</p>\n<p>This flow happens largely behind the scenes when using libraries like angular-oauth2-oidc, which handle the complexity for you.</p>\n<h3 id=\"3-security-implications\" style=\"position:relative;\"><a href=\"#3-security-implications\" aria-label=\"3 security implications permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Security Implications</h3>\n<p>While OAuth delegates much of the authentication security to providers, there are still important considerations:</p>\n<h3 id=\"token-validation\" style=\"position:relative;\"><a href=\"#token-validation\" aria-label=\"token validation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Token Validation</h3>\n<p>Always validate received tokens:</p>\n<ul>\n<li><strong>Verify signatures</strong> to ensure tokens haven’t been tampered with</li>\n<li><strong>Check issuer</strong> to confirm the token came from the expected provider</li>\n<li><strong>Validate audience</strong> to ensure the token was issued for your application</li>\n<li><strong>Verify expiration</strong> to reject outdated tokens</li>\n</ul>\n<h3 id=\"csrf-protection\" style=\"position:relative;\"><a href=\"#csrf-protection\" aria-label=\"csrf protection permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>CSRF Protection</h3>\n<p>Implement Cross-Site Request Forgery protection:</p>\n<ul>\n<li>Use the <code class=\"language-text\">state</code> parameter to verify authentication requests</li>\n<li>Implement PKCE (Proof Key for Code Exchange) for authorization code flows</li>\n</ul>\n<h3 id=\"secure-token-storage\" style=\"position:relative;\"><a href=\"#secure-token-storage\" aria-label=\"secure token storage permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Secure Token Storage</h3>\n<p>Follow the same secure storage principles discussed earlier:</p>\n<ul>\n<li>Keep access tokens in memory when possible</li>\n<li>Use secure, HttpOnly cookies for refresh tokens</li>\n<li>Clear all tokens on logout</li>\n</ul>\n<p>Most OAuth libraries handle these security measures automatically, but understanding them helps you configure the libraries correctly.</p>\n<h3 id=\"4-supertokens-integration-with-social-providers\" style=\"position:relative;\"><a href=\"#4-supertokens-integration-with-social-providers\" aria-label=\"4 supertokens integration with social providers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. SuperTokens Integration with Social Providers</h3>\n<p>If you’re using SuperTokens, integrating OAuth providers becomes even simpler.</p>\n<h3 id=\"available-social-providers\" style=\"position:relative;\"><a href=\"#available-social-providers\" aria-label=\"available social providers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Available Social Providers</h3>\n<p>SuperTokens offers pre-built integration with:</p>\n<ul>\n<li>Google</li>\n<li>GitHub</li>\n<li>Apple</li>\n<li>Facebook</li>\n<li>Discord</li>\n<li>And many others</li>\n</ul>\n<h3 id=\"implementation-benefits\" style=\"position:relative;\"><a href=\"#implementation-benefits\" aria-label=\"implementation benefits permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Implementation Benefits</h3>\n<ol>\n<li><strong>Simplified Setup</strong>: Configure provider details once</li>\n<li><strong>Consistent API</strong>: Same interface across different providers</li>\n<li><strong>Pre-built UI Components</strong>: Ready-to-use login buttons and flows</li>\n<li><strong>Session Management</strong>: Automatic handling of tokens and sessions</li>\n</ol>\n<h3 id=\"custom-ui-options\" style=\"position:relative;\"><a href=\"#custom-ui-options\" aria-label=\"custom ui options permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Custom UI Options</h3>\n<p>You can still create your own UI while leveraging SuperTokens’ backend authentication flow, giving you flexibility without sacrificing security.</p>\n<h3 id=\"when-to-use-oauth-providers\" style=\"position:relative;\"><a href=\"#when-to-use-oauth-providers\" aria-label=\"when to use oauth providers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>When to Use OAuth Providers</h3>\n<p>OAuth providers are particularly valuable when:</p>\n<ul>\n<li>You want to reduce friction in the sign-up process</li>\n<li>Your users already have accounts with major providers</li>\n<li>You prefer not to manage password security yourself</li>\n<li>You need to access provider APIs with user permission</li>\n</ul>\n<p>By implementing OAuth authentication with providers like Google, GitHub, or Microsoft, you create a more user-friendly login experience while leveraging the robust security infrastructure of established identity providers.</p>\n<h2 id=\"key-takeaways\" style=\"position:relative;\"><a href=\"#key-takeaways\" aria-label=\"key takeaways permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Key Takeaways</h2>\n<h3 id=\"angular-doesnt-handle-auth-out-of-the-box\" style=\"position:relative;\"><a href=\"#angular-doesnt-handle-auth-out-of-the-box\" aria-label=\"angular doesnt handle auth out of the box permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Angular Doesn’t Handle Auth Out of the Box</h3>\n<p>Unlike some frameworks that include built-in authentication, Angular leaves this critical functionality up to you:</p>\n<ul>\n<li>No default authentication or security mechanisms are provided</li>\n<li>Developers must implement authentication from scratch or use external libraries</li>\n<li>This approach provides flexibility but requires careful attention to security details</li>\n<li>Understanding authentication concepts is essential for proper implementation</li>\n</ul>\n<p>The responsibility for secure authentication falls entirely on developers, making external libraries and best practices crucial for production applications.</p>\n<h3 id=\"jwt-authentication-must-be-handled-securely\" style=\"position:relative;\"><a href=\"#jwt-authentication-must-be-handled-securely\" aria-label=\"jwt authentication must be handled securely permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>JWT Authentication Must Be Handled Securely</h3>\n<p>JSON Web Tokens (JWT) provide a powerful mechanism for authentication in Angular applications, but security depends on proper implementation:</p>\n<ul>\n<li>Token storage decisions significantly impact security (HttpOnly cookies recommended)</li>\n<li>Refresh token mechanisms require careful implementation to avoid vulnerabilities</li>\n<li>Proper route protection goes beyond Angular’s route guards</li>\n<li>CSRF protection is necessary when using cookie-based approaches</li>\n</ul>\n<p>Our detailed implementation demonstrated these concerns while showing how to build a functional JWT authentication system from the ground up.</p>\n<h3 id=\"supertokens-provides-a-production-ready-solution\" style=\"position:relative;\"><a href=\"#supertokens-provides-a-production-ready-solution\" aria-label=\"supertokens provides a production ready solution permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>SuperTokens Provides a Production-Ready Solution</h3>\n<p>While building authentication from scratch is educational, production applications benefit from battle-tested solutions:</p>\n<ul>\n<li>SuperTokens offers a complete, secure authentication layer for Angular applications</li>\n<li>Multiple authentication methods (password-based, passwordless, social) are available out-of-the-box</li>\n<li>Complex security requirements are handled automatically</li>\n<li>Integration with existing systems and OAuth providers is streamlined</li>\n</ul>\n<p>By using SuperTokens or similar authentication libraries, you gain robust security with significantly less development effort and risk.</p>\n<h3 id=\"final-thoughts\" style=\"position:relative;\"><a href=\"#final-thoughts\" aria-label=\"final thoughts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Final Thoughts</h3>\n<p>Authentication in Angular applications requires deliberate design and implementation. Whether you build from scratch or leverage existing solutions, understanding the security implications of your choices is essential.\nThe approaches covered in this from manual JWT implementation to SuperTokens integration provide a foundation for creating secure, user-friendly authentication systems in your Angular applications.</p>","frontmatter":{"date":"May 18, 2025","title":"Angular Authentication","cover":"angular-authentication.png","author":"Maurice Saldivar","description":"Learn how Angular authentication works, key methods to secure apps, and how to integrate with providers like SuperTokens or OAuth."},"fields":{"slug":"/angular-authentication/"}},"site":{"siteMetadata":{"title":"SuperTokens Blog"}}},"pageContext":{"id":"39117a2c-ea43-58ca-8d04-0e78b19de575","fields__slug":"/angular-authentication/","__params":{"fields__slug":"angular-authentication"}}},
    "staticQueryHashes": []}