From 35950a209b364d19db0df9fcc11e9b2a133e473c Mon Sep 17 00:00:00 2001 From: Ste Vaidis Date: Wed, 8 Jan 2025 10:13:34 +0200 Subject: [PATCH] Update OAuth2-Backend-Approach.md --- OAuth2-Backend-Approach.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/OAuth2-Backend-Approach.md b/OAuth2-Backend-Approach.md index a57c492..e3d5ea8 100644 --- a/OAuth2-Backend-Approach.md +++ b/OAuth2-Backend-Approach.md @@ -245,7 +245,7 @@ curl -X GET "https://www.googleapis.com/oauth2/v3/userinfo" \ } ``` -### Frontend Code +### Frontend Dashboard Code ```js import { useEffect, useState } from 'react'; @@ -301,3 +301,24 @@ function DashboardPage() { } ``` +### Backend Dashboard + +```js +app.get('/api/dashboard-data', (req, res) => { + // The auth cookie is automatically included in req.cookies + const authToken = req.cookies.auth_token; + + if (!authToken) { + return res.status(401).json({ error: 'Not authenticated' }); + } + + // Verify the token and get data in one go + try { + // verify token... + // get dashboard data... + res.json({ dashboardData: 'your data here' }); + } catch (err) { + res.status(401).json({ error: 'Invalid token' }); + } +}); +```