@wix/astro 0.2.17 → 0.3.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.
@@ -1,5 +1,5 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
- import { AuthenticationStrategy } from "@wix/sdk";
2
+ import type { AuthenticationStrategy } from "@wix/sdk";
3
3
  export declare const authStrategyAsyncLocalStorage: AsyncLocalStorage<{
4
4
  auth: AuthenticationStrategy<void>;
5
5
  }>;
@@ -31,9 +31,12 @@ export interface TokenResponse {
31
31
  scope?: string | null;
32
32
  }
33
33
  export declare const getMemberTokensForDirectLogin: (sessionToken: string) => Promise<{
34
- accessToken: AccessToken;
35
- refreshToken: {
36
- value: string;
37
- role: TokenRole;
34
+ clientId: string;
35
+ tokens: {
36
+ accessToken: AccessToken;
37
+ refreshToken: {
38
+ value: string;
39
+ role: TokenRole;
40
+ };
38
41
  };
39
42
  }>;
@@ -92,11 +92,14 @@ const getMemberTokens = async (code, state, oauthData) => {
92
92
  codeVerifier: oauthData.codeVerifier,
93
93
  });
94
94
  return {
95
- accessToken: createAccessToken(tokensResponse.access_token, tokensResponse.expires_in),
96
- refreshToken: {
97
- value: tokensResponse.refresh_token,
98
- role: TokenRole.MEMBER,
99
- },
95
+ clientId: wixContext['clientId'],
96
+ tokens: {
97
+ accessToken: createAccessToken(tokensResponse.access_token, tokensResponse.expires_in),
98
+ refreshToken: {
99
+ value: tokensResponse.refresh_token,
100
+ role: TokenRole.MEMBER,
101
+ },
102
+ }
100
103
  };
101
104
  };
