@@ -129,12 +129,12 @@ async function refreshIdentity(identity) {
129129 } ;
130130
131131 try {
132- const encryptedResponse = await axios . post ( uidBaseUrl + '/v2/token/refresh' , identity . refresh_token , headers ) ; //if HTTP response code is not 200, this throws and is caught in the catch handler below.
132+ const encryptedResponse = await axios . post ( uidBaseUrl + '/v2/token/refresh' , identity . refresh_token , headers ) ;
133133
134134 let response ;
135135 if ( identity . refresh_response_key ) {
136136 response = decrypt ( encryptedResponse . data , identity . refresh_response_key , true ) ;
137- } else { //If refresh_response_key doesn't exist, assume refresh_token came from a v1/token/generate query. In that scenario, /v2/token/refresh will return an unencrypted response.
137+ } else {
138138 response = encryptedResponse . data ;
139139 }
140140
@@ -151,98 +151,32 @@ async function refreshIdentity(identity) {
151151 return Date . now ( ) >= identity . identity_expires ? undefined : identity ;
152152 }
153153}
154- async function verifyIdentity ( req ) {
154+
155+ async function getValidIdentity ( req ) {
155156 if ( ! isRefreshableIdentity ( req . session . identity ) ) {
156- return false ;
157+ return null ;
157158 }
158159
159160 if ( Date . now ( ) >= req . session . identity . refresh_from || Date . now ( ) >= req . session . identity . identity_expires ) {
160161 req . session . identity = await refreshIdentity ( req . session . identity ) ;
161162 }
162163
163- return ! ! req . session . identity ;
164- }
165- async function protect ( req , res , next ) {
166- if ( await verifyIdentity ( req ) ) {
167- next ( ) ;
168- } else {
169- req . session = null ;
170- res . redirect ( '/login' ) ;
171- }
164+ return req . session . identity ;
172165}
173166
174- app . get ( '/' , protect , ( req , res ) => {
167+ // Main page - shows login form or identity state
168+ app . get ( '/' , async ( req , res ) => {
169+ const identity = await getValidIdentity ( req ) ;
170+
175171 res . render ( 'index' , {
176- identity : req . session . identity ,
172+ identity : identity ,
173+ isOptout : false ,
177174 identityName,
178175 docsBaseUrl
179176 } ) ;
180177} ) ;
181- app . get ( '/content1' , protect , ( req , res ) => {
182- res . render ( 'content' , {
183- identity : req . session . identity ,
184- content : 'First Sample Content' ,
185- identityName,
186- docsBaseUrl
187- } ) ;
188- } ) ;
189- app . get ( '/content2' , protect , ( req , res ) => {
190- res . render ( 'content' , {
191- identity : req . session . identity ,
192- content : 'Second Sample Content' ,
193- identityName,
194- docsBaseUrl
195- } ) ;
196- } ) ;
197- app . get ( '/login' , async ( req , res ) => {
198- if ( await verifyIdentity ( req ) ) {
199- res . redirect ( '/' ) ;
200- } else {
201- req . session = null ;
202- res . render ( 'login' , {
203- identityName,
204- docsBaseUrl
205- } ) ;
206- }
207- } ) ;
208-
209-
210- function _GenerateTokenV1 ( req , res ) {
211- axios . get ( uidBaseUrl + '/v1/token/generate?email=' + encodeURIComponent ( req . body . email ) , { headers : { 'Authorization' : 'Bearer ' + uidApiKey } } )
212- . then ( ( response ) => {
213- if ( response . data . status !== 'success' ) {
214- res . render ( 'error' , {
215- error : 'Got unexpected token generate status: ' + response . data . status ,
216- response,
217- identityName,
218- docsBaseUrl
219- } ) ;
220- } else if ( typeof response . data . body !== 'object' ) {
221- res . render ( 'error' , {
222- error : 'Unexpected token generate response format: ' + response . data ,
223- response,
224- identityName,
225- docsBaseUrl
226- } ) ;
227- } else {
228- req . session . identity = response . data . body ;
229- res . redirect ( '/' ) ;
230- }
231- } )
232- . catch ( ( error ) => {
233- res . render ( 'error' , {
234- error,
235- response : error . response ,
236- identityName,
237- docsBaseUrl
238- } ) ;
239- } ) ;
240- }
241178
242179app . post ( '/login' , async ( req , res ) => {
243- //Uncomment the following line to test that stored v1 sessions will still work when we upgrade to /v2/token/refresh.
244- //_GenerateTokenV1(req, res); return;
245-
246180 const jsonEmail = JSON . stringify ( { 'email' : req . body . email } ) ;
247181 const { envelope, nonce } = createEnvelope ( jsonEmail ) ;
248182
@@ -251,41 +185,61 @@ app.post('/login', async (req, res) => {
251185 } ;
252186
253187 try {
254- const encryptedResponse = await axios . post ( uidBaseUrl + '/v2/token/generate' , envelope , headers ) ; //if HTTP response code is not 200, this throws and is caught in the catch handler below.
188+ const encryptedResponse = await axios . post ( uidBaseUrl + '/v2/token/generate' , envelope , headers ) ;
255189 const response = decrypt ( encryptedResponse . data , uidClientSecret , false , nonce ) ;
256190
257- if ( response . status !== 'success' ) {
258- res . render ( 'error' , {
259- error : 'Got unexpected token generate status in decrypted response: ' + response . status ,
260- response,
191+ if ( response . status === 'optout' ) {
192+ // User has opted out - show optout state
193+ req . session . identity = null ;
194+ res . render ( 'index' , {
195+ identity : null ,
196+ isOptout : true ,
197+ identityName,
198+ docsBaseUrl
199+ } ) ;
200+ } else if ( response . status !== 'success' ) {
201+ // Error - show error state
202+ res . render ( 'index' , {
203+ identity : null ,
204+ isOptout : false ,
205+ error : 'Got unexpected token generate status: ' + response . status ,
261206 identityName,
262207 docsBaseUrl
263208 } ) ;
264209 } else if ( typeof response . body !== 'object' ) {
265- res . render ( 'error' , {
266- error : 'Unexpected token generate response format in decrypted response: ' + response ,
267- response,
210+ // Error - show error state
211+ res . render ( 'index' , {
212+ identity : null ,
213+ isOptout : false ,
214+ error : 'Unexpected token generate response format' ,
268215 identityName,
269216 docsBaseUrl
270217 } ) ;
271218 } else {
219+ // Success - store identity and show logged in state
272220 req . session . identity = response . body ;
273- res . redirect ( '/' ) ;
221+ res . render ( 'index' , {
222+ identity : response . body ,
223+ isOptout : false ,
224+ identityName,
225+ docsBaseUrl
226+ } ) ;
274227 }
275228 } catch ( error ) {
276- res . render ( 'error' , {
277- error,
278- response : error . response ,
229+ console . error ( 'Token generation failed:' , error ) ;
230+ res . render ( 'index' , {
231+ identity : null ,
232+ isOptout : false ,
233+ error : 'Token generation failed: ' + error . message ,
279234 identityName,
280235 docsBaseUrl
281236 } ) ;
282237 }
283-
284238} ) ;
285239
286240app . get ( '/logout' , ( req , res ) => {
287241 req . session = null ;
288- res . redirect ( '/login ' ) ;
242+ res . redirect ( '/' ) ;
289243} ) ;
290244
291245app . listen ( port , ( ) => {
0 commit comments