@lovable.dev/cloud-auth-js 1.1.3-rc.8 → 1.1.3-rc.9
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.
- package/dist/index.cjs +139 -190
- package/dist/index.d.cts +21 -19
- package/dist/index.d.ts +21 -19
- package/dist/index.js +138 -163
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1,204 +1,153 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
createLovableAuth: () => createLovableAuth
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
|
|
27
|
-
// src/auth.ts
|
|
28
|
-
var EXPECTED_MESSAGE_TYPE = "authorization_response";
|
|
29
|
-
var DEFAULT_OAUTH_BROKER_URL = "/~oauth/initiate";
|
|
30
|
-
var DEFAULT_SUPPORTED_OAUTH_ORIGINS = ["https://oauth.lovable.app", "https://lovable.dev"];
|
|
31
|
-
var DEFAULT_MOBILE_DEEP_LINK_REDIRECT_URI = "lovable://oauth-callback";
|
|
32
|
-
var DEFAULT_DESKTOP_LOCALHOST_REDIRECT_URI = "http://127.0.0.1/iframe-oauth/callback";
|
|
33
|
-
var POPUP_CHECK_INTERVAL_MS = 500;
|
|
34
|
-
var IFRAME_FALLBACK_TIMEOUT_MS = 12e4;
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/auth.ts
|
|
3
|
+
const EXPECTED_MESSAGE_TYPE = "authorization_response";
|
|
4
|
+
const DEFAULT_OAUTH_BROKER_URL = "/~oauth/initiate";
|
|
5
|
+
const DEFAULT_SUPPORTED_OAUTH_ORIGINS = ["https://oauth.lovable.app", "https://lovable.dev"];
|
|
6
|
+
const DEFAULT_MOBILE_DEEP_LINK_REDIRECT_URI = "lovable://oauth-callback";
|
|
7
|
+
const DEFAULT_DESKTOP_LOCALHOST_REDIRECT_URI = "http://127.0.0.1/iframe-oauth/callback";
|
|
8
|
+
const POPUP_CHECK_INTERVAL_MS = 500;
|
|
9
|
+
const IFRAME_FALLBACK_TIMEOUT_MS = 12e4;
|
|
35
10
|
function startWebMessageListener(supportedOrigins) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
window.removeEventListener("message", callback);
|
|
56
|
-
};
|
|
57
|
-
window.addEventListener("message", callback);
|
|
58
|
-
return {
|
|
59
|
-
cleanup,
|
|
60
|
-
messagePromise: promise
|
|
61
|
-
};
|
|
11
|
+
let resolvePromise;
|
|
12
|
+
const promise = new Promise((resolve) => {
|
|
13
|
+
resolvePromise = resolve;
|
|
14
|
+
});
|
|
15
|
+
const callback = (e) => {
|
|
16
|
+
if (!supportedOrigins.some((origin) => e.origin === origin)) return;
|
|
17
|
+
const data = e.data;
|
|
18
|
+
if (!data || typeof data !== "object") return;
|
|
19
|
+
if (data.type !== EXPECTED_MESSAGE_TYPE) return;
|
|
20
|
+
resolvePromise(data.response);
|
|
21
|
+
};
|
|
22
|
+
const cleanup = () => {
|
|
23
|
+
window.removeEventListener("message", callback);
|
|
24
|
+
};
|
|
25
|
+
window.addEventListener("message", callback);
|
|
26
|
+
return {
|
|
27
|
+
cleanup,
|
|
28
|
+
messagePromise: promise
|
|
29
|
+
};
|
|
62
30
|
}
|
|
63
31
|
function getPopupDimensions(isInIframe) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
32
|
+
const hasBrowserPosition = window.screenX !== 0 || window.screenY !== 0 || !isInIframe;
|
|
33
|
+
const width = hasBrowserPosition ? window.outerWidth * .5 : window.screen.width * .5;
|
|
34
|
+
const height = hasBrowserPosition ? window.outerHeight * .5 : window.screen.height * .5;
|
|
35
|
+
return {
|
|
36
|
+
width,
|
|
37
|
+
height,
|
|
38
|
+
left: hasBrowserPosition ? window.screenX + (window.outerWidth - width) / 2 : (window.screen.width - width) / 2,
|
|
39
|
+
top: hasBrowserPosition ? window.screenY + (window.outerHeight - height) / 2 : (window.screen.height - height) / 2
|
|
40
|
+
};
|
|
70
41
|
}
|
|
71
42
|
function processOAuthResponse(data, expectedState) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
return {
|
|
87
|
-
tokens: { access_token: data.access_token, refresh_token: data.refresh_token },
|
|
88
|
-
error: null
|
|
89
|
-
};
|
|
43
|
+
if (data.state !== expectedState) return { error: /* @__PURE__ */ new Error("State is invalid") };
|
|
44
|
+
if (data.error) {
|
|
45
|
+
if (data.error === "legacy_flow") return { error: /* @__PURE__ */ new Error("This flow is not supported in Preview mode. Please open the app in a new tab to sign in.") };
|
|
46
|
+
return { error: new Error(data.error_description ?? "Sign in failed") };
|
|
47
|
+
}
|
|
48
|
+
if (!data.access_token || !data.refresh_token) return { error: /* @__PURE__ */ new Error("No tokens received") };
|
|
49
|
+
return {
|
|
50
|
+
tokens: {
|
|
51
|
+
access_token: data.access_token,
|
|
52
|
+
refresh_token: data.refresh_token
|
|
53
|
+
},
|
|
54
|
+
error: null
|
|
55
|
+
};
|
|
90
56
|
}
|
|
91
57
|
function isDevice() {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return true;
|
|
97
|
-
return false;
|
|
58
|
+
const ua = navigator.userAgent;
|
|
59
|
+
if (/iPhone|iPad|iPod|Android/i.test(ua)) return true;
|
|
60
|
+
if (/Macintosh/i.test(ua) && navigator.maxTouchPoints > 1) return true;
|
|
61
|
+
return false;
|
|
98
62
|
}
|
|
99
63
|
function generateState() {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
return Math.random().toString(36).substring(2) + Date.now().toString(36);
|
|
64
|
+
if (typeof crypto !== "undefined" && crypto.getRandomValues) return [...crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16))].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
65
|
+
return Math.random().toString(36).substring(2) + Date.now().toString(36);
|
|
104
66
|
}
|
|
105
67
|
function createAuth(config = {}) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
error: error instanceof Error ? error : new Error(String(error))
|
|
185
|
-
};
|
|
186
|
-
} finally {
|
|
187
|
-
clearInterval(popupCheckInterval);
|
|
188
|
-
cleanup();
|
|
189
|
-
popup?.close();
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
return {
|
|
193
|
-
signInWithOAuth
|
|
194
|
-
};
|
|
68
|
+
const oauthBrokerUrl = config.oauthBrokerUrl ?? DEFAULT_OAUTH_BROKER_URL;
|
|
69
|
+
const supportedOAuthOrigins = config.supportedOAuthOrigins ?? DEFAULT_SUPPORTED_OAUTH_ORIGINS;
|
|
70
|
+
async function signInWithOAuth(provider, opts = {}) {
|
|
71
|
+
let isInIframe = false;
|
|
72
|
+
try {
|
|
73
|
+
isInIframe = window.self !== window.top;
|
|
74
|
+
} catch {
|
|
75
|
+
isInIframe = true;
|
|
76
|
+
}
|
|
77
|
+
const ua = navigator.userAgent;
|
|
78
|
+
const isMobileApp = /LovableApp\//.test(ua);
|
|
79
|
+
const isDesktopApp = !isMobileApp && /lovable/i.test(ua);
|
|
80
|
+
const state = generateState();
|
|
81
|
+
let redirectUri = opts.redirect_uri ?? window.location.origin;
|
|
82
|
+
if (isMobileApp && isInIframe) redirectUri = DEFAULT_MOBILE_DEEP_LINK_REDIRECT_URI;
|
|
83
|
+
else if (isDesktopApp && isInIframe) redirectUri = DEFAULT_DESKTOP_LOCALHOST_REDIRECT_URI;
|
|
84
|
+
const params = new URLSearchParams({
|
|
85
|
+
...opts.extraParams,
|
|
86
|
+
provider,
|
|
87
|
+
redirect_uri: redirectUri,
|
|
88
|
+
state
|
|
89
|
+
});
|
|
90
|
+
if (!isInIframe) {
|
|
91
|
+
window.location.href = `${oauthBrokerUrl}?${params.toString()}`;
|
|
92
|
+
return {
|
|
93
|
+
error: null,
|
|
94
|
+
redirected: true
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
if (!isMobileApp && !isDesktopApp) params.set("response_mode", "web_message");
|
|
98
|
+
const url = `${oauthBrokerUrl}?${params.toString()}`;
|
|
99
|
+
const { messagePromise, cleanup } = startWebMessageListener(isDesktopApp ? [...supportedOAuthOrigins, window.location.origin] : supportedOAuthOrigins);
|
|
100
|
+
let popup;
|
|
101
|
+
if (isDevice()) popup = window.open(url, "_blank");
|
|
102
|
+
else {
|
|
103
|
+
const { width, height, left, top } = getPopupDimensions(isInIframe);
|
|
104
|
+
popup = window.open(url, "oauth", `width=${width},height=${height},left=${left},top=${top}`);
|
|
105
|
+
}
|
|
106
|
+
if (!popup && (isMobileApp || isDesktopApp)) {
|
|
107
|
+
let webViewTimeoutId;
|
|
108
|
+
const webViewTimeoutPromise = new Promise((_, reject) => {
|
|
109
|
+
webViewTimeoutId = setTimeout(() => {
|
|
110
|
+
reject(/* @__PURE__ */ new Error("OAuth timed out waiting for response"));
|
|
111
|
+
}, IFRAME_FALLBACK_TIMEOUT_MS);
|
|
112
|
+
});
|
|
113
|
+
try {
|
|
114
|
+
return processOAuthResponse(await Promise.race([messagePromise, webViewTimeoutPromise]), state);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
return { error: error instanceof Error ? error : new Error(String(error)) };
|
|
117
|
+
} finally {
|
|
118
|
+
if (webViewTimeoutId) clearTimeout(webViewTimeoutId);
|
|
119
|
+
cleanup();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (!popup) {
|
|
123
|
+
cleanup();
|
|
124
|
+
return { error: /* @__PURE__ */ new Error("Popup was blocked") };
|
|
125
|
+
}
|
|
126
|
+
let popupCheckInterval;
|
|
127
|
+
const popupClosedPromise = new Promise((_, reject) => {
|
|
128
|
+
popupCheckInterval = setInterval(() => {
|
|
129
|
+
if (popup.closed) {
|
|
130
|
+
clearInterval(popupCheckInterval);
|
|
131
|
+
reject(/* @__PURE__ */ new Error("Sign in was cancelled"));
|
|
132
|
+
}
|
|
133
|
+
}, POPUP_CHECK_INTERVAL_MS);
|
|
134
|
+
});
|
|
135
|
+
try {
|
|
136
|
+
return processOAuthResponse(await Promise.race([messagePromise, popupClosedPromise]), state);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
return { error: error instanceof Error ? error : new Error(String(error)) };
|
|
139
|
+
} finally {
|
|
140
|
+
clearInterval(popupCheckInterval);
|
|
141
|
+
cleanup();
|
|
142
|
+
popup?.close();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return { signInWithOAuth };
|
|
195
146
|
}
|
|
196
|
-
|
|
197
|
-
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/index.ts
|
|
198
149
|
function createLovableAuth(config = {}) {
|
|
199
|
-
|
|
150
|
+
return createAuth(config);
|
|
200
151
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
createLovableAuth
|
|
204
|
-
});
|
|
152
|
+
//#endregion
|
|
153
|
+
exports.createLovableAuth = createLovableAuth;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,33 +1,35 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
1
2
|
type OAuthProvider = "google" | "apple" | "microsoft" | "lovable";
|
|
2
3
|
interface LovableAuthConfig {
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
oauthBrokerUrl?: string;
|
|
5
|
+
supportedOAuthOrigins?: string[];
|
|
5
6
|
}
|
|
6
7
|
interface OAuthTokens {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
access_token: string;
|
|
9
|
+
refresh_token: string;
|
|
9
10
|
}
|
|
10
11
|
interface SignInWithOAuthOptions {
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
redirect_uri?: string;
|
|
13
|
+
extraParams?: Record<string, string>;
|
|
13
14
|
}
|
|
14
15
|
type SignInWithOAuthResult = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
tokens: OAuthTokens;
|
|
17
|
+
error: null;
|
|
18
|
+
redirected?: false;
|
|
18
19
|
} | {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
tokens?: undefined;
|
|
21
|
+
error: Error;
|
|
22
|
+
redirected?: false;
|
|
22
23
|
} | {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
tokens?: undefined;
|
|
25
|
+
error: null;
|
|
26
|
+
redirected: true;
|
|
26
27
|
};
|
|
27
28
|
interface LovableAuth {
|
|
28
|
-
|
|
29
|
+
signInWithOAuth: (provider: OAuthProvider, opts?: SignInWithOAuthOptions) => Promise<SignInWithOAuthResult>;
|
|
29
30
|
}
|
|
30
|
-
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/index.d.ts
|
|
31
33
|
declare function createLovableAuth(config?: LovableAuthConfig): LovableAuth;
|
|
32
|
-
|
|
33
|
-
export { type LovableAuth, type LovableAuthConfig, type OAuthProvider, type OAuthTokens, type SignInWithOAuthOptions, type SignInWithOAuthResult, createLovableAuth };
|
|
34
|
+
//#endregion
|
|
35
|
+
export { type LovableAuth, type LovableAuthConfig, type OAuthProvider, type OAuthTokens, type SignInWithOAuthOptions, type SignInWithOAuthResult, createLovableAuth };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,33 +1,35 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
1
2
|
type OAuthProvider = "google" | "apple" | "microsoft" | "lovable";
|
|
2
3
|
interface LovableAuthConfig {
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
oauthBrokerUrl?: string;
|
|
5
|
+
supportedOAuthOrigins?: string[];
|
|
5
6
|
}
|
|
6
7
|
interface OAuthTokens {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
access_token: string;
|
|
9
|
+
refresh_token: string;
|
|
9
10
|
}
|
|
10
11
|
interface SignInWithOAuthOptions {
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
redirect_uri?: string;
|
|
13
|
+
extraParams?: Record<string, string>;
|
|
13
14
|
}
|
|
14
15
|
type SignInWithOAuthResult = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
tokens: OAuthTokens;
|
|
17
|
+
error: null;
|
|
18
|
+
redirected?: false;
|
|
18
19
|
} | {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
tokens?: undefined;
|
|
21
|
+
error: Error;
|
|
22
|
+
redirected?: false;
|
|
22
23
|
} | {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
tokens?: undefined;
|
|
25
|
+
error: null;
|
|
26
|
+
redirected: true;
|
|
26
27
|
};
|
|
27
28
|
interface LovableAuth {
|
|
28
|
-
|
|
29
|
+
signInWithOAuth: (provider: OAuthProvider, opts?: SignInWithOAuthOptions) => Promise<SignInWithOAuthResult>;
|
|
29
30
|
}
|
|
30
|
-
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/index.d.ts
|
|
31
33
|
declare function createLovableAuth(config?: LovableAuthConfig): LovableAuth;
|
|
32
|
-
|
|
33
|
-
export { type LovableAuth, type LovableAuthConfig, type OAuthProvider, type OAuthTokens, type SignInWithOAuthOptions, type SignInWithOAuthResult, createLovableAuth };
|
|
34
|
+
//#endregion
|
|
35
|
+
export { type LovableAuth, type LovableAuthConfig, type OAuthProvider, type OAuthTokens, type SignInWithOAuthOptions, type SignInWithOAuthResult, createLovableAuth };
|
package/dist/index.js
CHANGED
|
@@ -1,177 +1,152 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
//#region src/auth.ts
|
|
2
|
+
const EXPECTED_MESSAGE_TYPE = "authorization_response";
|
|
3
|
+
const DEFAULT_OAUTH_BROKER_URL = "/~oauth/initiate";
|
|
4
|
+
const DEFAULT_SUPPORTED_OAUTH_ORIGINS = ["https://oauth.lovable.app", "https://lovable.dev"];
|
|
5
|
+
const DEFAULT_MOBILE_DEEP_LINK_REDIRECT_URI = "lovable://oauth-callback";
|
|
6
|
+
const DEFAULT_DESKTOP_LOCALHOST_REDIRECT_URI = "http://127.0.0.1/iframe-oauth/callback";
|
|
7
|
+
const POPUP_CHECK_INTERVAL_MS = 500;
|
|
8
|
+
const IFRAME_FALLBACK_TIMEOUT_MS = 12e4;
|
|
9
9
|
function startWebMessageListener(supportedOrigins) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
window.removeEventListener("message", callback);
|
|
30
|
-
};
|
|
31
|
-
window.addEventListener("message", callback);
|
|
32
|
-
return {
|
|
33
|
-
cleanup,
|
|
34
|
-
messagePromise: promise
|
|
35
|
-
};
|
|
10
|
+
let resolvePromise;
|
|
11
|
+
const promise = new Promise((resolve) => {
|
|
12
|
+
resolvePromise = resolve;
|
|
13
|
+
});
|
|
14
|
+
const callback = (e) => {
|
|
15
|
+
if (!supportedOrigins.some((origin) => e.origin === origin)) return;
|
|
16
|
+
const data = e.data;
|
|
17
|
+
if (!data || typeof data !== "object") return;
|
|
18
|
+
if (data.type !== EXPECTED_MESSAGE_TYPE) return;
|
|
19
|
+
resolvePromise(data.response);
|
|
20
|
+
};
|
|
21
|
+
const cleanup = () => {
|
|
22
|
+
window.removeEventListener("message", callback);
|
|
23
|
+
};
|
|
24
|
+
window.addEventListener("message", callback);
|
|
25
|
+
return {
|
|
26
|
+
cleanup,
|
|
27
|
+
messagePromise: promise
|
|
28
|
+
};
|
|
36
29
|
}
|
|
37
30
|
function getPopupDimensions(isInIframe) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
31
|
+
const hasBrowserPosition = window.screenX !== 0 || window.screenY !== 0 || !isInIframe;
|
|
32
|
+
const width = hasBrowserPosition ? window.outerWidth * .5 : window.screen.width * .5;
|
|
33
|
+
const height = hasBrowserPosition ? window.outerHeight * .5 : window.screen.height * .5;
|
|
34
|
+
return {
|
|
35
|
+
width,
|
|
36
|
+
height,
|
|
37
|
+
left: hasBrowserPosition ? window.screenX + (window.outerWidth - width) / 2 : (window.screen.width - width) / 2,
|
|
38
|
+
top: hasBrowserPosition ? window.screenY + (window.outerHeight - height) / 2 : (window.screen.height - height) / 2
|
|
39
|
+
};
|
|
44
40
|
}
|
|
45
41
|
function processOAuthResponse(data, expectedState) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
tokens: { access_token: data.access_token, refresh_token: data.refresh_token },
|
|
62
|
-
error: null
|
|
63
|
-
};
|
|
42
|
+
if (data.state !== expectedState) return { error: /* @__PURE__ */ new Error("State is invalid") };
|
|
43
|
+
if (data.error) {
|
|
44
|
+
if (data.error === "legacy_flow") return { error: /* @__PURE__ */ new Error("This flow is not supported in Preview mode. Please open the app in a new tab to sign in.") };
|
|
45
|
+
return { error: new Error(data.error_description ?? "Sign in failed") };
|
|
46
|
+
}
|
|
47
|
+
if (!data.access_token || !data.refresh_token) return { error: /* @__PURE__ */ new Error("No tokens received") };
|
|
48
|
+
return {
|
|
49
|
+
tokens: {
|
|
50
|
+
access_token: data.access_token,
|
|
51
|
+
refresh_token: data.refresh_token
|
|
52
|
+
},
|
|
53
|
+
error: null
|
|
54
|
+
};
|
|
64
55
|
}
|
|
65
56
|
function isDevice() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return true;
|
|
71
|
-
return false;
|
|
57
|
+
const ua = navigator.userAgent;
|
|
58
|
+
if (/iPhone|iPad|iPod|Android/i.test(ua)) return true;
|
|
59
|
+
if (/Macintosh/i.test(ua) && navigator.maxTouchPoints > 1) return true;
|
|
60
|
+
return false;
|
|
72
61
|
}
|
|
73
62
|
function generateState() {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
return Math.random().toString(36).substring(2) + Date.now().toString(36);
|
|
63
|
+
if (typeof crypto !== "undefined" && crypto.getRandomValues) return [...crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16))].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
64
|
+
return Math.random().toString(36).substring(2) + Date.now().toString(36);
|
|
78
65
|
}
|
|
79
66
|
function createAuth(config = {}) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
error: error instanceof Error ? error : new Error(String(error))
|
|
159
|
-
};
|
|
160
|
-
} finally {
|
|
161
|
-
clearInterval(popupCheckInterval);
|
|
162
|
-
cleanup();
|
|
163
|
-
popup?.close();
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return {
|
|
167
|
-
signInWithOAuth
|
|
168
|
-
};
|
|
67
|
+
const oauthBrokerUrl = config.oauthBrokerUrl ?? DEFAULT_OAUTH_BROKER_URL;
|
|
68
|
+
const supportedOAuthOrigins = config.supportedOAuthOrigins ?? DEFAULT_SUPPORTED_OAUTH_ORIGINS;
|
|
69
|
+
async function signInWithOAuth(provider, opts = {}) {
|
|
70
|
+
let isInIframe = false;
|
|
71
|
+
try {
|
|
72
|
+
isInIframe = window.self !== window.top;
|
|
73
|
+
} catch {
|
|
74
|
+
isInIframe = true;
|
|
75
|
+
}
|
|
76
|
+
const ua = navigator.userAgent;
|
|
77
|
+
const isMobileApp = /LovableApp\//.test(ua);
|
|
78
|
+
const isDesktopApp = !isMobileApp && /lovable/i.test(ua);
|
|
79
|
+
const state = generateState();
|
|
80
|
+
let redirectUri = opts.redirect_uri ?? window.location.origin;
|
|
81
|
+
if (isMobileApp && isInIframe) redirectUri = DEFAULT_MOBILE_DEEP_LINK_REDIRECT_URI;
|
|
82
|
+
else if (isDesktopApp && isInIframe) redirectUri = DEFAULT_DESKTOP_LOCALHOST_REDIRECT_URI;
|
|
83
|
+
const params = new URLSearchParams({
|
|
84
|
+
...opts.extraParams,
|
|
85
|
+
provider,
|
|
86
|
+
redirect_uri: redirectUri,
|
|
87
|
+
state
|
|
88
|
+
});
|
|
89
|
+
if (!isInIframe) {
|
|
90
|
+
window.location.href = `${oauthBrokerUrl}?${params.toString()}`;
|
|
91
|
+
return {
|
|
92
|
+
error: null,
|
|
93
|
+
redirected: true
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (!isMobileApp && !isDesktopApp) params.set("response_mode", "web_message");
|
|
97
|
+
const url = `${oauthBrokerUrl}?${params.toString()}`;
|
|
98
|
+
const { messagePromise, cleanup } = startWebMessageListener(isDesktopApp ? [...supportedOAuthOrigins, window.location.origin] : supportedOAuthOrigins);
|
|
99
|
+
let popup;
|
|
100
|
+
if (isDevice()) popup = window.open(url, "_blank");
|
|
101
|
+
else {
|
|
102
|
+
const { width, height, left, top } = getPopupDimensions(isInIframe);
|
|
103
|
+
popup = window.open(url, "oauth", `width=${width},height=${height},left=${left},top=${top}`);
|
|
104
|
+
}
|
|
105
|
+
if (!popup && (isMobileApp || isDesktopApp)) {
|
|
106
|
+
let webViewTimeoutId;
|
|
107
|
+
const webViewTimeoutPromise = new Promise((_, reject) => {
|
|
108
|
+
webViewTimeoutId = setTimeout(() => {
|
|
109
|
+
reject(/* @__PURE__ */ new Error("OAuth timed out waiting for response"));
|
|
110
|
+
}, IFRAME_FALLBACK_TIMEOUT_MS);
|
|
111
|
+
});
|
|
112
|
+
try {
|
|
113
|
+
return processOAuthResponse(await Promise.race([messagePromise, webViewTimeoutPromise]), state);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
return { error: error instanceof Error ? error : new Error(String(error)) };
|
|
116
|
+
} finally {
|
|
117
|
+
if (webViewTimeoutId) clearTimeout(webViewTimeoutId);
|
|
118
|
+
cleanup();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (!popup) {
|
|
122
|
+
cleanup();
|
|
123
|
+
return { error: /* @__PURE__ */ new Error("Popup was blocked") };
|
|
124
|
+
}
|
|
125
|
+
let popupCheckInterval;
|
|
126
|
+
const popupClosedPromise = new Promise((_, reject) => {
|
|
127
|
+
popupCheckInterval = setInterval(() => {
|
|
128
|
+
if (popup.closed) {
|
|
129
|
+
clearInterval(popupCheckInterval);
|
|
130
|
+
reject(/* @__PURE__ */ new Error("Sign in was cancelled"));
|
|
131
|
+
}
|
|
132
|
+
}, POPUP_CHECK_INTERVAL_MS);
|
|
133
|
+
});
|
|
134
|
+
try {
|
|
135
|
+
return processOAuthResponse(await Promise.race([messagePromise, popupClosedPromise]), state);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
return { error: error instanceof Error ? error : new Error(String(error)) };
|
|
138
|
+
} finally {
|
|
139
|
+
clearInterval(popupCheckInterval);
|
|
140
|
+
cleanup();
|
|
141
|
+
popup?.close();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { signInWithOAuth };
|
|
169
145
|
}
|
|
170
|
-
|
|
171
|
-
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/index.ts
|
|
172
148
|
function createLovableAuth(config = {}) {
|
|
173
|
-
|
|
149
|
+
return createAuth(config);
|
|
174
150
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
};
|
|
151
|
+
//#endregion
|
|
152
|
+
export { createLovableAuth };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lovable.dev/cloud-auth-js",
|
|
3
|
-
"version": "1.1.3-rc.
|
|
3
|
+
"version": "1.1.3-rc.9",
|
|
4
4
|
"description": "Lovable legacy OAuth broker JS",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -36,14 +36,14 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"jsdom": "^26",
|
|
38
38
|
"rimraf": "^5.0.0",
|
|
39
|
-
"
|
|
39
|
+
"tsdown": "^0.22.5",
|
|
40
40
|
"typescript": "^5",
|
|
41
41
|
"vitest": "^4.1.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
|
-
"build": "
|
|
44
|
+
"build": "tsdown",
|
|
45
45
|
"check:package": "tmp=$(mktemp -d) && pnpm pack --out \"$tmp/pkg.tgz\" && tar -xzf \"$tmp/pkg.tgz\" -C \"$tmp\" && publint \"$tmp/package\" --strict && attw \"$tmp/pkg.tgz\"",
|
|
46
|
-
"dev": "
|
|
46
|
+
"dev": "tsdown --watch",
|
|
47
47
|
"test": "vitest run",
|
|
48
48
|
"typecheck": "tsgo --noEmit",
|
|
49
49
|
"clean": "rimraf dist"
|