@@ -30,8 +30,7 @@ const COOKIE_DOMAIN = process.env.TEST_BASE_URL
3030async function authenticateUser (
3131 email : string ,
3232 password : string ,
33- request : any ,
34- context : BrowserContext
33+ context : BrowserContext ,
3534) : Promise < void > {
3635 if ( ! email || ! password ) {
3736 throw new Error ( "Both email and password are required" ) ;
@@ -63,7 +62,7 @@ async function authenticateUser(
6362
6463 if ( ! workosApiKey || ! workosClientId ) {
6564 throw new Error (
66- "Missing WORKOS_API_KEY or WORKOS_CLIENT_ID environment variables"
65+ "Missing WORKOS_API_KEY or WORKOS_CLIENT_ID environment variables" ,
6766 ) ;
6867 }
6968
@@ -82,71 +81,27 @@ async function authenticateUser(
8281 clientId : workosClientId ,
8382 code : magicAuthToken . code ,
8483 email,
84+ session : {
85+ sealSession : true ,
86+ cookiePassword : process . env . WORKOS_COOKIE_PASSWORD ,
87+ } ,
8588 } ) ;
8689
87- // Step 2: Save session via our test endpoint
88- const baseURL = process . env . TEST_BASE_URL ;
89- const sessionResponse = await request . post (
90- `${ baseURL } /api/test/set-session` ,
91- {
92- data : {
93- user : authResponse . user ,
94- accessToken : authResponse . accessToken ,
95- refreshToken : authResponse . refreshToken ,
96- } ,
97- headers : {
98- "Content-Type" : "application/json" ,
99- } ,
100- }
101- ) ;
90+ const cookie = {
91+ name : COOKIE_NAME ,
92+ value : authResponse . sealedSession || "" ,
93+ domain : COOKIE_DOMAIN ,
94+ path : "/" ,
95+ httpOnly : true ,
96+ secure : false ,
97+ sameSite : "Lax" as const ,
98+ } as const ;
10299
103- if ( ! sessionResponse . ok ( ) ) {
104- const errorText = await sessionResponse . text ( ) ;
105- throw new Error (
106- `Authentication failed: ${ sessionResponse . status ( ) } - ${ errorText } `
107- ) ;
108- }
100+ user . cookies = [ cookie ] ;
109101
110- // Step 3: Extract and cache cookies
111- const responseCookies = sessionResponse . headers ( ) [ "set-cookie" ] ;
112- if ( responseCookies ) {
113- const cookies = [ ] ;
114- const cookieStrings = Array . isArray ( responseCookies )
115- ? responseCookies
116- : [ responseCookies ] ;
117-
118- for ( const cookieString of cookieStrings ) {
119- if ( cookieString && cookieString . includes ( COOKIE_NAME ) ) {
120- const [ nameValue ] = cookieString . split ( ";" ) ;
121- const [ name , value ] = nameValue . split ( "=" ) ;
122-
123- const cookie = {
124- name : name . trim ( ) ,
125- value : value . trim ( ) ,
126- domain : COOKIE_DOMAIN ,
127- path : "/" ,
128- httpOnly : true ,
129- secure : false ,
130- sameSite : "Lax" as const ,
131- } ;
132-
133- cookies . push ( cookie ) ;
134- }
135- }
136-
137- if ( cookies . length > 0 ) {
138- // Cache cookies for user
139- user . cookies = cookies ;
140-
141- // Add cookies to current context
142- await context . addCookies ( cookies ) ;
143- console . log ( `Authenticated and cached user: ${ email } ` ) ;
144- } else {
145- throw new Error ( `No ${ COOKIE_NAME } cookie found in response` ) ;
146- }
147- } else {
148- throw new Error ( "No Set-Cookie header found in response" ) ;
149- }
102+ // Add cookies to current context
103+ await context . addCookies ( user . cookies ) ;
104+ console . log ( `Authenticated and cached user: ${ email } ` ) ;
150105 } catch ( error ) {
151106 console . error ( `Authentication failed for user ${ email } :` , error ) ;
152107 throw error ;
@@ -159,10 +114,10 @@ export const test = base.extend<TestFixtures, WorkerFixtures>({
159114 password : [ undefined , { option : true } ] , // Password for authentication (optional)
160115
161116 // Override the default page fixture to handle authentication
162- page : async ( { page, email, password, request , context } , use ) => {
117+ page : async ( { page, email, password, context } , use ) => {
163118 if ( email && password ) {
164119 // Authenticate the user with email/password before providing the page
165- await authenticateUser ( email , password , request , context ) ;
120+ await authenticateUser ( email , password , context ) ;
166121 }
167122 // If email/password not provided, page remains unauthenticated
168123 await use ( page ) ;
0 commit comments