Backend-for-Frontend Auth: The Secure JS App Architecture

Bookmark
Rate this content

Modern apps face significant security threats, especially for browser-based JavaScript apps. This short talk will help you understand how app architecture and auth security impact each other. You will learn the most secure, standards-based architecture pattern for implementing auth for JS apps.

This talk has been presented at JSNation US 2025, check out the latest edition of this JavaScript Conference.

FAQ

Developers often struggle with token storage, 401 errors, and CORS issues rather than the expected production issues.

OAuth uses OAuth2 for authorization and OIDC (OpenID Connect) for authentication, both of which utilize an authorization server to issue tokens.

The back end can authenticate with the authorization server, persist sessions, and safely store tokens, whereas the front end cannot safely use a secret, persist sessions, or store tokens.

The back end can provide the security benefits of OAuth for the front end by serving it and managing tokens, thus overcoming front end security limitations.

A PXE challenge involves creating a random string called a code verifier and using a code challenge method to hash the verifier, creating a code challenge for secure OAuth token exchanges.

The back end creates a session ID, stores tokens, and sets a session ID cookie with a time to live matching the refresh token expiration. It checks session validity on each front end load.

If the access token expires but the refresh token is valid, the back end performs a refresh grant to obtain new tokens from the authorization server.

The BFF pattern allows the back end to handle security concerns, token storage, and API requests, providing the security benefits of back end OAuth to the front end without its security issues.

The front end calls the back end logout endpoint, which redirects to the auth server's logout URI. The back end removes the session, deletes the session ID cookie, and redirects to a front end logout callback page.

A code demo of back end for front end OAuth, along with other auth architectures, is available at a specific GitHub repository provided by Kim Meida from FusionAuth.

Kim Maida
Kim Maida
6 min
17 Nov, 2025

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Challenges in OAuth, importance of back end security for front end, and implementation details of OAuth flow between front end, back end, and authorization server. Detailed process of token handling, session management, and user data retrieval in OAuth implementation between front end, back end, and authorization server.

1. Understanding OAuth Security Flow

Short description:

Challenges in OAuth, importance of back end security for front end, and implementation details of OAuth flow between front end, back end, and authorization server.

So when it comes to OAuth, many of the challenges that devs run into aren't actually the things they're expecting to have issues with. We worry about things like production issues and then end up struggling with token storage and 401 errors and cores.

Now, standards-based OAuth uses OAuth2 for authorization and OIDC for authentication. Both of these specifications use an authorization server to issue tokens. Now, let's compare OAuth in the back end and the front end. The back end can authenticate itself with the authorization server. It can persist user sessions and it can safely store tokens.

On the other hand, a front end app can't safely use a secret. It can't persist sessions, and it can't safely store tokens either. So these security issues are definitely not trivial. But if you control a back end that's serving your front end, you can get the security of back end OAuth in your front end.

So let's say you have a JavaScript front end, a code executing back end serving the front end, and a standards-based authorization server. The user loads the front end, which calls the back end OAuth API to check for an existing session. If they don't have a session, the back end creates a proof key for code exchange or PXE challenge. This means it creates random string called a code verifier and uses a code challenge method to hash the code verifier to create what we call the code challenge. The back end then sets an HTTP-only PXE cookie.

And when the user logs in, the front end calls the back end login endpoint. The back end composes an authorization request and redirects to the authorization server's authorized URI. And it includes the code challenge with this request. Now the auth server verifies the user's credentials and redirects to the back end callback route with the authorization code. The back end calls the auth server's token endpoint, and it includes the client secret, the code, and the code verifier.

2. OAuth Token Handling and User Data Retrieval

Short description:

Detailed process of token handling, session management, and user data retrieval in OAuth implementation between front end, back end, and authorization server.

And the auth server uses the code challenge method to create the code challenge and then match it to the one from the authorization request. If all this is successful, then the authorization server returns tokens. Now the back end can delete the PXE cookie and then create a session ID and store tokens. The session's time to live should match the refresh token expiration. Back end then sets a session ID cookie and redirects the front end to a login callback page.

