forked from toeverything/AFFiNE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(server): use post request to consume magic link token (toeverythi…
- Loading branch information
Showing
6 changed files
with
76 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,13 +69,15 @@ test('should be able to sign in with email', async t => { | |
t.is(res.body.email, u1.email); | ||
t.true(mailer.sendSignInMail.calledOnce); | ||
|
||
let [signInLink] = mailer.sendSignInMail.firstCall.args; | ||
const [signInLink] = mailer.sendSignInMail.firstCall.args; | ||
const url = new URL(signInLink); | ||
signInLink = url.pathname + url.search; | ||
const email = url.searchParams.get('email'); | ||
const token = url.searchParams.get('token'); | ||
|
||
const signInRes = await request(app.getHttpServer()) | ||
.get(signInLink) | ||
.expect(302); | ||
.post('/api/auth/magic-link') | ||
.send({ email, token }) | ||
.expect(201); | ||
|
||
const session = await getSession(app, signInRes); | ||
t.is(session.user!.id, u1.id); | ||
|
@@ -95,13 +97,15 @@ test('should be able to sign up with email', async t => { | |
t.is(res.body.email, '[email protected]'); | ||
t.true(mailer.sendSignUpMail.calledOnce); | ||
|
||
let [signUpLink] = mailer.sendSignUpMail.firstCall.args; | ||
const [signUpLink] = mailer.sendSignUpMail.firstCall.args; | ||
const url = new URL(signUpLink); | ||
signUpLink = url.pathname + url.search; | ||
const email = url.searchParams.get('email'); | ||
const token = url.searchParams.get('token'); | ||
|
||
const signInRes = await request(app.getHttpServer()) | ||
.get(signUpLink) | ||
.expect(302); | ||
.post('/api/auth/magic-link') | ||
.send({ email, token }) | ||
.expect(201); | ||
|
||
const session = await getSession(app, signInRes); | ||
t.is(session.user!.email, '[email protected]'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { type LoaderFunction, redirect } from 'react-router-dom'; | ||
|
||
export const loader: LoaderFunction = async ({ request }) => { | ||
const url = new URL(request.url); | ||
const queries = url.searchParams; | ||
const email = queries.get('email'); | ||
const token = queries.get('token'); | ||
const redirectUri = queries.get('redirect_uri'); | ||
|
||
if (!email || !token) { | ||
return redirect('/404'); | ||
} | ||
|
||
const res = await fetch('/api/auth/magic-link', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ email, token }), | ||
}); | ||
|
||
if (!res.ok) { | ||
let error: string; | ||
try { | ||
const { message } = await res.json(); | ||
error = message; | ||
} catch (e) { | ||
error = 'failed to verify sign-in token'; | ||
} | ||
return redirect(`/signIn?error=${encodeURIComponent(error)}`); | ||
} | ||
|
||
location.href = redirectUri || '/'; | ||
return null; | ||
}; | ||
|
||
export const Component = () => { | ||
// TODO: loading ui | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters