middleware fix wip
This commit is contained in:
@@ -27,34 +27,50 @@ export class WebsiteAuthManager {
|
||||
const role = (typeof requestOrRole === 'string' ? requestOrRole : maybeRole) as AuthRole;
|
||||
const request = typeof requestOrRole === 'string' ? null : requestOrRole;
|
||||
|
||||
const context = await browser.newContext({ baseURL });
|
||||
|
||||
if (request) {
|
||||
const token = await WebsiteAuthManager.loginViaApi(request, apiBaseUrl, role);
|
||||
|
||||
// Critical: the website must receive `gp_session` so middleware can forward it.
|
||||
// Playwright runs in its own container and accesses website via PLAYWRIGHT_BASE_URL
|
||||
// The cookie domain must match the hostname in the URL that Playwright uses
|
||||
const url = new URL(baseURL);
|
||||
const domain = url.hostname; // "website" in Docker, "localhost" locally
|
||||
// If using API login, create context with cookies pre-set
|
||||
if (typeof requestOrRole !== 'string') {
|
||||
const token = await WebsiteAuthManager.loginViaApi(requestOrRole, apiBaseUrl, role);
|
||||
|
||||
await context.addCookies([
|
||||
{
|
||||
name: 'gp_session',
|
||||
value: token,
|
||||
domain: domain,
|
||||
path: '/',
|
||||
httpOnly: true,
|
||||
sameSite: 'Lax',
|
||||
console.log(`[WebsiteAuthManager] Creating context with pre-set cookie, baseURL: ${baseURL}, token length: ${token.length}`);
|
||||
|
||||
// Create context with storage state that includes the cookie
|
||||
// This is more reliable than adding cookies after context creation
|
||||
const contextWithCookies = await browser.newContext({
|
||||
baseURL,
|
||||
storageState: {
|
||||
cookies: [
|
||||
{
|
||||
name: 'gp_session',
|
||||
value: token,
|
||||
domain: new URL(baseURL).hostname,
|
||||
path: '/',
|
||||
expires: -1,
|
||||
httpOnly: true,
|
||||
secure: false,
|
||||
sameSite: 'Lax',
|
||||
},
|
||||
],
|
||||
origins: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
const page = await contextWithCookies.newPage();
|
||||
|
||||
// Verify cookies
|
||||
const cookies = await contextWithCookies.cookies();
|
||||
console.log(`[WebsiteAuthManager] Cookies in context:`, cookies);
|
||||
|
||||
return {
|
||||
context: contextWithCookies,
|
||||
page,
|
||||
role,
|
||||
};
|
||||
}
|
||||
|
||||
// UI login path
|
||||
const context = await browser.newContext({ baseURL });
|
||||
const page = await context.newPage();
|
||||
|
||||
if (!request) {
|
||||
await WebsiteAuthManager.loginViaUi(page, role);
|
||||
}
|
||||
await WebsiteAuthManager.loginViaUi(page, role);
|
||||
|
||||
return {
|
||||
context,
|
||||
|
||||
Reference in New Issue
Block a user