nocal-auth-sdk 0.2.1 → 0.2.2
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.d.ts +1 -1
- package/dist/index.js +25 -21
- package/dist/types.d.ts +6 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare class NocalAuthSDK {
|
|
|
13
13
|
user: NocalUser;
|
|
14
14
|
session: NocalSession;
|
|
15
15
|
}>;
|
|
16
|
-
|
|
16
|
+
exchangeCodeForToken(code: string, codeVerifier: string): Promise<NocalSession>;
|
|
17
17
|
private fetchUserInfo;
|
|
18
18
|
getUser(): Promise<NocalUser | null>;
|
|
19
19
|
getSession(): Promise<NocalSession | null>;
|
package/dist/index.js
CHANGED
|
@@ -60,8 +60,12 @@ export class NocalAuthSDK {
|
|
|
60
60
|
authUrl.searchParams.set("code_challenge_method", "S256");
|
|
61
61
|
const width = 480;
|
|
62
62
|
const height = 680;
|
|
63
|
-
const left = typeof window !== "undefined"
|
|
64
|
-
|
|
63
|
+
const left = typeof window !== "undefined"
|
|
64
|
+
? window.screenX + (window.outerWidth - width) / 2
|
|
65
|
+
: 0;
|
|
66
|
+
const top = typeof window !== "undefined"
|
|
67
|
+
? window.screenY + (window.outerHeight - height) / 2
|
|
68
|
+
: 0;
|
|
65
69
|
const popup = window.open(authUrl.toString(), "NocalAuthPopup", `width=${width},height=${height},left=${left},top=${top},status=yes,scrollbars=yes`);
|
|
66
70
|
if (!popup) {
|
|
67
71
|
throw new Error("POPUP_BLOCKED");
|
|
@@ -72,8 +76,9 @@ export class NocalAuthSDK {
|
|
|
72
76
|
if (event.origin !== allowedOrigin)
|
|
73
77
|
return;
|
|
74
78
|
const data = event.data;
|
|
75
|
-
if (!data ||
|
|
76
|
-
|
|
79
|
+
if (!data ||
|
|
80
|
+
(data.type !== "NOCAL_OAUTH_CODE" &&
|
|
81
|
+
data.type !== "NOCAL_OAUTH_ERROR")) {
|
|
77
82
|
return;
|
|
78
83
|
}
|
|
79
84
|
window.removeEventListener("message", messageListener);
|
|
@@ -115,17 +120,14 @@ export class NocalAuthSDK {
|
|
|
115
120
|
if (popup.closed) {
|
|
116
121
|
clearInterval(checkClosedTimer);
|
|
117
122
|
window.removeEventListener("message", messageListener);
|
|
118
|
-
// Kiểm tra nếu popup đóng và đã có code trong redirect URL (nếu dùng redirect thuần)
|
|
119
123
|
reject(new Error("POPUP_CLOSED_BY_USER"));
|
|
120
124
|
}
|
|
121
125
|
else {
|
|
122
|
-
// Lắng nghe URL thay đổi nếu Supabase redirect trực tiếp về redirectUri
|
|
123
126
|
try {
|
|
124
127
|
const popupUrl = new URL(popup.location.href);
|
|
125
128
|
if (popupUrl.origin === new URL(this.redirectUri).origin) {
|
|
126
129
|
const code = popupUrl.searchParams.get("code");
|
|
127
130
|
const error = popupUrl.searchParams.get("error");
|
|
128
|
-
const returnedState = popupUrl.searchParams.get("state");
|
|
129
131
|
if (code || error) {
|
|
130
132
|
clearInterval(checkClosedTimer);
|
|
131
133
|
window.removeEventListener("message", messageListener);
|
|
@@ -151,7 +153,7 @@ export class NocalAuthSDK {
|
|
|
151
153
|
}
|
|
152
154
|
}
|
|
153
155
|
catch (e) {
|
|
154
|
-
// Cross-origin check
|
|
156
|
+
// Cross-origin check
|
|
155
157
|
}
|
|
156
158
|
}
|
|
157
159
|
}
|
|
@@ -162,22 +164,24 @@ export class NocalAuthSDK {
|
|
|
162
164
|
});
|
|
163
165
|
}
|
|
164
166
|
async exchangeCodeForToken(code, codeVerifier) {
|
|
165
|
-
const
|
|
167
|
+
const params = new URLSearchParams();
|
|
168
|
+
params.set("grant_type", "authorization_code");
|
|
169
|
+
params.set("client_id", this.clientId);
|
|
170
|
+
params.set("redirect_uri", this.redirectUri);
|
|
171
|
+
params.set("code", code);
|
|
172
|
+
params.set("code_verifier", codeVerifier); // PKCE xác thực thay cho secret
|
|
173
|
+
const response = await fetch(`${this.authServerUrl}/auth/v1/oauth/token`, {
|
|
166
174
|
method: "POST",
|
|
167
|
-
headers: {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
client_id: this.clientId,
|
|
172
|
-
redirect_uri: this.redirectUri,
|
|
173
|
-
code_verifier: codeVerifier,
|
|
174
|
-
}),
|
|
175
|
+
headers: {
|
|
176
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
177
|
+
},
|
|
178
|
+
body: params.toString(),
|
|
175
179
|
});
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
throw new Error(
|
|
180
|
+
if (!response.ok) {
|
|
181
|
+
const errorData = await response.json();
|
|
182
|
+
throw new Error(`Supabase Auth Error: ${errorData.error_description || errorData.error}`);
|
|
179
183
|
}
|
|
180
|
-
return
|
|
184
|
+
return (await response.json());
|
|
181
185
|
}
|
|
182
186
|
async fetchUserInfo(accessToken) {
|
|
183
187
|
const res = await fetch(`${this.authServerUrl}/auth/v1/oauth/userinfo`, {
|
package/dist/types.d.ts
CHANGED
|
@@ -2,9 +2,12 @@ export interface NocalClientConfig {
|
|
|
2
2
|
clientId: string;
|
|
3
3
|
redirectUri: string;
|
|
4
4
|
authServerUrl?: string;
|
|
5
|
+
storageKey?: string;
|
|
5
6
|
}
|
|
6
7
|
export interface OAuthLoginReq {
|
|
7
8
|
state?: string;
|
|
9
|
+
provider?: string;
|
|
10
|
+
redirectTo?: string;
|
|
8
11
|
}
|
|
9
12
|
export interface NocalUser {
|
|
10
13
|
sub: string;
|
|
@@ -12,12 +15,13 @@ export interface NocalUser {
|
|
|
12
15
|
phone?: string;
|
|
13
16
|
email_confirmed?: boolean;
|
|
14
17
|
phone_confirmed?: boolean;
|
|
15
|
-
user_metadata?: Record<string,
|
|
16
|
-
[key: string]:
|
|
18
|
+
user_metadata?: Record<string, unknown>;
|
|
19
|
+
[key: string]: unknown;
|
|
17
20
|
}
|
|
18
21
|
export interface NocalSession {
|
|
19
22
|
access_token: string;
|
|
20
23
|
refresh_token: string;
|
|
21
24
|
token_type: string;
|
|
22
25
|
expires_in: number;
|
|
26
|
+
expires_at?: number;
|
|
23
27
|
}
|