Skip to main content

Implementing the callback routes

After the OAuth2 server has verified the authorization code, it will redirect the user to the callback URL. This callback URL is configured in the OAuth2 client settings. The callback endpoint exchanges the temporary authorization code for permanent access tokens while verifying security measures (PKCE challenge and state parameter) to prevent attacks.

index.js
import session from "express-session"
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
}),
)
app.get("/callback", async (req, res) => {
if (!config) {
throw new Error("Config not found")
}

try {
// Get the current URL
const currentUrl = new URL(req.url, `http://${req.headers.host}`)

// Exchange code for tokens with PKCE verification
const tokens = await client.authorizationCodeGrant(config, currentUrl, {
pkceCodeVerifier: req.session.codeVerifier,
expectedState: req.session.state,
})

// Store tokens in session
req.session.tokens = tokens

// Redirect to home page
res.redirect("/")
} catch (error) {
console.error("Callback error:", error)
res.status(500).send(`Authentication failed: ${error.message}`)
}
})

app.listen(3000, () => {
console.log("Server is running on port 3000")
})