Skip to content

Commit

Permalink
wrap in try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewHEguardian committed Oct 23, 2024
1 parent 2db7b3b commit 27df4b6
Showing 1 changed file with 50 additions and 40 deletions.
90 changes: 50 additions & 40 deletions server/routes/newspaperArchive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,58 @@ const router = Router();
router.use(withIdentity(401));

router.get('/auth', async (req: Request, res: Response) => {
const config = await newspaperArchiveConfigPromise;
const authString = config?.authString;
if (authString === undefined) {
log.error(`Missing newspaper archive auth key`);
return res.sendStatus(500);
}

const hasCorrectEntitlement = await checkSupporterEntitlement(req);

if (!hasCorrectEntitlement) {
// ToDo: show the user an error/info page
return res.redirect('/');
}

const authHeader = base64(`${authString}`);
const requestBody: NewspapersRequestBody = {};

const response = await fetch(
'https://www.newspapers.com/api/userauth/public/get-tpa-token',
{
headers: {
Authorization: `Basic ${authHeader}`,
'Content-Type': 'application/json',
try {
const config = await newspaperArchiveConfigPromise;
const authString = config?.authString;
if (authString === undefined) {
log.error(`Missing newspaper archive auth key`);
return res.sendStatus(500);
}

const hasCorrectEntitlement = await checkSupporterEntitlement(req);

if (!hasCorrectEntitlement) {
// ToDo: show the user an error/info page
return res.redirect('/');
}

const authHeader = base64(`${authString}`);
const requestBody: NewspapersRequestBody = {};

const response = await fetch(
'https://www.newspapers.com/api/userauth/public/get-tpa-token',
{
headers: {
Authorization: `Basic ${authHeader}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify(requestBody),
},
method: 'POST',
body: JSON.stringify(requestBody),
},
);

// ToDo: we have zod on the server, we could parse the responses with that
const responseJson = (await response.json()) as NewspapersResponseBody;

const archiveReturnUrlString = req.query['ncom-return-url'];
if (archiveReturnUrlString && typeof archiveReturnUrlString === 'string') {
const tpaToken = new URL(responseJson.url).searchParams.get('tpa');

const archiveReturnUrl = new URL(archiveReturnUrlString);
archiveReturnUrl.searchParams.set('tpa', tpaToken ?? '');
return res.redirect(archiveReturnUrl.toString());
);

// ToDo: we have zod on the server, we could parse the responses with that
const responseJson = (await response.json()) as NewspapersResponseBody;

const archiveReturnUrlString = req.query['ncom-return-url'];
if (
archiveReturnUrlString &&
typeof archiveReturnUrlString === 'string'
) {
const tpaToken = new URL(responseJson.url).searchParams.get('tpa');

const archiveReturnUrl = new URL(archiveReturnUrlString);
archiveReturnUrl.searchParams.set('tpa', tpaToken ?? '');
return res.redirect(archiveReturnUrl.toString());
}

return res.redirect(responseJson.url);
} catch (e) {
log.error(
`Something went wrong authenticating with newspapers.com. ${e}`,
);
return res.sendStatus(500);
}

return res.redirect(responseJson.url);
});

export { router };
Expand Down

0 comments on commit 27df4b6

Please sign in to comment.