nocal-auth-sdk 0.1.6 → 0.1.8

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,37 +1,10 @@
1
- export type PopupResult<T = Record<string, unknown>> = {
2
- type: "success";
3
- data: T;
4
- } | {
5
- type: "error";
6
- error: string;
7
- } | {
8
- type: "closed";
9
- };
10
1
  export interface NocalAuthOptions {
11
- clientId: string;
12
- redirectUri: string;
13
- scope?: string;
14
- state?: string;
15
- issuerOrigin?: string;
16
- responseType?: "code" | "token";
17
- popupFeatures?: string;
18
- extraParams?: Record<string, string | number | boolean | undefined>;
19
- }
20
- export interface PopupOptions {
21
- width?: number;
22
- height?: number;
23
- popupName?: string;
2
+ authorizeUrl?: string;
3
+ [key: string]: any;
24
4
  }
25
5
  export declare class NocalAuth {
26
- private readonly clientId;
27
- private readonly redirectUri;
28
- private readonly scope?;
29
- private readonly state?;
30
- private readonly issuerOrigin;
31
- private readonly responseType;
32
- private readonly popupFeatures?;
33
- private readonly extraParams?;
34
- constructor(options: NocalAuthOptions);
35
- private getAuthorizeUrl;
36
- loginWithPopup(popupOptions?: PopupOptions): Promise<Record<string, unknown>>;
6
+ private authorizeUrl;
7
+ [key: string]: any;
8
+ constructor(options?: NocalAuthOptions);
9
+ loginWithPopup(): Promise<unknown>;
37
10
  }
