@inflector/aura 0.1.8 → 0.1.10

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/auth.d.ts CHANGED
@@ -1,3 +1,9 @@
1
+ type SessionEvent = "login" | "logout" | "change";
2
+ interface SessionEventsType {
3
+ on(event: SessionEvent, callback: () => void): () => void;
4
+ emit(event: SessionEvent): void;
5
+ }
6
+ export declare const SessionEvents: SessionEventsType;
1
7
  export declare const AuraAuth: (url: string, workspace: string) => {
2
8
  Logout: <FetchOptions extends import("better-auth").ClientFetchOption<never, Partial<Record<string, any>> & Record<string, any>, Record<string, any> | undefined>>(data_0?: import("better-auth").Prettify<{
3
9
  query?: Record<string, any> | undefined;
@@ -79,6 +85,9 @@ export declare const AuraAuth: (url: string, workspace: string) => {
79
85
  message?: string | undefined;
80
86
  }, FetchOptions["throw"] extends true ? true : false>>;
81
87
  };
88
+ OnUserLoaded: (fn: () => void | Promise<void>) => void;
89
+ OnUserNotFound: (fn: () => void | Promise<void>) => void;
90
+ readonly User: any;
82
91
  Oauth: {
83
92
  Google: (data?: Omit<import("better-auth").Prettify<{
84
93
  provider: (string & {}) | "github" | "apple" | "atlassian" | "cognito" | "discord" | "facebook" | "figma" | "microsoft" | "google" | "huggingface" | "slack" | "spotify" | "twitch" | "twitter" | "dropbox" | "kick" | "linear" | "linkedin" | "gitlab" | "tiktok" | "reddit" | "roblox" | "salesforce" | "vk" | "zoom" | "notion" | "kakao" | "naver" | "line" | "paybin" | "paypal" | "polar" | "vercel";
@@ -1229,4 +1238,5 @@ export declare const AuraAuth: (url: string, workspace: string) => {
1229
1238
  }, FetchOptions["throw"] extends true ? true : false>>;
1230
1239
  };
1231
1240
  };
1241
+ export {};
1232
1242
  //# sourceMappingURL=auth.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,QAAQ,GAAI,KAA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better-auth/client/plugins";
3
3
  import { computed } from "nanostores";
4
+ // Only create once on the global object
5
+ export const SessionEvents = (() => {
6
+ if (window.__SessionEvents)
7
+ return window.__SessionEvents;
8
+ const listeners = {
9
+ login: [],
10
+ logout: [],
11
+ change: [],
12
+ };
13
+ const events = {
14
+ on(event, callback) {
15
+ listeners[event].push(callback);
16
+ return () => {
17
+ const i = listeners[event].indexOf(callback);
18
+ if (i >= 0)
19
+ listeners[event].splice(i, 1);
20
+ };
21
+ },
22
+ emit(event) {
23
+ listeners[event].forEach((cb) => cb());
24
+ listeners.change.forEach((cb) => cb());
25
+ },
26
+ };
27
+ window.__SessionEvents = events;
28
+ return events;
29
+ })();
4
30
  export const AuraAuth = (url, workspace) => {
31
+ const _onUserLoadedCallbacks = [];
32
+ const _onUserNotFoundCallbacks = [];
33
+ let _initialized = false;
34
+ let User = null;
5
35
  const authClient = createAuthClient({
6
36
  baseURL: url + "/api/auth/" + workspace,
7
- plugins: [
8
- anonymousClient()
9
- ],
37
+ plugins: [anonymousClient()],
10
38
  fetchOptions: {
11
39
  credentials: "include",
12
40
  },
@@ -14,26 +42,71 @@ export const AuraAuth = (url, workspace) => {
14
42
  const Session = computed(authClient.useSession, (session) => {
15
43
  if (!session?.data?.user)
16
44
  return session;
45
+ const Events = window.__SessionEvents;
46
+ Events.emit("change");
17
47
  return {
18
48
  ...session,
19
49
  data: {
20
50
  ...session.data,
21
51
  user: {
22
52
  ...session.data.user,
23
- image: session.data.user.image
24
- ? url + session.data.user.image
25
- : null,
53
+ image: session.data.user.image ? url + session.data.user.image : null,
26
54
  },
27
55
  },
28
56
  };
29
57
  });
30
- authClient.getSession();
58
+ const triggerCallbacks = async (callbacks) => {
59
+ for (const fn of callbacks)
60
+ await fn();
61
+ };
62
+ const initialize = async () => {
63
+ try {
64
+ const data = await authClient.getSession();
65
+ if (data.data?.user) {
66
+ User = data.data.user;
67
+ await triggerCallbacks(_onUserLoadedCallbacks);
68
+ }
69
+ else {
70
+ await triggerCallbacks(_onUserNotFoundCallbacks);
71
+ }
72
+ }
73
+ catch (err) {
74
+ console.error("Error initializing auth:", err);
75
+ await triggerCallbacks(_onUserNotFoundCallbacks);
76
+ }
77
+ finally {
78
+ _initialized = true;
79
+ }
80
+ };
81
+ const OnUserLoaded = (fn) => {
82
+ if (_initialized && User) {
83
+ Promise.resolve(fn());
84
+ }
85
+ else {
86
+ _onUserLoadedCallbacks.push(fn);
87
+ }
88
+ };
89
+ const OnUserNotFound = (fn) => {
90
+ if (_initialized && !User) {
91
+ Promise.resolve(fn());
92
+ }
93
+ else {
94
+ _onUserNotFoundCallbacks.push(fn);
95
+ }
96
+ };
97
+ // Initialize auth on creation
98
+ initialize();
31
99
  return {
32
100
  Logout: authClient.signOut,
33
101
  Email: {
34
102
  SignUp: authClient.signUp.email,
35
103
  Login: authClient.signIn.email,
36
104
  },
105
+ OnUserLoaded,
106
+ OnUserNotFound,
107
+ get User() {
108
+ return User;
109
+ },
37
110
  Oauth: (() => {
38
111
  const withDefaultCallback = (provider, data) => authClient.signIn.social({
39
112
  provider,
@@ -66,6 +139,6 @@ export const AuraAuth = (url, workspace) => {
66
139
  Anonymous: {
67
140
  SignUp: authClient.signIn.anonymous,
68
141
  Delete: authClient.deleteAnonymousUser,
69
- }
142
+ },
70
143
  };
71
144
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflector/aura",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",