Now whenever the front end app loads, it calls a back end check session endpoint with the session ID cookie if it exists. And the back end uses that session ID to look up the user's session. If there's a valid session, then the back end uses the access token expiration to schedule a refresh grant for when the token expires. If the access token is expired but the refresh token is valid, then we're going to perform a refresh grant by sending the refresh token to the auth server's token endpoint. The auth server sends new tokens down the wire, which are saved in the session.

Now when the app needs user data, it can call a user info endpoint with the session cookie. The back end looks up the session, and then it can either decode the ID token or it can call the authorization server's user info endpoint with the access token in the authorization header. The authorization server returns user info, which is stored in the user's session and then is sent to the front end. Now if the check session looks up the session ID and either the session is expired or there is no cookie, or the session just doesn't exist, then the user's not logged in and they must do so to proceed.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Top Content
The talk discusses the importance of supply chain security in the open source ecosystem, highlighting the risks of relying on open source code without proper code review. It explores the trend of supply chain attacks and the need for a new approach to detect and block malicious dependencies. The talk also introduces Socket, a tool that assesses the security of packages and provides automation and analysis to protect against malware and supply chain attacks. It emphasizes the need to prioritize security in software development and offers insights into potential solutions such as realms and Deno's command line flags.
The State of Passwordless Auth on the Web
JSNation 2023JSNation 2023
30 min
The State of Passwordless Auth on the Web
Passwords are terrible and easily hacked, with most people not using password managers. The credential management API and autocomplete attribute can improve user experience and security. Two-factor authentication enhances security but regresses user experience. Passkeys offer a seamless and secure login experience, but browser support may be limited. Recommendations include detecting Passkey support and offering fallbacks to passwords and two-factor authentication.
5 Ways You Could Have Hacked Node.js
JSNation 2023JSNation 2023
22 min
5 Ways You Could Have Hacked Node.js
Top Content
The Node.js security team is responsible for addressing vulnerabilities and receives reports through HackerOne. The Talk discusses various hacking techniques, including DLL injections and DNS rebinding attacks. It also highlights Node.js security vulnerabilities such as HTTP request smuggling and certification validation. The importance of using HTTP proxy tunneling and the experimental permission model in Node.js 20 is emphasized. NearForm, a company specializing in Node.js, offers services for scaling and improving security.
Content Security Policy with Next.js: Leveling Up your Website's Security
React Summit US 2023React Summit US 2023
9 min
Content Security Policy with Next.js: Leveling Up your Website's Security
Top Content
Watch video: Content Security Policy with Next.js: Leveling Up your Website's Security
Lucas Estevão, a Principal UI Engineer and Technical Manager at Avenue Code, discusses how to implement Content Security Policy (CSP) with Next.js to enhance website security. He explains that CSP is a security layer that protects against cross-site scripting and data injection attacks by restricting browser functionality. The talk covers adding CSP to an XJS application using meta tags or headers, and demonstrates the use of the 'nonce' attribute for allowing inline scripts securely. Estevão also highlights the importance of using content security reports to identify and improve application security.
Server-side Auth with Remix, Prisma, and the Web Platform
Node Congress 2022Node Congress 2022
34 min
Server-side Auth with Remix, Prisma, and the Web Platform
Top Content
This Talk is about server-side authentication with Remix, Prisma, and the web platform. It covers adding authentication to a Remix app, troubleshooting and login setup, handling user login and session creation, creating user sessions and redirects, handling user ID retrieval and validation, and working with cookies in Remix. The speaker emphasizes that Remix is ready for production and suitable for enterprise apps. Remix simplifies the mental model and improves performance by bridging the network gap between the front end and back end.
How React Applications Get Hacked in the Real-World
React Summit 2022React Summit 2022
7 min
How React Applications Get Hacked in the Real-World
Top Content
How to hack a RealWorld live React application in seven minutes. Tips, best practices, and pitfalls when writing React code. XSS and cross-site scripting in React. React's secure by default, but not always. The first thing to discover: adding a link to a React application. React code vulnerability: cross-site scripting with Twitter link. React doesn't sanitize or output H ref attributes. Fix attempts: detect JavaScript, use dummy hashtag, transition to lowercase. Control corrector exploit. Best practices: avoid denialist approach, sanitize user inputs. React's lack of sanitization and output encoding for user inputs. Exploring XSS vulnerabilities and the need to pretty print JSON. The React JSON pretty package and its potential XSS risks. The importance of context encoding and secure coding practices.

