-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauth-setup.ts
More file actions
52 lines (47 loc) · 1.44 KB
/
auth-setup.ts
File metadata and controls
52 lines (47 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) Gridiron Survivor.
// Licensed under the MIT License.
import { createClient } from '@supabase/supabase-js';
import { Page } from '@playwright/test';
/**
* Get a Supabase client with authentication set up for Playwright tests
* @param page - The Playwright page object
* @param userEmail - The email of the user to sign in as
* @returns Promise that resolves when authentication is set up
*/
async function getSupabaseClient(page: Page, userEmail: string): Promise<void> {
const supabaseClient = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
);
const { data, error } = await supabaseClient.auth.signInWithPassword({
email: userEmail,
password: 'password',
});
if (!data.user) {
throw Error(error?.message);
}
const projectRef = process.env
.NEXT_PUBLIC_SUPABASE_URL!.split('//')[1]
.split('.')[0];
await page.context().addCookies([
{
name: `sb-${projectRef}-auth-token`,
value: JSON.stringify({
access_token: data.session.access_token,
refresh_token: data.session.refresh_token,
expires_in: 3600,
expires_at: Math.floor(Date.now() / 1000) + 3600,
token_type: 'bearer',
user: {
id: data.user.id,
email: data.user.email,
},
}),
domain: 'localhost',
path: '/',
httpOnly: false,
secure: false,
},
]);
}
export { getSupabaseClient };