Skip to content

Commit 2208be0

Browse files
fix optout bug by clearing session and clean up logs
1 parent 986f3be commit 2208be0

File tree

1 file changed

+8
-85
lines changed
  • web-integrations/javascript-sdk/server-side

1 file changed

+8
-85
lines changed

web-integrations/javascript-sdk/server-side/server.js

Lines changed: 8 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -104,72 +104,6 @@ app.set('view engine', 'html');
104104

105105
app.use(nocache());
106106

107-
/**
108-
* Check if an identity is still valid and refreshable
109-
*/
110-
function isRefreshableIdentity(identity) {
111-
if (!identity || typeof identity !== 'object') {
112-
return false;
113-
}
114-
if (!identity.refresh_expires || Date.now() >= identity.refresh_expires) {
115-
return false;
116-
}
117-
return !!identity.refresh_token;
118-
}
119-
120-
/**
121-
* Refresh an identity token using the SDK
122-
* The SDK will automatically refresh tokens when initialized with an existing identity
123-
*/
124-
async function refreshIdentity(identity) {
125-
// TODO: Use JS SDK to refresh identity
126-
// The SDK's init() with an existing identity will handle refresh automatically
127-
// Example:
128-
// const sdk = getUid2Sdk();
129-
// return new Promise((resolve) => {
130-
// sdk.init({
131-
// baseUrl: uidBaseUrl,
132-
// identity: identity
133-
// });
134-
// sdk.callbacks.push((eventType, payload) => {
135-
// if (eventType === 'IdentityUpdated') {
136-
// resolve(payload.identity);
137-
// }
138-
// });
139-
// });
140-
141-
console.log('TODO: Implement SDK-based identity refresh');
142-
return identity;
143-
}
144-
145-
/**
146-
* Verify and refresh identity if needed
147-
*/
148-
async function verifyIdentity(req) {
149-
if (!isRefreshableIdentity(req.session.identity)) {
150-
return false;
151-
}
152-
153-
// Check if identity needs refresh
154-
if (Date.now() >= req.session.identity.refresh_from || Date.now() >= req.session.identity.identity_expires) {
155-
req.session.identity = await refreshIdentity(req.session.identity);
156-
}
157-
158-
return !!req.session.identity;
159-
}
160-
161-
/**
162-
* Middleware to protect routes that require authentication
163-
*/
164-
async function protect(req, res, next) {
165-
if (await verifyIdentity(req)) {
166-
next();
167-
} else {
168-
req.session = null;
169-
res.redirect('/login');
170-
}
171-
}
172-
173107
// Routes
174108

175109
/**
@@ -198,43 +132,28 @@ app.post('/login', async (req, res) => {
198132
}
199133

200134
try {
201-
console.log(`Generating token for email: ${req.body.email}`);
202-
203-
// Use the SDK's setIdentityFromEmail method
204-
// This is the same method used in browser environments
135+
// Call the SDK's setIdentityFromEmail method and wait for the result via callback
205136
const identity = await new Promise((resolve, reject) => {
206137
const timeout = setTimeout(() => {
207138
reject(new Error('Token generation timed out after 10 seconds'));
208139
}, 10000);
209140

210-
// Add callback to capture the identity or optout
211141
const callbackHandler = (eventType, payload) => {
212-
// Handle successful identity generation
213142
if ((eventType === 'InitCompleted' || eventType === 'IdentityUpdated') && payload?.identity) {
214143
clearTimeout(timeout);
215-
// Remove this specific callback
216-
const index = uid2Sdk.callbacks.indexOf(callbackHandler);
217-
if (index > -1) {
218-
uid2Sdk.callbacks.splice(index, 1);
219-
}
144+
uid2Sdk.callbacks.splice(uid2Sdk.callbacks.indexOf(callbackHandler), 1);
220145
resolve(payload.identity);
221146
}
222147

223-
// Handle optout - user has opted out of UID2
224148
if (eventType === 'OptoutReceived') {
225149
clearTimeout(timeout);
226-
// Remove this specific callback
227-
const index = uid2Sdk.callbacks.indexOf(callbackHandler);
228-
if (index > -1) {
229-
uid2Sdk.callbacks.splice(index, 1);
230-
}
150+
uid2Sdk.callbacks.splice(uid2Sdk.callbacks.indexOf(callbackHandler), 1);
231151
reject(new Error('Got unexpected token generate status: optout'));
232152
}
233153
};
234154

235155
uid2Sdk.callbacks.push(callbackHandler);
236156

237-
// Call setIdentityFromEmail
238157
uid2Sdk.setIdentityFromEmail(
239158
req.body.email,
240159
{
@@ -255,7 +174,8 @@ app.post('/login', async (req, res) => {
255174
res.redirect('/');
256175

257176
} catch (error) {
258-
console.error('Token generation failed:', error);
177+
console.error('Token generation failed:', error.message);
178+
req.session = null;
259179
res.render('error', {
260180
error: error.message || error.toString(),
261181
response: error.response || null,
@@ -269,6 +189,9 @@ app.post('/login', async (req, res) => {
269189
* Logout endpoint - clears session and returns to main page
270190
*/
271191
app.get('/logout', (req, res) => {
192+
if (uid2Sdk && uid2Sdk.disconnect) {
193+
uid2Sdk.disconnect();
194+
}
272195
req.session = null;
273196
res.redirect('/');
274197
});

0 commit comments

Comments
 (0)