@decocms/apps 0.27.0 → 0.28.0

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.
@@ -2,10 +2,10 @@
2
2
  * VTEX Authentication Actions
3
3
  *
4
4
  * Ported from deco-cx/apps vtex/actions/authentication/*.ts
5
+ * Cookie forwarding happens automatically via RequestContext.responseHeaders.
5
6
  * @see https://github.com/deco-cx/apps/tree/main/vtex/actions/authentication
6
7
  */
7
8
 
8
- import type { VtexFetchResult } from "../client";
9
9
  import { getVtexConfig, vtexFetchWithCookies } from "../client";
10
10
  import { VTEX_AUTH_COOKIE } from "../utils/vtexId";
11
11
 
@@ -91,19 +91,23 @@ export function extractLoginCookies(response: AuthResponse): LoginCookies | null
91
91
  // Actions
92
92
  // ---------------------------------------------------------------------------
93
93
 
94
- export async function startAuthentication(options?: {
94
+ export interface StartAuthenticationProps {
95
95
  callbackUrl?: string;
96
96
  returnUrl?: string;
97
97
  locale?: string;
98
98
  appStart?: boolean;
99
- }): Promise<VtexFetchResult<StartAuthentication>> {
99
+ }
100
+
101
+ export async function startAuthentication(
102
+ props?: StartAuthenticationProps,
103
+ ): Promise<StartAuthentication> {
100
104
  const config = getVtexConfig();
101
105
  const {
102
106
  callbackUrl = "/",
103
107
  returnUrl = "/",
104
108
  locale = config.locale ?? "pt-BR",
105
109
  appStart = true,
106
- } = options ?? {};
110
+ } = props ?? {};
107
111
 
108
112
  const params = new URLSearchParams({
109
113
  locale,
@@ -118,21 +122,23 @@ export async function startAuthentication(options?: {
118
122
  );
119
123
  }
120
124
 
125
+ export interface ClassicSignInProps {
126
+ email: string;
127
+ password: string;
128
+ authenticationToken?: string;
129
+ }
130
+
121
131
  /**
122
132
  * Classic email + password sign-in.
123
133
  * Calls startAuthentication internally if no authenticationToken provided.
134
+ * Set-Cookie headers from both calls are forwarded via RequestContext.responseHeaders.
124
135
  */
125
- export async function classicSignIn(
126
- email: string,
127
- password: string,
128
- authenticationToken?: string,
129
- ): Promise<VtexFetchResult<AuthResponse>> {
130
- let token = authenticationToken;
131
- let startCookies: string[] = [];
136
+ export async function classicSignIn(props: ClassicSignInProps): Promise<AuthResponse> {
137
+ const { email, password } = props;
138
+ let token = props.authenticationToken;
132
139
  if (!token) {
133
140
  const startResult = await startAuthentication();
134
- token = startResult.data.authenticationToken ?? undefined;
135
- startCookies = startResult.setCookies;
141
+ token = startResult.authenticationToken ?? undefined;
136
142
  if (!token) throw new Error("Failed to obtain authentication token from startAuthentication");
137
143
  }
138
144
 
@@ -141,23 +147,24 @@ export async function classicSignIn(
141
147
  password,
142
148
  authenticationToken: token,
143
149
  });
144
- const result = await vtexFetchWithCookies<AuthResponse>(
145
- "/api/vtexid/pub/authentication/classic/validate",
146
- { method: "POST", body, headers: FORM_HEADERS },
147
- );
148
- result.setCookies = [...startCookies, ...result.setCookies];
149
- return result;
150
+ return vtexFetchWithCookies<AuthResponse>("/api/vtexid/pub/authentication/classic/validate", {
151
+ method: "POST",
152
+ body,
153
+ headers: FORM_HEADERS,
154
+ });
155
+ }
156
+
157
+ export interface AccessKeySignInProps {
158
+ email: string;
159
+ accessKey: string;
160
+ authenticationToken: string;
150
161
  }
151
162
 
152
163
  /**
153
164
  * Passwordless sign-in via email access key.
154
- * Reads VtexSessionToken from cookie if not provided directly.
155
165
  */
156
- export async function accessKeySignIn(
157
- email: string,
158
- accessKey: string,
159
- authenticationToken: string,
160
- ): Promise<VtexFetchResult<AuthResponse>> {
166
+ export async function accessKeySignIn(props: AccessKeySignInProps): Promise<AuthResponse> {
167
+ const { email, accessKey, authenticationToken } = props;
161
168
  const body = new URLSearchParams({
162
169
  login: email,
163
170
  accessKey,
@@ -173,7 +180,6 @@ export async function accessKeySignIn(
173
180
 
174
181
  /**
175
182
  * Logout — returns list of cookie names that must be cleared (Max-Age=0).
176
- * Also calls deleteSession if a sessionId cookie is available.
177
183
  */
178
184
  export function logout(): { cookiesToClear: string[] } {
179
185
  const { account } = getVtexConfig();
@@ -187,30 +193,34 @@ export function logout(): { cookiesToClear: string[] } {
187
193
  };
188
194
  }
189
195
 
196
+ export interface RefreshTokenProps {
197
+ fingerprint?: string;
198
+ }
199
+
190
200
  /**
191
201
  * Refreshes the VTEX auth token using existing session cookies.
202
+ * Cookies are read automatically from RequestContext.
192
203
  */
193
- export async function refreshToken(
194
- cookieHeader: string,
195
- fingerprint?: string,
196
- ): Promise<VtexFetchResult<RefreshTokenResponse>> {
204
+ export async function refreshToken(props?: RefreshTokenProps): Promise<RefreshTokenResponse> {
197
205
  return vtexFetchWithCookies<RefreshTokenResponse>("/api/vtexid/refreshtoken/webstore", {
198
206
  method: "POST",
199
- body: JSON.stringify({ fingerprint }),
200
- headers: { cookie: cookieHeader },
207
+ body: JSON.stringify({ fingerprint: props?.fingerprint }),
201
208
  });
202
209
  }
203
210
 
211
+ export interface RecoveryPasswordProps {
212
+ email: string;
213
+ newPassword: string;
214
+ accessKey: string;
215
+ authenticationToken: string;
216
+ locale?: string;
217
+ }
218
+
204
219
  /**
205
220
  * Sets a new password using an email access key (password-recovery flow).
206
221
  */
207
- export async function recoveryPassword(
208
- email: string,
209
- newPassword: string,
210
- accessKey: string,
211
- authenticationToken: string,
212
- locale?: string,
213
- ): Promise<VtexFetchResult<AuthResponse>> {
222
+ export async function recoveryPassword(props: RecoveryPasswordProps): Promise<AuthResponse> {
223
+ const { email, newPassword, accessKey, authenticationToken, locale } = props;
214
224
  const config = getVtexConfig();
215
225
 
216
226
  const params = new URLSearchParams({
@@ -231,25 +241,27 @@ export async function recoveryPassword(
231
241
  );
232
242
  }
233
243
 
244
+ export interface ResetPasswordProps {
245
+ email: string;
246
+ currentPassword: string;
247
+ newPassword: string;
248
+ authenticationToken?: string;
249
+ locale?: string;
250
+ }
251
+
234
252
  /**
235
253
  * Resets password for an already-authenticated user.
236
254
  * Calls startAuthentication internally if no authenticationToken provided.
255
+ * Set-Cookie headers from both calls are forwarded via RequestContext.responseHeaders.
237
256
  */
238
- export async function resetPassword(
239
- email: string,
240
- currentPassword: string,
241
- newPassword: string,
242
- authenticationToken?: string,
243
- locale?: string,
244
- ): Promise<VtexFetchResult<AuthResponse>> {
257
+ export async function resetPassword(props: ResetPasswordProps): Promise<AuthResponse> {
258
+ const { email, currentPassword, newPassword, locale } = props;
245
259
  const config = getVtexConfig();
246
260
 
247
- let token = authenticationToken;
248
- let startCookies: string[] = [];
261
+ let token = props.authenticationToken;
249
262
  if (!token) {
250
263
  const startResult = await startAuthentication({ locale });
251
- token = startResult.data.authenticationToken ?? undefined;
252
- startCookies = startResult.setCookies;
264
+ token = startResult.authenticationToken ?? undefined;
253
265
  if (!token) throw new Error("Failed to obtain authentication token from startAuthentication");
254
266
  }
255
267
 
@@ -265,37 +277,35 @@ export async function resetPassword(
265
277
  authenticationToken: token,
266
278
  });
267
279
 
268
- const result = await vtexFetchWithCookies<AuthResponse>(
280
+ return vtexFetchWithCookies<AuthResponse>(
269
281
  `/api/vtexid/pub/authentication/classic/setpassword?${params}`,
270
282
  { method: "POST", body, headers: FORM_HEADERS },
271
283
  );
272
- result.setCookies = [...startCookies, ...result.setCookies];
273
- return result;
284
+ }
285
+
286
+ export interface SendEmailVerificationProps {
287
+ email: string;
288
+ authenticationToken?: string;
289
+ locale?: string;
290
+ parentAppId?: string;
274
291
  }
275
292
 
276
293
  /**
277
294
  * Sends an access-key verification email.
278
295
  * Calls startAuthentication internally if no authenticationToken provided.
279
- * Returns { success, authenticationToken, setCookies }.
296
+ * Returns { success, authenticationToken }.
280
297
  */
281
- export async function sendEmailVerification(
282
- email: string,
283
- authenticationToken?: string,
284
- locale?: string,
285
- parentAppId?: string,
286
- ): Promise<{
298
+ export async function sendEmailVerification(props: SendEmailVerificationProps): Promise<{
287
299
  success: boolean;
288
300
  authenticationToken: string | null;
289
- setCookies: string[];
290
301
  }> {
302
+ const { email, locale, parentAppId } = props;
291
303
  try {
292
- let token = authenticationToken;
293
- let startCookies: string[] = [];
304
+ let token = props.authenticationToken;
294
305
 
295
306
  if (!token) {
296
307
  const startResult = await startAuthentication({ locale });
297
- token = startResult.data.authenticationToken ?? undefined;
298
- startCookies = startResult.setCookies;
308
+ token = startResult.authenticationToken ?? undefined;
299
309
  if (!token) throw new Error("Failed to obtain authentication token");
300
310
  }
301
311
 
@@ -308,17 +318,16 @@ export async function sendEmailVerification(
308
318
  { method: "POST", body, headers: FORM_HEADERS },
309
319
  );
310
320
 
311
- if (result.data?.authStatus === "InvalidToken") {
321
+ if (result?.authStatus === "InvalidToken") {
312
322
  throw new Error("Authentication token is invalid");
313
323
  }
314
324
 
315
325
  return {
316
326
  success: true,
317
327
  authenticationToken: token,
318
- setCookies: [...startCookies, ...result.setCookies],
319
328
  };
320
329
  } catch (error) {
321
330
  console.error("[sendEmailVerification]", error);
322
- return { success: false, authenticationToken: null, setCookies: [] };
331
+ return { success: false, authenticationToken: null };
323
332
  }
324
333
  }