@@ -2,6 +2,7 @@ import { Adapter, DatabaseSessionAttributes, DatabaseUserAttributes, Lucia, Time
2
2
import { DrizzleSQLiteAdapter } from '@lucia-auth/adapter-drizzle'
3
3
import { SessionTable , UserTable } from '../db/schema'
4
4
import { DB } from '../db/client'
5
+ import type { ApiContextProps } from '../context'
5
6
6
7
/**
7
8
* Lucia's isValidRequestOrigin method will compare the
@@ -18,17 +19,30 @@ export const getAllowedOriginHost = (app_url: string, request?: Request) => {
18
19
return requestHost === appHost ? appHost : undefined
19
20
}
20
21
21
- export const createAuth = ( db : DB , appUrl : string ) => {
22
+ export const isCrossDomain = ( appUrl ?: string , apiUrl ?: string ) => {
23
+ if ( ! appUrl || ! apiUrl ) return true
24
+ const appHost = new URL ( appUrl ) . host
25
+ const apiHost = new URL ( apiUrl ) . host
26
+ return ! apiHost . endsWith ( appHost )
27
+ }
28
+
29
+ export function getCookieOptions ( ctx : ApiContextProps ) {
30
+ return isCrossDomain ( ctx . env . APP_URL , ctx . env . PUBLIC_API_URL )
31
+ ? 'HttpOnly; SameSite=None; Secure;'
32
+ : 'HttpOnly; SameSite=Lax; Secure;'
33
+ }
34
+
35
+ export const createAuth = ( db : DB , appUrl : string , apiUrl : string ) => {
22
36
// @ts -ignore Expect type errors because this is D1 and not SQLite... but it works
23
37
const adapter = new DrizzleSQLiteAdapter ( db , SessionTable , UserTable )
24
38
// cast probably only needed until adapter-drizzle is updated
39
+ // @ts -ignore the "none" option for sameSite works... but https://github.com/lucia-auth/lucia/issues/1320
25
40
return new Lucia ( adapter as Adapter , {
26
- ...getAuthOptions ( appUrl ) ,
41
+ ...getAuthOptions ( appUrl , apiUrl ) ,
27
42
} )
28
43
}
29
44
30
- export const getAuthOptions = ( appUrl : string ) => {
31
- const env = ! appUrl || appUrl . startsWith ( 'http:' ) ? 'DEV' : 'PROD'
45
+ export const getAuthOptions = ( appUrl : string , apiUrl : string ) => {
32
46
return {
33
47
getUserAttributes : ( data : DatabaseUserAttributes ) => {
34
48
return {
@@ -45,8 +59,9 @@ export const getAuthOptions = (appUrl: string) => {
45
59
name : 'auth_session' ,
46
60
expires : false ,
47
61
attributes : {
48
- secure : env === 'PROD' ,
49
- sameSite : 'lax' as const ,
62
+ secure : true ,
63
+ // This might not work forever https://github.com/lucia-auth/lucia/issues/1320
64
+ sameSite : isCrossDomain ( appUrl , apiUrl ) ? ( 'none' as const ) : ( 'lax' as const ) ,
50
65
} ,
51
66
} ,
52
67
0 commit comments