-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clients/article: pre-authenticated links in emails
- Loading branch information
Showing
7 changed files
with
97 additions
and
7 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
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,61 @@ | ||
import { getServerSideAPI } from '@/utils/api' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
// exchange the magic link with the server | ||
const handleMagicLinkToken = async (magic: string): Promise<string[]> => { | ||
const api = getServerSideAPI() | ||
|
||
try { | ||
const user = await api.users.getAuthenticated() | ||
if (user.id) { | ||
// user is already logged in | ||
return [] | ||
} | ||
} catch {} | ||
|
||
try { | ||
const magicLink = await api.magicLink.authenticateMagicLinkRaw({ | ||
token: magic, | ||
}) | ||
const val = await magicLink.value() | ||
if (val.success) { | ||
return magicLink.raw.headers.getSetCookie() | ||
} | ||
} catch {} | ||
|
||
return [] | ||
} | ||
|
||
export async function middleware(request: NextRequest) { | ||
if (request.nextUrl.pathname.startsWith('/_next/')) { | ||
return | ||
} | ||
|
||
// Intercept all requests with magic_link_token set. | ||
// Try to authenticate with this token, and redirect to the URL with the token stripped. | ||
if (request.nextUrl.searchParams.has('magic_link_token')) { | ||
const magic = request.nextUrl.searchParams.get('magic_link_token') | ||
|
||
let setCookies: string[] = [] | ||
|
||
if (magic) { | ||
setCookies = await handleMagicLinkToken(magic) | ||
} | ||
|
||
const target = new URL(request.nextUrl) | ||
|
||
// remove magic_link_token from URL even if it was invalid | ||
target.searchParams.delete('magic_link_token') | ||
|
||
// redirect | ||
const response = NextResponse.redirect(target) | ||
|
||
// forward set-cookie header(s) to client | ||
for (const setCookie of setCookies) { | ||
response.headers.set('Set-Cookie', setCookie) | ||
} | ||
return response | ||
} | ||
|
||
return | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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