Skip to content

Commit

Permalink
add notify
Browse files Browse the repository at this point in the history
  • Loading branch information
shihjay2 committed Sep 26, 2024
1 parent 7c54703 commit 772369d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components/magicLink/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default function Login({ challenge, clinical=false, authonly=false, clien
<Stack spacing={2}>
<Button variant="contained" onClick={passKey} startIcon={<div><PersonIcon/><KeyIcon/><Fingerprint/></div>}>Sign In with PassKey</Button>
{isChecking ? (<Grid style={{ textAlign: "center" }}><CircularProgress color="primary" sx={{mr:2}}/>Check your e-mail to verify...</Grid>) : (<div></div>)}
{isTimeout ? (<Grid style={{ textAlign: "center" }}>Verification Timed Out - Click on the link below to try again...</Grid>) : (<div></div>)}
{isTimeout ? (<Grid style={{ textAlign: "center" }}><Typography variant="body2" color="error.main">Verification Timed Out - Click on the link below to try again...</Typography></Grid>) : (<div></div>)}
{authonly || clinical ? (
<Grid style={{ textAlign: "center" }}>New to Trustee? <Link component="button" onClick={createPassKey}>Create your Passkey</Link></Grid>
) : (
Expand Down
63 changes: 63 additions & 0 deletions pages/api/as/notify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { NextApiRequest, NextApiResponse } from 'next';
import NextCors from 'nextjs-cors';
import verifySig from '../../../lib/verifySig';
import verifyJWT from '../../../lib/verifyJWT';
import objectPath from 'object-path';
import fs from 'fs';
import path from 'path';

const domain: string = process.env.DOMAIN !== undefined ? process.env.DOMAIN: '';

async function handler(req: NextApiRequest, res: NextApiResponse) {
await NextCors(req, res, {
methods: ["POST"],
origin: '*',
optionsSuccessStatus: 200
});
if (await verifySig(req)) {
if (objectPath.has(req, 'body.to')) {
let proceed = false;
if (req.headers['authorization'] !== undefined) {
const jwt = req.headers['authorization'].split(' ')[1];
if (await verifyJWT(jwt, objectPath.get(req, 'body.access.ro'))) {
proceed = true;
}
}
if (proceed) {
const access = req.body.access.join(', ');
const message = req.body.from + '(' + req.body.from_email + ') has invited you to <b>' + access + '</b> the folowing health record:';
const htmlContent = fs.readFileSync(path.join(process.cwd(), 'public', 'email.html'), 'utf-8');
const htmlFinal = htmlContent.replace(/[\r\n]+/gm, '')
.replace('@title', 'HIE of One - Health Record Shared With You')
.replace('@previewtext', 'HIE of One - Health Record Shared With You')
.replace('@paragraphtext', `<h3>${req.body.from} shared a health record resource</h3>${message}`)
.replace('@2paragraphtext', '')
.replaceAll('@link', req.body.url)
.replace('@buttonstyle', 'display:block')
.replace('@buttontext', 'Link to their Personal Health Record');
const sendmail = await fetch(domain + "/api/sendmail", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: req.body.privilege,
subject: "HIE of One - Resource Privilege Approved",
html: htmlFinal,
})
});
const { error } = await sendmail.json();
if (error) {
console.log(error);
}
res.status(200).json({success: true});
} else {
res.status(401).send('Unauthorized - verify JWT failed');
}
}
} else {
res.status(401).send('Unauthorized - verify signature failed');
}
}

export default handler;

0 comments on commit 772369d

Please sign in to comment.