Workshops on related topic

Hands-On Workshop: Introduction to Pentesting for Web Apps / Web APIs
JSNation US 2024JSNation US 2024
148 min
Hands-On Workshop: Introduction to Pentesting for Web Apps / Web APIs
Featured Workshop
Gregor Biswanger
Gregor Biswanger
In this hands-on workshop, you will be equipped with the tools to effectively test the security of web applications. This course is designed for beginners as well as those already familiar with web application security testing who wish to expand their knowledge. In a world where websites play an increasingly central role, ensuring the security of these technologies is crucial. Understanding the attacker's perspective and knowing the appropriate defense mechanisms have become essential skills for IT professionals.This workshop, led by the renowned trainer Gregor Biswanger, will guide you through the use of industry-standard pentesting tools such as Burp Suite, OWASP ZAP, and the professional pentesting framework Metasploit. You will learn how to identify and exploit common vulnerabilities in web applications. Through practical exercises and challenges, you will be able to put your theoretical knowledge into practice and expand it. In this course, you will acquire the fundamental skills necessary to protect your websites from attacks and enhance the security of your systems.
Authentication Beyond Passwords
React Day Berlin 2023React Day Berlin 2023
127 min
Authentication Beyond Passwords
WorkshopFree
Juan Cruz Martinez
Juan Cruz Martinez
Passwords have long been the keys to our kingdoms. However, they often become the weak points in our armor — forgotten, misused, or exploited. Our Next apps often make use of passwords to authenticate users, but what would a world with no passwords look like? And how we can start driving into that future today?
0 to Auth in an hour with ReactJS
React Summit 2023React Summit 2023
56 min
0 to Auth in an hour with ReactJS
WorkshopFree
Kevin Gao
Kevin Gao
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool. There are multiple alternatives that are much better than passwords to identify and authenticate your users - including SSO, SAML, OAuth, Magic Links, One-Time Passwords, and Authenticator Apps.
While addressing security aspects and avoiding common pitfalls, we will enhance a full-stack JS application (Node.js backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session securely for subsequent client requests, validating / refreshing sessions- Basic Authorization - extracting and validating claims from the session token JWT and handling authorization in backend flows
At the end of the workshop, we will also touch other approaches of authentication implementation with Descope - using frontend or backend SDKs.
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
OWASP Top Ten Security Vulnerabilities in Node.js
JSNation 2024JSNation 2024
97 min
OWASP Top Ten Security Vulnerabilities in Node.js
Workshop
Marco Ippolito
Marco Ippolito
In this workshop, we'll cover the top 10 most common vulnerabilities and critical security risks identified by OWASP, which is a trusted authority in Web Application Security.During the workshop, you will learn how to prevent these vulnerabilities and develop the ability to recognize them in web applications.The workshop includes 10 code challenges that represent each of the OWASP's most common vulnerabilities. There will be given hints to help solve the vulnerabilities and pass the tests.The trainer will also provide detailed explanations, slides, and real-life examples in Node.js to help understand the problems better. Additionally, you'll gain insights from a Node.js Maintainer who will share how they manage security within a large project.It's suitable for Node.js Developers of all skill levels, from beginners to experts, it requires a general knowledge of web application and JavaScript.
Table of contents:- Broken Access Control- Cryptographic Failures- Injection- Insecure Design- Security Misconfiguration- Vulnerable and Outdated Components- Identification and Authentication Failures- Software and Data Integrity Failures- Security Logging and Monitoring Failures- Server-Side Request Forgery
0 To Auth In An Hour For Your JavaScript App
JSNation 2023JSNation 2023
57 min
0 To Auth In An Hour For Your JavaScript App
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.js backend + Vanilla JS frontend) to authenticate users with One Time Passwords (email) and OAuth, including:
- User authentication – Managing user interactions, returning session / refresh JWTs- Session management and validation – Storing the session securely for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.