Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-dodos-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/auth-js': patch
---

fix for ssr
11 changes: 7 additions & 4 deletions packages/auth-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare module 'hono' {
}

export type AuthEnv = {
AUTH_URL?: string
AUTH_SECRET: string
AUTH_REDIRECT_PROXY_URL?: string
[key: string]: string | undefined
Expand Down Expand Up @@ -63,8 +64,9 @@ function setEnvDefaults(env: AuthEnv, config: AuthConfig) {

export async function getAuthUser(c: Context): Promise<AuthUser | null> {
const config = c.get('authConfig')
setEnvDefaults(env(c), config)
const origin = env(c)['AUTH_URL'] ? new URL(env(c)['AUTH_URL']).origin : new URL(c.req.url).origin
let ctxEnv = env(c) as AuthEnv
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`, {
headers: { cookie: c.req.header('cookie') ?? '' },
})
Expand Down Expand Up @@ -117,14 +119,15 @@ export function initAuthConfig(cb: ConfigHandler): MiddlewareHandler {
export function authHandler(): MiddlewareHandler {
return async (c) => {
const config = c.get('authConfig')
let ctxEnv = env(c) as AuthEnv

setEnvDefaults(env(c), config)
setEnvDefaults(ctxEnv, config)

if (!config.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)
}
}
11 changes: 2 additions & 9 deletions packages/auth-js/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export { SessionProviderProps }
class AuthConfigManager {
private static instance: AuthConfigManager | null = null
_config: AuthClientConfig = {
baseUrl: parseUrl(window.location.origin).origin,
basePath: parseUrl(window.location.origin).path,
baseUrl: typeof window !== 'undefined' ? parseUrl(window.location.origin).origin : '',
basePath: typeof window !== 'undefined' ? parseUrl(window.location.origin).path : '/api/auth',
credentials: 'same-origin',
_lastSync: 0,
_session: undefined,
Expand Down Expand Up @@ -148,13 +148,6 @@ export async function getSession(params?: GetSessionParams) {
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() {
const response = await fetchData<{ csrfToken: string }>(
'csrf',
Expand Down