We let users login with Github first because it’s something we already know how to.
There are four steps:
- Create the app
- Create the sign-in link
- User logs in on Github
- Exchange code for access token
Create the app
We’re going to create an app on Github. Here are the instructions from Github on how to do this.
You can name the app anything you want.
- I named it Social Media Login App.
- We’re going to set
callbackURLtohttps://auth/github/callback.
Pay attention to the https protocol. Make sure you use https instead of http for this app.
We need this https protocol because we served this app with https.
Before you move on, make sure you add your Github credentials to the .env file.
GITHUB_CLIENT_ID="Your Client ID"GITHUB_CLIENT_SECRET="Your Client Secret"Create a sign-in link
When the user clicks the sign-in link, we will redirect the user to Github. We can do this in two steps:
- We direct the user to an endpoint in our server.
- In this endpoint, we redirect the user to Github.
<!-- Direct user to endpoint in our server --><a href="/auth/github" ...> Login with Github </a>In this endpoint, we craft the sign-in link and redirect the user to Github.
router.get('/auth/github', (req, res) => { const baseURL = 'https://github.com/login/oauth/authorize'
const params = { client_id: process.env.GITHUB_CLIENT_ID, }
const query = new URLSearchParams(params) const link = baseURL + '?' + query.toString() return link})User logs in to Github
There’s nothing for us to do here :)
Exchange code for access token
Github will redirect the user to our callback url (/auth/github/callback) after they have logged in on Github. This callback url will contain a code query paramater.
We need to use this code value to get an access token.
We can grab the code value from the url through req.query.
router.get('/auth/github/callback', async (req, res) => { const { code } = req.query})Then we send this code to Github’s token endpoint to get the access token.
router.get('/auth/github/callback', async (req, res) => { // Exchange code for access token const response = await zlFetch.post( 'https://github.com/login/oauth/access_token', { body: { client_id: process.env.GITHUB_CLIENT_ID, client_secret: process.env.GITHUB_CLIENT_SECRET, code, }, }, )
// Get the token const token = response.body console.log(token)})You should receive a token like this:
The user is now logged in.
Handling the access token
We can save the access token in three places:
- A database
- Local storage
- Cookies
We’re going to store the access token in local storage for this app. We do this because of these three reasons:
- Local storage is easier to understand for frontend developers (like us).
- Local storage and cookies work the same way. They’re just saved in different places, which means the implementation is similar. Once you know how to use one, you’ll know how to use the other one.
- If you want to use cookies, you need to know how to use template engines on the server. We did not cover this in the course so it’s harder to explain how to use cookies.
When you build a real application, make sure you always store the access token in your database. You want to do this because you can use it to verify the access token. More on this in a later lesson.
That’s it for this lesson. Let’s move on to the next one.
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