package/dist/NocalAuth.js CHANGED
@@ -1,129 +1,54 @@
1
- function buildQuery(params) {
2
- const qs = Object.entries(params)
3
- .filter(([, value]) => value !== undefined && value !== null && value !== "")
4
- .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
5
- .join("&");
6
- return qs ? `?${qs}` : "";
7
- }
8
1
  export class NocalAuth {
9
- constructor(options) {
10
- if (!options.clientId) {
11
- throw new Error("NocalAuth requires a clientId");
12
- }
13
- if (!options.redirectUri) {
14
- throw new Error("NocalAuth requires a redirectUri");
15
- }
16
- this.clientId = options.clientId;
17
- this.redirectUri = options.redirectUri;
18
- this.scope = options.scope;
19
- this.state = options.state;
20
- this.issuerOrigin = (options.issuerOrigin ?? "https://auth.nocal.vn").replace(/\/$/, "");
21
- this.responseType = options.responseType ?? "code";
22
- this.popupFeatures = options.popupFeatures;
23
- this.extraParams = options.extraParams;
2
+ constructor(options = {}) {
3
+ this.authorizeUrl = options.authorizeUrl || "";
24
4
  }
25
- getAuthorizeUrl() {
26
- const baseUrl = `${this.issuerOrigin}`;
27
- const query = buildQuery({
28
- client_id: this.clientId,
29
- redirect_uri: this.redirectUri,
30
- response_type: this.responseType,
31
- scope: this.scope,
32
- state: this.state,
33
- ...this.extraParams,
34
- });
35
- return `${baseUrl}${query}`;
36
- }
37
- async loginWithPopup(popupOptions = {}) {
5
+ loginWithPopup() {
6
+ // Thêm check an toàn nếu chạy ở môi trường SSR (Next.js/Nuxt) không có window
38
7
  if (typeof window === "undefined") {
39
- throw new Error("loginWithPopup() can only be used in a browser environment");
40
- }
41
- const width = popupOptions.width ?? 500;
42
- const height = popupOptions.height ?? 700;
43
- const popupName = popupOptions.popupName ?? "nocal_auth_popup";
44
- const dualScreenLeft = window.screenLeft ?? window.screenX ?? 0;
45
- const dualScreenTop = window.screenTop ?? window.screenY ?? 0;
46
- const screenWidth = window.innerWidth ??
47
- document.documentElement.clientWidth ??
48
- window.screen.width ??
49
- 1280;
50
- const screenHeight = window.innerHeight ??
51
- document.documentElement.clientHeight ??
52
- window.screen.height ??
53
- 720;
54
- const left = Math.round(dualScreenLeft + (screenWidth - width) / 2);
55
- const top = Math.round(dualScreenTop + (screenHeight - height) / 2);
56
- const features = [
57
- `width=${width}`,
58
- `height=${height}`,
59
- `left=${left}`,
60
- `top=${top}`,
61
- "resizable=yes",
62
- "scrollbars=yes",
63
- this.popupFeatures ?? "",
64
- ]
65
- .filter(Boolean)
66
- .join(",");
67
- // Xây dựng URL với tham số display=popup và các tham số khác
68
- const authUrl = new URL(this.getAuthorizeUrl());
69
- authUrl.searchParams.set("display", "popup");
70
- const popup = window.open(authUrl.toString(), popupName, features);
71
- if (!popup) {
72
- throw new Error("POPUP_BLOCKED");
8
+ return Promise.reject(new Error("loginWithPopup() can only be used in a browser environment"));
73
9
  }
74
- popup.focus?.();
75
10
  return new Promise((resolve, reject) => {
11
+ const width = 500;
12
+ const height = 700;
13
+ const left = window.screenX + (window.innerWidth - width) / 2;
14
+ const top = window.screenY + (window.innerHeight - height) / 2;
15
+ const features = `width=${width},height=${height},left=${left},top=${top},resizable=yes,scrollbars=yes`;
16
+ const popup = window.open(this.authorizeUrl, "nocal_auth_popup", features);
17
+ if (!popup)
18
+ return reject(new Error("POPUP_BLOCKED"));
19
+ popup.focus();
76
20
  let settled = false;
77
- let checkInterval = undefined;
21
+ let timer = null;
78
22
  const cleanup = () => {
79
- settled = true;
80
- window.removeEventListener("message", handleMessage);
81
- if (checkInterval) {
82
- clearInterval(checkInterval);
83
- }
84
- };
85
- const rejectWith = (message) => {
86
23
  if (settled)
87
24
  return;
88
- cleanup();
89
- reject(new Error(message));
25
+ settled = true;
26
+ window.removeEventListener("message", handleMessage);
27
+ if (timer)
28
+ clearInterval(timer);
90
29
  };
91
30
  const handleMessage = (event) => {
92
- if (settled)
93
- return;
94
- // Kiểm tra origin bảo mật
95
- if (event.origin !== this.issuerOrigin) {
96
- return;
97
- }
98
31
  const payload = event.data;
99
- if (!payload || typeof payload !== "object") {
32
+ if (!payload || payload.type !== "nocal_auth_result")
100
33
  return;
101
- }
102
- if (payload.type !== "nocal_auth_result") {
103
- return;
104
- }
105
34
  cleanup();
106
- if (payload.success === false || payload.error) {
107
- rejectWith(typeof payload.error === "string" ? payload.error : "AUTH_FAILED");
108
- return;
35
+ if (payload.success) {
36
+ resolve(payload.data);
109
37
  }
110
- const rawData = payload.data ?? payload;
111
- const data = {
112
- token: rawData.accessToken ?? rawData.token ?? rawData.access_token,
113
- refreshToken: rawData.refreshToken,
114
- user: rawData.user,
115
- state: rawData.state,
116
- };
117
- if (!data || Object.keys(data).length === 0) {
118
- rejectWith("AUTH_DATA_MISSING");
119
- return;
38
+ else {
39
+ reject(new Error(payload.error || "AUTH_FAILED"));
120
40
  }
121
- resolve(data);
122
41
  };
123
- window.addEventListener("message", handleMessage, false);
124
- checkInterval = window.setInterval(() => {
125
- if (popup.closed) {
126
- rejectWith("POPUP_CLOSED");
42
+ window.addEventListener("message", handleMessage);
43
+ timer = setInterval(() => {
44
+ try {
45
+ if (popup.closed) {
46
+ cleanup();
47
+ reject(new Error("POPUP_CLOSED"));
48
+ }
49
+ }
50
+ catch (e) {
51
+ // Bỏ qua lỗi Cross-Origin khi popup chuyển hướng
127
52
  }
128
53
  }, 500);
129
54
  });
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { NocalAuth } from './NocalAuth';
2
- export type { NocalAuthOptions, PopupOptions, PopupResult } from './NocalAuth';
1
+ export { NocalAuth } from "./NocalAuth";
2
+ export type { NocalAuthOptions } from "./NocalAuth";
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { NocalAuth } from './NocalAuth';
1
+ export { NocalAuth } from "./NocalAuth";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nocal-auth-sdk",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Nocal Auth - client-side SDK for Login with Nocal (popup flow)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",