It’s slightly harder to use local storage because you need to pass the token to the frontend, before you can save it to local storage.
There are three ways to pass the token:
- Use a query string
- Render HTML that contains some JS
- Use the server as a proxy
We’ll talk about each of these ways and which to choose.
Method 1: Using a query string
This method is straightforward — you pass the access token into a query parameter as you redirect the user to a logged-in page.
router.get('/auth/provider/callback', (req, res) => { // ...
// Redirects to the frontend res.redirect(`/logged-in?token=${access_token}`)})On this logged-in page, you can grab the access token (in the browser) with URLSearchParams.
// Frontendif (location.pathname === '/logged-in') { const searchParams = new URLSearchParams(location.search) const token = searchParams.get('token')}The problem with this method is it exposes the access token in the URL — which is a security concern:
- People may copy-paste this URL and giveaway their token by accident
- Attackers may read the access token if they (somehow) get access to the browser’s history.
- Servers may log URLs into a file. If an attacker (somehow) access this file, they can steal the access token.
Mitigating security concerns
We can mitigate the first and second concerns by overwriting the URL with a new one — one without the query string. We’ll use history.replaceState to do this because replaceState overwrites browser history.
// Frontendif (location.pathname === '/logged-in') { const searchParams = new URLSearchParams(location.search) const token = searchParams.get('token')
// Strips token from the URL so others cannot view them, for security reasons. history.replaceState('', '', location.pathname)}We can mitigate the third concern by deciding how we want the server to log URLs. Learning to do this is outside of the scope of this program.
Method 2: Rendering a HTML with some JavaScript
This method requires you to send HTML that contains some JavaScript. The JavaScript saves to token into local storage and immediately redirects the user to a logged-in page.
router.get('/auth/provider/callback', (req, res) => { // ... const html = ` <html> <script> localStorage.setItem('token', ${access_token}) location.href = '/logged-in' </script> </html> ` // Redirects to the frontend res.send(html)})This method is really simple, and it’s much more secure compared to method 1.
The only downside is you have to hardcode the redirect url in the server.
Method 3: Use the server as a proxy
This third method requires you to handle the callback_url from the browser
if (location.pathname === '/auth/provider/callback') { // Handle callback}You have to obtain code from URLSearchParams and send it to the server with a Fetch request.
if (location.pathname === '/auth/provider/callback') { const searchParams = new URLSearchParams(location.search) const code = searchParams.get('code')
const response = zlFetch.post('/auth/provider/token', { body: { code }, })}The backend will exchange the code value for an access token.
app.post('/auth/provider/token', async (req, res) => { const { code } = req.body
// Get access token const response = await zlFetch.post( 'https://provider.com/oauth/access_token', { body: { client_id: clientID, client_secret: clientSecret, code, }, } )})When the identity provider returns the access token, you send it back to your frontend as a JSON value.
app.post('/auth/provider/token', async (req, res) => { // ... const response = zlFetch.post(/* ... */) const token = response.body.access_token
res.json(token)})Your frontend then receives the token and saves it to local storage.
if (location.pathname === '/auth/provider/callback') { // ... const response = zlFetch.post('/auth/provider/token', { body: { code }, })
const token = response.body.token}This method is secure because you’re not leaking any important credentials. But it’s unwieldy compared to the first two methods mentioned above.
It can get especially unwieldy if you need to include state and code_verifier values.
I don’t recommend method 3.
Summary
You need to pass an access token into the browser before the browser can save them into localStorage.
There are three ways to pass access tokens:
- Using a query string
- Sending HTML with some JavaScript
- Using the server as a proxy
I recommend the 1st method most of the time for ease of use because security concerns can be perfectly mitigated with good practices.
Only choose the 2nd method if you need to make sure the security is super-duper top-notch.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson