Skip to content

Commit

Permalink
bump @auth/core
Browse files Browse the repository at this point in the history
  • Loading branch information
OrJDev committed Sep 17, 2024
1 parent fb01875 commit fd8def1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 71 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-walls-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solid-mediakit/auth': patch
---

bump @auth/core
7 changes: 7 additions & 0 deletions examples/auth/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface ImportMetaEnv {
readonly VITE_AUTH_PATH: string
}

interface ImportMeta {
readonly env: ImportMetaEnv
}
2 changes: 1 addition & 1 deletion examples/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {
"zod": "^3.23.8",
"@solid-mediakit/auth": "workspace:*",
"@auth/core": "^0.34.2",
"@auth/core": "^0.35.0",
"@solidjs/router": "^0.14.1",
"@solidjs/start": "^1.0.6",
"solid-js": "^1.8.19",
Expand Down
1 change: 1 addition & 0 deletions examples/auth/src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export const authOptions: SolidAuthConfig = {
}),
],
debug: false,
basePath: import.meta.env.VITE_AUTH_PATH,
}
4 changes: 2 additions & 2 deletions packages/auth/solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"devDependencies": {
"@auth/core": "^0.34.1",
"@auth/core": "^0.35.0",
"rollup": "^4.18.0",
"rollup-preset-solid": "^2.0.1",
"@types/node": "^20.14.9",
Expand All @@ -63,7 +63,7 @@
"solid-js": "^1.8.15",
"vinxi": "^0.3.10",
"@solidjs/meta": "^0.29.3",
"@auth/core": "^0.29.0 || ~0.29.0"
"@auth/core": "^0.35.0"
},
"engines": {
"node": ">=16"
Expand Down
49 changes: 18 additions & 31 deletions packages/auth/solid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type { SolidAuthConfig }
const authorizationParamsPrefix = 'authorizationParams-'

export function SolidAuth(
config: SolidAuthConfig | ((event: APIEvent) => PromiseLike<SolidAuthConfig>)
config: SolidAuthConfig | ((event: APIEvent) => PromiseLike<SolidAuthConfig>),
): {
signIn: APIHandler
signOut: APIHandler
Expand All @@ -26,7 +26,7 @@ export function SolidAuth(
const _config = typeof config === 'object' ? config : await config(event)

setEnvDefaults(process.env, _config)

_config.basePath ??= getBasePath(_config)
const { request } = event
const url = new URL(request.url)
const action = url.pathname
Expand All @@ -37,7 +37,7 @@ export function SolidAuth(
isAuthAction(action) &&
url.pathname.startsWith(_config.basePath + '/')
) {
return await Auth(request, _config)
return Auth(request, _config)
}

return new Response('Not Found', { status: 404 })
Expand Down Expand Up @@ -65,7 +65,7 @@ export function SolidAuth(
_options,
authorizationParams,
_config,
event
event,
)
},
signOut: async (event: APIEvent) => {
Expand Down Expand Up @@ -95,7 +95,7 @@ export async function server_signIn(
options: SignInParams[1] = {},
authorizationParams: SignInParams[2],
config: SolidAuthConfig,
event: APIEvent
event: APIEvent,
) {
const { request } = event
const { protocol } = new URL(request.url)
Expand All @@ -107,13 +107,7 @@ export async function server_signIn(
} = options instanceof FormData ? Object.fromEntries(options) : options

const callbackUrl = redirectTo?.toString() ?? headers.get('Referer') ?? '/'
const base = createActionURL(
'signin',
protocol,
headers,
process.env,
config.basePath
)
const base = createActionURL('signin', protocol, headers, process.env, config)

if (!provider) {
const url = `${base}?${new URLSearchParams({ callbackUrl })}`
Expand Down Expand Up @@ -155,7 +149,7 @@ export async function server_signIn(
c.options?.httpOnly ? 'HttpOnly;' : ''
} ${c.options?.secure ? 'Secure;' : ''} ${
c.options?.sameSite ? `SameSite=${c.options.sameSite};` : ''
}`
}`,
)
}

Expand All @@ -171,20 +165,14 @@ type SignOutParams = Parameters<App.Locals['signOut']>
export async function server_signOut(
options: SignOutParams[0],
config: SolidAuthConfig,
event: APIEvent
event: APIEvent,
) {
const { request } = event
const { protocol } = new URL(request.url)
const headers = new Headers(request.headers)
headers.set('Content-Type', 'application/x-www-form-urlencoded')

const url = createActionURL(
'signout',
protocol,
headers,
process.env,
config.basePath
)
const url = createActionURL('signout', protocol, headers, process.env, config)
const callbackUrl = options?.redirectTo ?? headers.get('Referer') ?? '/'
const body = new URLSearchParams({ callbackUrl })
const req = new Request(url, { method: 'POST', headers, body })
Expand All @@ -199,7 +187,7 @@ export async function server_signOut(
c.options?.httpOnly ? 'HttpOnly;' : ''
} ${c.options?.secure ? 'Secure;' : ''} ${
c.options?.sameSite ? `SameSite=${c.options.sameSite};` : ''
}`
}`,
)

if (options?.redirect ?? true)
Expand All @@ -211,7 +199,7 @@ export async function server_signOut(

export async function auth(
event: APIEvent,
config: SolidAuthConfig
config: SolidAuthConfig,
): ReturnType<App.Locals['auth']> {
setEnvDefaults(process.env, config)
config.trustHost ??= true
Expand All @@ -223,7 +211,7 @@ export async function auth(
protocol,
req.headers,
process.env,
config.basePath
config,
)
const request = new Request(sessionUrl, {
headers: { cookie: req.headers.get('cookie') ?? '' },
Expand All @@ -236,7 +224,7 @@ export async function auth(
'set-cookie',
`${name}=${value}; Path=/; ${options.httpOnly ? 'HttpOnly;' : ''} ${
options.secure ? 'Secure;' : ''
} ${options.sameSite ? `SameSite=${options.sameSite};` : ''}`
} ${options.sameSite ? `SameSite=${options.sameSite};` : ''}`,
)
}

Expand All @@ -252,25 +240,24 @@ export type GetSessionResult = Promise<Session | null>

export async function getSession(
_req: Request | RequestEvent,
options: SolidAuthConfig
options: SolidAuthConfig,
): GetSessionResult {
const req = 'request' in _req ? _req.request : _req
setEnvDefaults(process.env, options)

const { protocol } = new URL(req.url)

options.basePath = getBasePath()
const url = createActionURL(
'session',
protocol,
new Headers(req.headers),
process.env,
options.basePath
options,
)

const response = await Auth(
new Request(url, { headers: req.headers }),
options
options,
)

const { status = 200 } = response
Expand All @@ -284,11 +271,11 @@ export async function getSession(

export function protected$(
page: (session$: Session) => JSXElement,
fallBack: () => JSXElement
fallBack: () => JSXElement,
): VoidComponent
export function protected$(
page: (session$: Session) => JSXElement,
redirectTo?: string
redirectTo?: string,
): VoidComponent
export function protected$(...args: any[]): VoidComponent {
throw new Error('Should be compiled by the auth plugin.')
Expand Down
13 changes: 7 additions & 6 deletions packages/auth/solid/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ export function objectIsSession(obj: any): obj is Session {
}

export const getEnv = (env: string) => {
if (env.startsWith('VITE_')) {
return (import.meta as any).env[env]
}
if (
typeof process !== 'undefined' &&
typeof process.env !== 'undefined' &&
!env.startsWith('VITE_')
) {
return process.env[env]
}
if (env.startsWith('VITE_')) {
return (import.meta as any).env[env]
}
return undefined
}

Expand All @@ -69,14 +69,15 @@ export const conditionalEnv = (...envs: string[]) => {

export function setEnvDefaults(
envObject: Record<string, string | undefined>,
config: SolidAuthConfig
config: SolidAuthConfig,
) {
coreSetEnvDefaults(envObject, config)
config.trustHost ??= isDev
config.basePath = getBasePath()
config.basePath ??= getBasePath(config)
}

export const getBasePath = (): string => {
export const getBasePath = (config?: SolidAuthConfig): string => {
if (config?.basePath) return config.basePath
const ev = conditionalEnv('VITE_AUTH_PATH')
return ev ?? `/api/auth`
}
38 changes: 7 additions & 31 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd8def1

Please sign in to comment.