102
105
  export const getMemberTokensForDirectLogin = async (sessionToken) => {
@@ -283,6 +283,8 @@
283
283
  import { authentication, verification, recovery } from '@wix/identity';
284
284
  import { getMemberTokensForDirectLogin } from './login-helpers/login-helpers.js';
285
285
  import { wixContext } from '@wix/sdk-context';
286
+ import type { WixClient } from '@wix/sdk';
287
+ import type { SiteSessionAuth } from "@wix/sdk/auth/site-session";
286
288
 
287
289
  class LoginForm extends HTMLElement {
288
290
  constructor() {
@@ -478,8 +480,11 @@
478
480
  response.sessionToken!
479
481
  );
480
482
 
483
+ // Update context
484
+ (wixContext['client'] as WixClient<undefined, ReturnType<typeof SiteSessionAuth>>).auth.setTokens(tokens.tokens);
485
+
481
486
  // Set cookie
482
- this.setCookie('wixSession', JSON.stringify(tokens.refreshToken), 2);
487
+ this.setCookie('wixSession', JSON.stringify(tokens), 2);
483
488
 
484
489
  // Show success animation
485
490
  this.showSuccessMessage();
@@ -544,14 +549,15 @@
544
549
  }
545
550
 
546
551
  setCookie(name, value, days) {
547
- // Using a simplified cookie setter for demonstration
548
552
  let expires = '';
549
553
  if (days) {
550
554
  const date = new Date();
551
555
  date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
552
556
  expires = "; expires=" + date.toUTCString();
553
557
  }
554
- document.cookie = name + "=" + (value || "") + expires + "; path=/";
558
+ const encodedName = encodeURIComponent(name);
559
+ const encodedValue = encodeURIComponent(value || '');
560
+ document.cookie = `${encodedName}=${encodedValue}${expires}; path=/`;
555
561
  }
556
562
 
557
563
  onCaptchaChange(token) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -2,7 +2,21 @@ import type { AstroIntegration } from "astro";
2
2
  import { wixBlogLoader } from "./loaders/blog.js";
3
3
  export { wixBlogLoader };
4
4
  export type { Runtime } from "./entrypoints/server.js";
5
- export declare function createIntegration(opts?: {
5
+ export interface WixAstroIntegrationOptions {
6
+ /**
7
+ * Name of the cookie used for the Wix session
8
+ * @default "wixSession"
9
+ */
6
10
  sessionCookieName?: string;
11
+ /**
12
+ * Whether to pre-warm the redirect session by injecting an iframe to cookie.wix.com
13
+ * @default false
14
+ */
15
+ preWarmRedirectSession?: boolean;
16
+ /***
17
+ * Wether to inect routes for Wix Auth
18
+ * @default true
19
+ */
7
20
  useWixAuth?: boolean;
8
- }): AstroIntegration;
21
+ }
22
+ export declare function createIntegration(opts?: WixAstroIntegrationOptions): AstroIntegration;
@@ -14,14 +14,16 @@ export { wixBlogLoader };
14
14
  export function createIntegration(opts = {
15
15
  sessionCookieName: "wixSession",
16
16
  useWixAuth: true,
17
+ preWarmRedirectSession: false,
17
18
  }) {
18
19
  const sessionCookieName = opts.sessionCookieName ?? "wixSession";
20
+ const preWarmRedirectSession = opts.preWarmRedirectSession ?? false;
19
21
  let _config;
20
22
  let _buildOutput;
21
23
  return {
22
24
  name: "@wix/astro",
23
25
  hooks: {
24
- "astro:config:setup": async ({ config, updateConfig, addMiddleware, injectRoute, logger, }) => {
26
+ "astro:config:setup": async ({ config, updateConfig, addMiddleware, injectRoute, injectScript, logger, }) => {
25
27
  const aRequire = buildResolver(fileURLToPath(import.meta.url), {
26
28
  resolveToAbsolute: true,
27
29
  });
@@ -67,6 +69,9 @@ export function createIntegration(opts = {
67
69
  `);
68
70
  throw new Error(`${chalk.magenta(`WIX_CLIENT_ID`)} not found in loaded environment variables`);
69
71
  }
72
+ if (preWarmRedirectSession) {
73
+ injectScript('page', `import { getAuth } from '@wix/astro/runtime/client'; getAuth().syncToWixPages();`);
74
+ }
70
75
  updateConfig({
71
76
  env: {
72
77
  schema: {
@@ -1,5 +1,5 @@
1
1
  import pRetry from "p-retry";
2
- import { getAuth } from "../../runtime.js";
2
+ import { getAuth } from "../../runtime.server.js";
3
3
  import { OAUTH_COOKIE_STATE, WIX_LOGIN_REDIRECT } from "./constants.js";
4
4
  import { sessionCookieJson } from "./runtime.js";
5
5
  export const prerender = false;
@@ -1,5 +1,5 @@
1
1
  import { AUTH_CALLBACK_PATHNAME, AUTH_LOGIN_CALLBACK_PARAM, OAUTH_COOKIE_STATE, PROMPT_QUERY_PARAM, } from "./constants.js";
2
- import { getAuth } from "../../runtime.js";
2
+ import { getAuth } from "../../runtime.server.js";
3
3
  export const prerender = false;
4
4
  export async function GET({ url }) {
5
5
  // Extract search parameters from the URL
@@ -1,4 +1,4 @@
1
- import { getAuth } from "../../runtime.js";
1
+ import { getAuth } from "../../runtime.server.js";
2
2
  import { sessionCookieJson } from "./runtime.js";
3
3
  export const prerender = false;
4
4
  export const GET = async ({ request, redirect, cookies }) => {
@@ -1,4 +1,4 @@
1
- import { getAuth } from "../../runtime.js";
1
+ import { getAuth } from "../../runtime.server.js";
2
2
  export const prerender = false;
3
3
  export async function GET({ request, redirect }) {
4
4
  const returnTo = request.headers.get("Referer") ?? "/";
@@ -0,0 +1,3 @@
1
+ import type { WixClient } from "@wix/sdk";
2
+ import type { SiteSessionAuth } from "@wix/sdk/auth/site-session";
3
+ export declare function getWixClient(): WixClient<undefined, ReturnType<typeof SiteSessionAuth>>;
@@ -0,0 +1,4 @@
1
+ import { wixContext } from '@wix/sdk-context';
2
+ export function getWixClient() {
3
+ return wixContext['client'];
4
+ }
@@ -0,0 +1,2 @@
1
+ import type { SiteSessionAuth } from "@wix/sdk/auth/site-session";
2
+ export declare function getAuth(): ReturnType<typeof SiteSessionAuth>;
@@ -0,0 +1,4 @@
1
+ import { wixContext } from '@wix/sdk-context';
2
+ export function getAuth() {
3
+ return wixContext['client'].auth;
4
+ }
package/dist/runtime.d.ts CHANGED
@@ -1,2 +1,5 @@
1
1
  import { OAuthStrategy } from "@wix/sdk/auth/oauth2";
2
+ import type { WixClient } from "@wix/sdk";
3
+ import type { SiteSessionAuth } from "@wix/sdk/auth/site-session";
2
4
  export declare function getAuth(): ReturnType<typeof OAuthStrategy>;
5
+ export declare function getWixClient(): WixClient<undefined, ReturnType<typeof SiteSessionAuth>>;
package/dist/runtime.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { authStrategyAsyncLocalStorage } from "./auth-context.js";
2
+ import { wixContext } from '@wix/sdk-context';
2
3
  export function getAuth() {
3
4
  const auth = authStrategyAsyncLocalStorage.getStore()?.auth;
4
5
  if (!auth) {
@@ -6,3 +7,6 @@ export function getAuth() {
6
7
  }
7
8
  return auth;
8
9
  }
10
+ export function getWixClient() {
11
+ return wixContext['client'];
12
+ }
@@ -0,0 +1,2 @@
1
+ import { OAuthStrategy } from "@wix/sdk/auth/oauth2";
2
+ export declare function getAuth(): ReturnType<typeof OAuthStrategy>;
@@ -0,0 +1,8 @@
1
+ import { authStrategyAsyncLocalStorage } from "./auth-context.js";
2
+ export function getAuth() {
3
+ const auth = authStrategyAsyncLocalStorage.getStore()?.auth;
4
+ if (!auth) {
5
+ throw new Error("No authentication strategy found in the current context");
6
+ }
7
+ return auth;
8
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wix/astro",
3
3
  "description": "Develop your Astro site on the Wix Platform",
4
- "version": "0.2.17",
4
+ "version": "0.3.0",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -14,7 +14,8 @@
14
14
  "./loaders": "./dist/loaders/index.js",
15
15
  "./entrypoints/server.js": "./dist/entrypoints/server.js",
16
16
  "./package.json": "./package.json",
17
- "./runtime": "./dist/runtime.js",
17
+ "./runtime/client": "./dist/runtime.client.js",
18
+ "./runtime/server": "./dist/runtime.server.js",
18
19
  "./components/login.astro": "./dist/components/login.astro"
19
20
  },
20
21
  "files": [
@@ -32,7 +33,7 @@
32
33
  "@wix/blog": "^1.0.345",
33
34
  "@wix/data": "^1.0.194",
34
35
  "@wix/identity": "^1.0.125",
35
- "@wix/sdk": "^1.15.14",
36
+ "@wix/sdk": "^1.15.18",
36
37
  "chalk": "^5.4.1",
37
38
  "esm-resolve": "^1.0.11",
38
39
  "globby": "^14.0.2",