@@ -657,8 +657,8 @@ const createUserFixture = (user: UserWithIncludes, page: Page) => {
657657 eventTypes : user . eventTypes ,
658658 routingForms : user . routingForms ,
659659 self,
660- apiLogin : async ( password ?: string ) =>
661- apiLogin ( { ...( await self ( ) ) , password : password || user . username } , store . page ) ,
660+ apiLogin : async ( navigateToUrl ?: string , password ?: string ) =>
661+ apiLogin ( { ...( await self ( ) ) , password : password || user . username } , store . page , navigateToUrl ) ,
662662 /** Don't forget to close context at the end */
663663 apiLoginOnNewBrowser : async ( browser : Browser , password ?: string ) => {
664664 const newContext = await browser . newContext ( ) ;
@@ -969,25 +969,44 @@ export async function login(
969969
970970export async function apiLogin (
971971 user : Pick < Prisma . User , "username" > & Partial < Pick < Prisma . User , "email" > > & { password : string | null } ,
972- page : Page
972+ page : Page ,
973+ navigateToUrl ?: string
973974) {
975+ // Get CSRF token
974976 const csrfToken = await page
975977 . context ( )
976978 . request . get ( "/api/auth/csrf" )
977979 . then ( ( response ) => response . json ( ) )
978980 . then ( ( json ) => json . csrfToken ) ;
979- const data = {
981+
982+ // Make the login request
983+ const loginData = {
980984 email : user . email ?? `${ user . username } @example.com` ,
981985 password : user . password ?? user . username ,
982986 callbackURL : WEBAPP_URL ,
983987 redirect : "false" ,
984988 json : "true" ,
985989 csrfToken,
986990 } ;
991+
987992 const response = await page . context ( ) . request . post ( "/api/auth/callback/credentials" , {
988- data,
993+ data : loginData ,
989994 } ) ;
995+
990996 expect ( response . status ( ) ) . toBe ( 200 ) ;
997+
998+ /**
999+ * Critical: Navigate to a protected page to trigger NextAuth session loading
1000+ * This forces NextAuth to run the jwt and session callbacks that populate
1001+ * the session with profile, org, and other important data
1002+ * We picked /settings/my-account/profile due to it being one of
1003+ * our lighest protected pages and doesnt do anything other than load the user profile
1004+ */
1005+ await page . goto ( navigateToUrl || "/settings/my-account/profile" ) ;
1006+
1007+ // Wait for the session to be fully established
1008+ await page . waitForLoadState ( ) ;
1009+
9911010 return response ;
9921011}
9931012
0 commit comments