|
1 | 1 | import { ApolloClient, InMemoryCache } from '@apollo/client';
|
2 |
| -import { LOGIN_USER, REFRESH_AUTH_TOKEN } from './gql/GQL_MUTATIONS'; |
3 |
| - |
4 |
| -let tokenSetter: ReturnType<typeof setInterval>; |
| 2 | +import { LOGIN_USER } from './gql/GQL_MUTATIONS'; |
5 | 3 |
|
| 4 | +// Cookie-based authentication - no token storage needed |
6 | 5 | export function hasCredentials() {
|
7 | 6 | if (typeof window === 'undefined') {
|
8 | 7 | return false; // Server-side, no credentials available
|
9 | 8 | }
|
10 |
| - |
11 |
| - const authToken = sessionStorage.getItem( |
12 |
| - process.env.NEXT_PUBLIC_AUTH_TOKEN_SS_KEY as string |
13 |
| - ); |
14 |
| - const refreshToken = localStorage.getItem( |
15 |
| - process.env.NEXT_PUBLIC_REFRESH_TOKEN_LS_KEY as string |
16 |
| - ); |
17 |
| - |
18 |
| - if (!!authToken && !!refreshToken) { |
19 |
| - return true; |
20 |
| - } |
21 |
| - |
| 9 | + |
| 10 | + // With cookie-based auth, we'll check if user is logged in through a query |
| 11 | + // For now, we'll return false and let components handle the check |
22 | 12 | return false;
|
23 | 13 | }
|
24 | 14 |
|
25 | 15 | export async function getAuthToken() {
|
26 |
| - let authToken = sessionStorage.getItem( |
27 |
| - process.env.NEXT_PUBLIC_AUTH_TOKEN_SS_KEY as string, |
28 |
| - ); |
29 |
| - if (!authToken || !tokenSetter) { |
30 |
| - authToken = await fetchAuthToken(); |
31 |
| - } |
32 |
| - return authToken; |
33 |
| -} |
34 |
| - |
35 |
| -async function fetchAuthToken() { |
36 |
| - const refreshToken = localStorage.getItem( |
37 |
| - process.env.NEXT_PUBLIC_REFRESH_TOKEN_LS_KEY as string, |
38 |
| - ); |
39 |
| - if (!refreshToken) { |
40 |
| - // No refresh token means the user is not authenticated. |
41 |
| - return; |
42 |
| - } |
43 |
| - |
44 |
| - try { |
45 |
| - const client = new ApolloClient({ |
46 |
| - uri: process.env.NEXT_PUBLIC_GRAPHQL_URL, |
47 |
| - cache: new InMemoryCache(), |
48 |
| - }); |
49 |
| - |
50 |
| - const { data } = await client.mutate({ |
51 |
| - mutation: REFRESH_AUTH_TOKEN, |
52 |
| - variables: { refreshToken }, |
53 |
| - }); |
54 |
| - |
55 |
| - const authToken = data?.refreshJwtAuthToken?.authToken; |
56 |
| - if (!authToken) { |
57 |
| - throw new Error('Failed to retrieve a new auth token'); |
58 |
| - } |
59 |
| - // Save token. |
60 |
| - sessionStorage.setItem( |
61 |
| - process.env.NEXT_PUBLIC_AUTH_TOKEN_SS_KEY as string, |
62 |
| - authToken, |
63 |
| - ); |
64 |
| - if (tokenSetter) { |
65 |
| - clearInterval(tokenSetter); |
66 |
| - } |
67 |
| - tokenSetter = setInterval( |
68 |
| - async () => { |
69 |
| - if (!hasCredentials()) { |
70 |
| - clearInterval(tokenSetter); |
71 |
| - return; |
72 |
| - } |
73 |
| - fetchAuthToken(); |
74 |
| - }, |
75 |
| - Number(process.env.NEXT_PUBLIC_AUTH_KEY_TIMEOUT || 300000), |
76 |
| - ); |
77 |
| - |
78 |
| - return authToken; |
79 |
| - } catch (err) { |
80 |
| - console.error(err); |
81 |
| - } |
82 |
| -} |
83 |
| - |
84 |
| -function saveCredentials( |
85 |
| - authToken: string, |
86 |
| - sessionToken: string, |
87 |
| - refreshToken: string | null = null, |
88 |
| -) { |
89 |
| - sessionStorage.setItem( |
90 |
| - process.env.NEXT_PUBLIC_AUTH_TOKEN_SS_KEY as string, |
91 |
| - authToken, |
92 |
| - ); |
93 |
| - sessionStorage.setItem( |
94 |
| - process.env.NEXT_PUBLIC_SESSION_TOKEN_LS_KEY as string, |
95 |
| - sessionToken, |
96 |
| - ); |
97 |
| - if (refreshToken) { |
98 |
| - localStorage.setItem( |
99 |
| - process.env.NEXT_PUBLIC_REFRESH_TOKEN_LS_KEY as string, |
100 |
| - refreshToken, |
101 |
| - ); |
102 |
| - } |
| 16 | + // Cookie-based auth doesn't need JWT tokens |
| 17 | + return null; |
103 | 18 | }
|
104 | 19 |
|
105 | 20 | export async function login(username: string, password: string) {
|
106 |
| - const headers: { [key: string]: string } = {}; |
107 |
| - const sessionToken = sessionStorage.getItem( |
108 |
| - process.env.NEXT_PUBLIC_SESSION_TOKEN_LS_KEY as string, |
109 |
| - ); |
110 |
| - if (sessionToken) { |
111 |
| - headers['woocommerce-session'] = `Session ${sessionToken}`; |
112 |
| - } |
113 | 21 | try {
|
114 | 22 | const client = new ApolloClient({
|
115 | 23 | uri: process.env.NEXT_PUBLIC_GRAPHQL_URL,
|
116 | 24 | cache: new InMemoryCache(),
|
117 |
| - headers, |
| 25 | + credentials: 'include', // Include cookies in requests |
118 | 26 | });
|
119 | 27 |
|
120 | 28 | const { data } = await client.mutate({
|
121 | 29 | mutation: LOGIN_USER,
|
122 | 30 | variables: { username, password },
|
123 | 31 | });
|
124 | 32 |
|
125 |
| - const { authToken, refreshToken, customer } = data.login; |
| 33 | + const loginResult = data.loginWithCookies; |
126 | 34 |
|
127 |
| - if (!authToken || !refreshToken || !customer.sessionToken) { |
128 |
| - throw new Error('Failed to retrieve credentials.'); |
| 35 | + if (loginResult.status !== 'SUCCESS') { |
| 36 | + throw new Error('Login failed'); |
129 | 37 | }
|
130 |
| - saveCredentials(authToken, customer.sessionToken, refreshToken); |
131 |
| - return customer; |
| 38 | + |
| 39 | + // On successful login, cookies are automatically set by the server |
| 40 | + return { success: true, status: loginResult.status }; |
132 | 41 | } catch (error: unknown) {
|
133 | 42 | if (error instanceof Error) {
|
134 | 43 | throw new Error(error.message);
|
135 | 44 | }
|
136 | 45 | throw new Error('An unknown error occurred during login.');
|
137 | 46 | }
|
138 | 47 | }
|
| 48 | + |
| 49 | +export async function logout() { |
| 50 | + // For cookie-based auth, we might need a logout mutation |
| 51 | + // For now, we can clear any client-side state |
| 52 | + if (typeof window !== 'undefined') { |
| 53 | + // Redirect to login or home page after logout |
| 54 | + window.location.href = '/'; |
| 55 | + } |
| 56 | +} |
0 commit comments