nocal-auth-sdk 0.2.0 → 0.2.1
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 +2 -1
- package/dist/index.js +44 -6
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare class NocalAuthSDK {
|
|
|
7
7
|
private currentUser;
|
|
8
8
|
constructor(config: NocalClientConfig);
|
|
9
9
|
/**
|
|
10
|
-
* Mở Popup UI Đăng nhập & Ủy quyền OAuth 2.0 với PKCE
|
|
10
|
+
* Mở Popup UI Đăng nhập & Ủy quyền chuẩn Supabase Native OAuth 2.0 với PKCE
|
|
11
11
|
*/
|
|
12
12
|
signInWithNocal(req?: OAuthLoginReq): Promise<{
|
|
13
13
|
user: NocalUser;
|
|
@@ -19,4 +19,5 @@ export declare class NocalAuthSDK {
|
|
|
19
19
|
getSession(): Promise<NocalSession | null>;
|
|
20
20
|
signOut(): Promise<void>;
|
|
21
21
|
}
|
|
22
|
+
export { NocalAuthSDK as NocalAuth };
|
|
22
23
|
export * from "./types";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const DEFAULT_AUTH_SERVER_URL = "
|
|
1
|
+
const DEFAULT_AUTH_SERVER_URL = "https://hldymiruucsqqzicnwdv.supabase.co";
|
|
2
2
|
// Helper for PKCE crypto
|
|
3
3
|
function base64URLEncode(buffer) {
|
|
4
4
|
let binary = "";
|
|
@@ -45,13 +45,13 @@ export class NocalAuthSDK {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
-
* Mở Popup UI Đăng nhập & Ủy quyền OAuth 2.0 với PKCE
|
|
48
|
+
* Mở Popup UI Đăng nhập & Ủy quyền chuẩn Supabase Native OAuth 2.0 với PKCE
|
|
49
49
|
*/
|
|
50
50
|
async signInWithNocal(req) {
|
|
51
51
|
const state = req?.state || generateRandomString(16);
|
|
52
52
|
const codeVerifier = generateRandomString(32);
|
|
53
53
|
const codeChallenge = await generateCodeChallenge(codeVerifier);
|
|
54
|
-
const authUrl = new URL(`${this.authServerUrl}/oauth/authorize`);
|
|
54
|
+
const authUrl = new URL(`${this.authServerUrl}/auth/v1/oauth/authorize`);
|
|
55
55
|
authUrl.searchParams.set("client_id", this.clientId);
|
|
56
56
|
authUrl.searchParams.set("redirect_uri", this.redirectUri);
|
|
57
57
|
authUrl.searchParams.set("response_type", "code");
|
|
@@ -73,6 +73,7 @@ export class NocalAuthSDK {
|
|
|
73
73
|
return;
|
|
74
74
|
const data = event.data;
|
|
75
75
|
if (!data || (data.type !== "NOCAL_OAUTH_CODE" && data.type !== "NOCAL_OAUTH_ERROR")) {
|
|
76
|
+
// Hoặc kiểm tra URL redirect của popup nếu Supabase native redirect trực tiếp
|
|
76
77
|
return;
|
|
77
78
|
}
|
|
78
79
|
window.removeEventListener("message", messageListener);
|
|
@@ -95,7 +96,6 @@ export class NocalAuthSDK {
|
|
|
95
96
|
}
|
|
96
97
|
try {
|
|
97
98
|
popup.close();
|
|
98
|
-
// Đổi code lấy token
|
|
99
99
|
const session = await this.exchangeCodeForToken(code, codeVerifier);
|
|
100
100
|
const user = await this.fetchUserInfo(session.access_token);
|
|
101
101
|
this.currentSession = session;
|
|
@@ -115,8 +115,45 @@ export class NocalAuthSDK {
|
|
|
115
115
|
if (popup.closed) {
|
|
116
116
|
clearInterval(checkClosedTimer);
|
|
117
117
|
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)
|
|
118
119
|
reject(new Error("POPUP_CLOSED_BY_USER"));
|
|
119
120
|
}
|
|
121
|
+
else {
|
|
122
|
+
// Lắng nghe URL thay đổi nếu Supabase redirect trực tiếp về redirectUri
|
|
123
|
+
try {
|
|
124
|
+
const popupUrl = new URL(popup.location.href);
|
|
125
|
+
if (popupUrl.origin === new URL(this.redirectUri).origin) {
|
|
126
|
+
const code = popupUrl.searchParams.get("code");
|
|
127
|
+
const error = popupUrl.searchParams.get("error");
|
|
128
|
+
const returnedState = popupUrl.searchParams.get("state");
|
|
129
|
+
if (code || error) {
|
|
130
|
+
clearInterval(checkClosedTimer);
|
|
131
|
+
window.removeEventListener("message", messageListener);
|
|
132
|
+
popup.close();
|
|
133
|
+
if (error) {
|
|
134
|
+
reject(new Error(error));
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (code) {
|
|
138
|
+
this.exchangeCodeForToken(code, codeVerifier)
|
|
139
|
+
.then(async (session) => {
|
|
140
|
+
const user = await this.fetchUserInfo(session.access_token);
|
|
141
|
+
this.currentSession = session;
|
|
142
|
+
this.currentUser = user;
|
|
143
|
+
if (typeof window !== "undefined") {
|
|
144
|
+
localStorage.setItem("nocal_auth_session", JSON.stringify({ session, user }));
|
|
145
|
+
}
|
|
146
|
+
resolve({ user, session });
|
|
147
|
+
})
|
|
148
|
+
.catch(reject);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
// Cross-origin check while on Supabase auth domain
|
|
155
|
+
}
|
|
156
|
+
}
|
|
120
157
|
}
|
|
121
158
|
catch (e) {
|
|
122
159
|
// ignore
|
|
@@ -125,7 +162,7 @@ export class NocalAuthSDK {
|
|
|
125
162
|
});
|
|
126
163
|
}
|
|
127
164
|
async exchangeCodeForToken(code, codeVerifier) {
|
|
128
|
-
const res = await fetch(`${this.authServerUrl}/
|
|
165
|
+
const res = await fetch(`${this.authServerUrl}/auth/v1/oauth/token`, {
|
|
129
166
|
method: "POST",
|
|
130
167
|
headers: { "Content-Type": "application/json" },
|
|
131
168
|
body: JSON.stringify({
|
|
@@ -143,7 +180,7 @@ export class NocalAuthSDK {
|
|
|
143
180
|
return data;
|
|
144
181
|
}
|
|
145
182
|
async fetchUserInfo(accessToken) {
|
|
146
|
-
const res = await fetch(`${this.authServerUrl}/
|
|
183
|
+
const res = await fetch(`${this.authServerUrl}/auth/v1/oauth/userinfo`, {
|
|
147
184
|
headers: {
|
|
148
185
|
Authorization: `Bearer ${accessToken}`,
|
|
149
186
|
},
|
|
@@ -168,4 +205,5 @@ export class NocalAuthSDK {
|
|
|
168
205
|
}
|
|
169
206
|
}
|
|
170
207
|
}
|
|
208
|
+
export { NocalAuthSDK as NocalAuth };
|
|
171
209
|
export * from "./types";
|