Skip to content

Commit 5fb2e47

Browse files
committed
resume verify if timeout
1 parent 8404a1c commit 5fb2e47

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

components/magicLink/login.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,30 @@ export default function Login({ challenge, clinical=false, authonly=false, clien
5151
if (client !== '') {
5252
setClientExist(true)
5353
}
54+
if (localStorage.getItem('email') !== null) {
55+
setEmailValue(localStorage.getItem('email') || '');
56+
createPassKey();
57+
}
5458
}, [client]);
5559

5660
const createPassKey = async () => {
5761
if (email !== '') {
5862
if (validate(email)) {
5963
// Check if user has an account
60-
setIsChecking(true)
64+
localStorage.setItem('email', email);
65+
setIsChecking(true);
6166
const isRegistered = await fetch("/api/couchdb/patients/" + email,
6267
{ method: "GET", headers: {"Content-Type": "application/json"} })
6368
.then((res) => res.json()).then((json) => json._id);
64-
const nonce = await fetch("/api/auth/create",
65-
{ method: "PUT", headers: {"Content-Type": "application/json"}, body: JSON.stringify({email: email} )})
66-
.then((res) => res.json()).then((json) => json.nonce);
69+
let nonce = '';
70+
if (localStorage.getItem('nonce') === null || localStorage.getItem('nonce') === '') {
71+
nonce = await fetch("/api/auth/create",
72+
{ method: "PUT", headers: {"Content-Type": "application/json"}, body: JSON.stringify({email: email} )})
73+
.then((res) => res.json()).then((json) => json.nonce);
74+
localStorage.setItem('nonce', nonce);
75+
} else {
76+
nonce = localStorage.getItem('nonce') || '';
77+
}
6778
let check = false;
6879
let proceed = false;
6980
let timer = 0;
@@ -83,6 +94,8 @@ export default function Login({ challenge, clinical=false, authonly=false, clien
8394
}
8495
}
8596
if (proceed) {
97+
localStorage.removeItem('email');
98+
localStorage.removeItem('nonce');
8699
await fetch("/api/magicLink/login",
87100
{ method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({email: email}) });
88101
if (isRegistered === undefined) {

pages/api/as/notify.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
2525
}
2626
if (proceed) {
2727
const access = req.body.access.join(', ');
28-
console.log(access)
2928
const message = req.body.from + ' (' + req.body.from_email + ') has invited you to <b>' + access + '</b> the following health record:';
3029
const htmlContent = fs.readFileSync(path.join(process.cwd(), 'public', 'email.html'), 'utf-8');
3130
const htmlFinal = htmlContent.replace(/[\r\n]+/gm, '')

pages/api/as/sendmail.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { NextApiRequest, NextApiResponse } from 'next';
2+
import NextCors from 'nextjs-cors';
3+
import verifySig from '../../../lib/verifySig';
4+
import verifyJWT from '../../../lib/verifyJWT';
5+
import objectPath from 'object-path';
6+
import fs from 'fs';
7+
import path from 'path';
8+
9+
const domain: string = process.env.DOMAIN !== undefined ? process.env.DOMAIN: '';
10+
11+
async function handler(req: NextApiRequest, res: NextApiResponse) {
12+
await NextCors(req, res, {
13+
methods: ["POST"],
14+
origin: '*',
15+
optionsSuccessStatus: 200
16+
});
17+
// const body = {
18+
19+
// from: '',
20+
// from_email: '[email protected]',
21+
// subject: '',
22+
// title: '',
23+
// previewtext: '',
24+
// paragraphtext: '',
25+
// paragraphtext2: '',
26+
// link: 'https://example.com',
27+
// buttonstyle: 'display:block' || 'display:none',
28+
// buttontext: ''
29+
// }
30+
if (await verifySig(req)) {
31+
if (objectPath.has(req, 'body.to')) {
32+
let proceed = false;
33+
if (req.headers['authorization'] !== undefined) {
34+
const jwt = req.headers['authorization'].split(' ')[1];
35+
if (await verifyJWT(jwt, objectPath.get(req, 'body.from_email'))) {
36+
proceed = true;
37+
}
38+
}
39+
if (proceed) {
40+
const htmlContent = fs.readFileSync(path.join(process.cwd(), 'public', 'email.html'), 'utf-8');
41+
const htmlFinal = htmlContent.replace(/[\r\n]+/gm, '')
42+
.replace('@title', req.body.title)
43+
.replace('@previewtext', req.body.previewtext)
44+
.replace('@paragraphtext', req.body.paragraphtext)
45+
.replace('@2paragraphtext', req.body.paragraphtext2)
46+
.replaceAll('@link', req.body.link)
47+
.replace('@buttonstyle', req.body.buttonstyle)
48+
.replace('@buttontext', req.body.buttontext);
49+
const sendmail = await fetch(domain + "/api/sendmail", {
50+
method: "POST",
51+
headers: {
52+
"Content-Type": "application/json",
53+
},
54+
body: JSON.stringify({
55+
email: req.body.to,
56+
subject: req.body.subject,
57+
html: htmlFinal,
58+
})
59+
});
60+
const { error } = await sendmail.json();
61+
if (error) {
62+
console.log(error);
63+
}
64+
res.status(200).json({success: true});
65+
} else {
66+
res.status(401).send('Unauthorized - verify JWT failed');
67+
}
68+
}
69+
} else {
70+
res.status(401).send('Unauthorized - verify signature failed');
71+
}
72+
}
73+
74+
export default handler;

0 commit comments

Comments
 (0)