fix(auth-js): react package work with ssr (#494)

pull/498/head
divyam234 2024-05-04 08:12:10 +05:30 committed by GitHub
parent 7ab462dbc4
commit 300ef2f8bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 13 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/auth-js': patch
---
fix for ssr

View File

@ -15,6 +15,7 @@ declare module 'hono' {
} }
export type AuthEnv = { export type AuthEnv = {
AUTH_URL?: string
AUTH_SECRET: string AUTH_SECRET: string
AUTH_REDIRECT_PROXY_URL?: string AUTH_REDIRECT_PROXY_URL?: string
[key: string]: string | undefined [key: string]: string | undefined
@ -63,8 +64,9 @@ function setEnvDefaults(env: AuthEnv, config: AuthConfig) {
export async function getAuthUser(c: Context): Promise<AuthUser | null> { export async function getAuthUser(c: Context): Promise<AuthUser | null> {
const config = c.get('authConfig') const config = c.get('authConfig')
setEnvDefaults(env(c), config) let ctxEnv = env(c) as AuthEnv
const origin = env(c)['AUTH_URL'] ? new URL(env(c)['AUTH_URL']).origin : new URL(c.req.url).origin setEnvDefaults(ctxEnv, config)
const origin = ctxEnv.AUTH_URL ? new URL(ctxEnv.AUTH_URL).origin : new URL(c.req.url).origin
const request = new Request(`${origin}${config.basePath}/session`, { const request = new Request(`${origin}${config.basePath}/session`, {
headers: { cookie: c.req.header('cookie') ?? '' }, headers: { cookie: c.req.header('cookie') ?? '' },
}) })
@ -117,14 +119,15 @@ export function initAuthConfig(cb: ConfigHandler): MiddlewareHandler {
export function authHandler(): MiddlewareHandler { export function authHandler(): MiddlewareHandler {
return async (c) => { return async (c) => {
const config = c.get('authConfig') const config = c.get('authConfig')
let ctxEnv = env(c) as AuthEnv
setEnvDefaults(env(c), config) setEnvDefaults(ctxEnv, config)
if (!config.secret) { if (!config.secret) {
throw new HTTPException(500, { message: 'Missing AUTH_SECRET' }) throw new HTTPException(500, { message: 'Missing AUTH_SECRET' })
} }
const res = await Auth(reqWithEnvUrl(c.req.raw, env(c)['AUTH_URL']), config) const res = await Auth(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL), config)
return new Response(res.body, res) return new Response(res.body, res)
} }
} }

View File

@ -31,8 +31,8 @@ export { SessionProviderProps }
class AuthConfigManager { class AuthConfigManager {
private static instance: AuthConfigManager | null = null private static instance: AuthConfigManager | null = null
_config: AuthClientConfig = { _config: AuthClientConfig = {
baseUrl: parseUrl(window.location.origin).origin, baseUrl: typeof window !== 'undefined' ? parseUrl(window.location.origin).origin : '',
basePath: parseUrl(window.location.origin).path, basePath: typeof window !== 'undefined' ? parseUrl(window.location.origin).path : '/api/auth',
credentials: 'same-origin', credentials: 'same-origin',
_lastSync: 0, _lastSync: 0,
_session: undefined, _session: undefined,
@ -148,13 +148,6 @@ export async function getSession(params?: GetSessionParams) {
return session return session
} }
/**
* Returns the current Cross-Site Request Forgery Token (CSRF Token)
* required to make requests that changes state. (e.g. signing in or out, or updating the session).
*
* [CSRF Prevention: Double Submit Cookie](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#double-submit-cookie)
* @internal
*/
export async function getCsrfToken() { export async function getCsrfToken() {
const response = await fetchData<{ csrfToken: string }>( const response = await fetchData<{ csrfToken: string }>(
'csrf', 'csrf',