Before we can save the token to Local Storage, we need to send the token to the browser.
We’re going to use the query string method to stay consistent with the apps we’ve created so far.
Sending token to frontend via the query string
Let’s take a look at the token Facebook sends back to us and see what values we need to send to our frontend.
We can see that expires_in is 5183280 minutes.
- This means the access token will expire in 1439.8 hours. (Divide it by 3600).
- It further means the access token will expire in 59.99 days. (Further divide it by 24).
Pretty cool! Facebook has given us a pretty long-lived access token and we don’t have to worry about refreshing the token for 60 days!
But it still expires… so we still need to create an expiresAt value and send it to the frontend. After all, we want to allow users to login to our app for longer than 60 days, don’t we?
const expiresAt = token.expires_in * 1000 + Date.now()So with everything we’ve seen so far, we know we need to send four values to the frontend.
authStatusprovideraccess_tokenexpiresAt
Here’s how you can do this.
router.get('/auth/github/callback', async (req, res) => { // ... const token = response.body const expiresAt = token.expires_in * 1000 + Date.now()
// Sends the token to the browser to be stored in Local Storage const params = { authStatus: 'success', provider: 'facebook', expiresAt, access_token: token.access_token, }
const query = new URLSearchParams(params).toString() res.redirect('/' + '?' + query)})Getting and saving the token
The code we have written so far already gets and saves the token values. So there’s nothing we have to do here!
If you look at your local storage, you should be able to see the access token from Facebook.
Let’s move on.
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