@micha.bigler/ui-core-micha 2.1.16 → 2.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -135,12 +135,60 @@ export async function authenticateMfaWithPasskey() {
135
135
  return authenticateWithMFA({ credential: credentialJson });
136
136
  }
137
137
 
138
- export function startSocialLogin(provider) {
138
+ function getCsrfTokenFromCookie() {
139
+ if (typeof document === 'undefined' || !document.cookie) return null;
140
+ const match = document.cookie.match(/(?:^|; )csrftoken=([^;]+)/);
141
+ return match ? decodeURIComponent(match[1]) : null;
142
+ }
143
+
144
+ function submitSocialRedirectForm({ provider, callbackUrl, csrfToken }) {
145
+ const form = document.createElement('form');
146
+ form.method = 'POST';
147
+ form.action = `${HEADLESS_BASE}/auth/provider/redirect`;
148
+ form.style.display = 'none';
149
+
150
+ const fields = {
151
+ provider,
152
+ process: 'login',
153
+ callback_url: callbackUrl,
154
+ csrfmiddlewaretoken: csrfToken,
155
+ };
156
+
157
+ Object.entries(fields).forEach(([name, value]) => {
158
+ const input = document.createElement('input');
159
+ input.type = 'hidden';
160
+ input.name = name;
161
+ input.value = String(value);
162
+ form.appendChild(input);
163
+ });
164
+
165
+ document.body.appendChild(form);
166
+ form.submit();
167
+ }
168
+
169
+ export async function startSocialLogin(provider) {
139
170
  if (typeof window === 'undefined') {
140
171
  throw normaliseApiError(
141
172
  new Error('Auth.SOCIAL_LOGIN_NOT_IN_BROWSER'),
142
173
  'Auth.SOCIAL_LOGIN_NOT_IN_BROWSER'
143
174
  );
144
175
  }
145
- window.location.href = `/accounts/${provider}/login/?process=login`;
146
- }
176
+
177
+ try {
178
+ // Ensures csrftoken cookie exists before form POST.
179
+ await apiClient.get('/api/csrf/');
180
+ } catch {
181
+ // Continue; token might already be present.
182
+ }
183
+
184
+ const csrfToken = getCsrfTokenFromCookie();
185
+ if (!csrfToken) {
186
+ throw normaliseApiError(
187
+ new Error('Auth.SOCIAL_LOGIN_FAILED'),
188
+ 'Auth.SOCIAL_LOGIN_FAILED',
189
+ );
190
+ }
191
+
192
+ const callbackUrl = `${window.location.origin}/login`;
193
+ submitSocialRedirectForm({ provider, callbackUrl, csrfToken });
194
+ }