-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(widget): save jwt to localstorage to bypass 3rd party cookie limi…
…tation
- Loading branch information
Showing
18 changed files
with
155 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import { NextAuth } from '@chirpy-dev/trpc'; | ||
import { getWidgetSession, isWidgetRequest, NextAuth } from '@chirpy-dev/trpc'; | ||
import { getNextAuthOptions } from '@chirpy-dev/trpc/src/auth/auth-options'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export default async function auth(req: NextApiRequest, res: NextApiResponse) { | ||
const isWidget = isWidgetRequest(req); | ||
if ( | ||
isWidget && | ||
req.url?.endsWith('/api/auth/session') && | ||
req.method === 'GET' | ||
) { | ||
// Used by widget/iframe, nextauth can't handle auth via http header | ||
const session = await getWidgetSession(req); | ||
return res.status(200).json(session || {}); | ||
} | ||
|
||
const options = getNextAuthOptions(req); | ||
return await NextAuth(options)(req, res); | ||
} |
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,35 @@ | ||
import { WIDGET_HEADER } from '@chirpy-dev/utils'; | ||
import type { NextApiRequest } from 'next'; | ||
import type { Session } from 'next-auth'; | ||
import { getToken } from 'next-auth/jwt'; | ||
|
||
import { getNextAuthOptions } from './auth/auth-options'; | ||
|
||
export function isWidgetRequest(req: NextApiRequest) { | ||
return req.headers[WIDGET_HEADER.toLowerCase()] === 'true'; | ||
} | ||
|
||
/** | ||
* Used by widget/iframe, nextauth can't handle auth via http header | ||
*/ | ||
export async function getWidgetSession( | ||
req: NextApiRequest, | ||
): Promise<Session | null> { | ||
if (!isWidgetRequest(req)) { | ||
return null; | ||
} | ||
const options = getNextAuthOptions(req); | ||
// getToken supports cookie and http header `authorization` | ||
const token = await getToken({ | ||
req, | ||
}); | ||
if (!token) { | ||
return null; | ||
} | ||
const session = await options.callbacks?.session?.({ | ||
token, | ||
// @ts-expect-error | ||
session: {}, | ||
}); | ||
return session as Session; | ||
} |
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
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
Oops, something went wrong.