@linabase/js 0.2.2 → 0.3.0
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 +37 -1
- package/dist/index.d.cts +16 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +37 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -631,13 +631,28 @@ var AuthClient = class {
|
|
|
631
631
|
/**
|
|
632
632
|
* Set (or clear) the current session. Use this to restore a persisted
|
|
633
633
|
* session on app launch (e.g., from AsyncStorage / SecureStore).
|
|
634
|
+
* Automatically refreshes the token if it has expired.
|
|
634
635
|
* Emits INITIAL_SESSION to onAuthStateChange listeners when called externally.
|
|
635
636
|
* Internal callers (signIn, signUp, etc.) pass _internal=true to skip the emit.
|
|
636
637
|
*/
|
|
637
638
|
setSession(session, _internal) {
|
|
638
639
|
this.currentSession = session;
|
|
639
640
|
if (this.onSessionChange) this.onSessionChange(session);
|
|
640
|
-
if (!_internal)
|
|
641
|
+
if (!_internal) {
|
|
642
|
+
if (session && session.expires_at <= Math.floor(Date.now() / 1e3)) {
|
|
643
|
+
this.refreshSession().then(({ error }) => {
|
|
644
|
+
if (error) {
|
|
645
|
+
this.currentSession = null;
|
|
646
|
+
if (this.onSessionChange) this.onSessionChange(null);
|
|
647
|
+
this.emit("SIGNED_OUT", null);
|
|
648
|
+
} else {
|
|
649
|
+
this.emit("INITIAL_SESSION", this.currentSession);
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
} else {
|
|
653
|
+
this.emit("INITIAL_SESSION", session);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
641
656
|
}
|
|
642
657
|
// ─── Email/Password ────────────────────────────────────────
|
|
643
658
|
async signUp(params) {
|
|
@@ -1057,9 +1072,30 @@ function createClient(config) {
|
|
|
1057
1072
|
});
|
|
1058
1073
|
}
|
|
1059
1074
|
const authClient = new AuthClient(request);
|
|
1075
|
+
const persistSession = config.auth?.persistSession !== false && !!config.auth?.storage;
|
|
1076
|
+
const sessionStorage = config.auth?.storage;
|
|
1077
|
+
const sessionKey = config.auth?.storageKey || "@linabase/session";
|
|
1060
1078
|
authClient.onSessionChange = (session) => {
|
|
1061
1079
|
accessToken = session?.access_token || null;
|
|
1062
1080
|
};
|
|
1081
|
+
if (persistSession && sessionStorage) {
|
|
1082
|
+
authClient.onAuthStateChange((event, session) => {
|
|
1083
|
+
if (session) {
|
|
1084
|
+
sessionStorage.setItem(sessionKey, JSON.stringify(session));
|
|
1085
|
+
} else if (event === "SIGNED_OUT") {
|
|
1086
|
+
sessionStorage.removeItem(sessionKey);
|
|
1087
|
+
}
|
|
1088
|
+
});
|
|
1089
|
+
Promise.resolve(sessionStorage.getItem(sessionKey)).then((raw) => {
|
|
1090
|
+
if (raw) {
|
|
1091
|
+
try {
|
|
1092
|
+
authClient.setSession(JSON.parse(raw));
|
|
1093
|
+
} catch {
|
|
1094
|
+
sessionStorage.removeItem(sessionKey);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1063
1099
|
function buildClient(reqFn, branchSlug) {
|
|
1064
1100
|
const rc = new RpcClient(reqFn);
|
|
1065
1101
|
const ac = new AuthClient(reqFn);
|
package/dist/index.d.cts
CHANGED
|
@@ -236,6 +236,7 @@ declare class AuthClient {
|
|
|
236
236
|
/**
|
|
237
237
|
* Set (or clear) the current session. Use this to restore a persisted
|
|
238
238
|
* session on app launch (e.g., from AsyncStorage / SecureStore).
|
|
239
|
+
* Automatically refreshes the token if it has expired.
|
|
239
240
|
* Emits INITIAL_SESSION to onAuthStateChange listeners when called externally.
|
|
240
241
|
* Internal callers (signIn, signUp, etc.) pass _internal=true to skip the emit.
|
|
241
242
|
*/
|
|
@@ -415,10 +416,24 @@ declare class FunctionsClient {
|
|
|
415
416
|
}>;
|
|
416
417
|
}
|
|
417
418
|
|
|
419
|
+
/**
|
|
420
|
+
* Minimal async key-value storage interface (compatible with AsyncStorage, localStorage, etc.)
|
|
421
|
+
*/
|
|
422
|
+
interface SessionStorage {
|
|
423
|
+
getItem(key: string): Promise<string | null> | string | null;
|
|
424
|
+
setItem(key: string, value: string): Promise<void> | void;
|
|
425
|
+
removeItem(key: string): Promise<void> | void;
|
|
426
|
+
}
|
|
418
427
|
interface LinabaseConfig {
|
|
419
428
|
url: string;
|
|
420
429
|
anonKey?: string;
|
|
421
430
|
serviceRoleKey?: string;
|
|
431
|
+
/** Persistent storage for auth sessions (e.g., AsyncStorage for React Native). */
|
|
432
|
+
auth?: {
|
|
433
|
+
storage?: SessionStorage;
|
|
434
|
+
storageKey?: string;
|
|
435
|
+
persistSession?: boolean;
|
|
436
|
+
};
|
|
422
437
|
}
|
|
423
438
|
interface LinabaseClient {
|
|
424
439
|
from: (table: string) => DatabaseClient;
|
|
@@ -439,4 +454,4 @@ interface LinabaseClient {
|
|
|
439
454
|
}
|
|
440
455
|
declare function createClient(config: LinabaseConfig): LinabaseClient;
|
|
441
456
|
|
|
442
|
-
export { AuthClient, type AuthSession, type AuthUser, BucketClient, type CsvQueryResult, DatabaseClient, type FunctionInvokeOptions, FunctionsClient, type LinabaseClient, type LinabaseConfig, type OAuthProvider, type PostgrestError, type QueryResult, RpcClient, type SingleQueryResult, StorageClient, createClient };
|
|
457
|
+
export { AuthClient, type AuthSession, type AuthUser, BucketClient, type CsvQueryResult, DatabaseClient, type FunctionInvokeOptions, FunctionsClient, type LinabaseClient, type LinabaseConfig, type OAuthProvider, type PostgrestError, type QueryResult, RpcClient, type SessionStorage, type SingleQueryResult, StorageClient, createClient };
|
package/dist/index.d.ts
CHANGED
|
@@ -236,6 +236,7 @@ declare class AuthClient {
|
|
|
236
236
|
/**
|
|
237
237
|
* Set (or clear) the current session. Use this to restore a persisted
|
|
238
238
|
* session on app launch (e.g., from AsyncStorage / SecureStore).
|
|
239
|
+
* Automatically refreshes the token if it has expired.
|
|
239
240
|
* Emits INITIAL_SESSION to onAuthStateChange listeners when called externally.
|
|
240
241
|
* Internal callers (signIn, signUp, etc.) pass _internal=true to skip the emit.
|
|
241
242
|
*/
|
|
@@ -415,10 +416,24 @@ declare class FunctionsClient {
|
|
|
415
416
|
}>;
|
|
416
417
|
}
|
|
417
418
|
|
|
419
|
+
/**
|
|
420
|
+
* Minimal async key-value storage interface (compatible with AsyncStorage, localStorage, etc.)
|
|
421
|
+
*/
|
|
422
|
+
interface SessionStorage {
|
|
423
|
+
getItem(key: string): Promise<string | null> | string | null;
|
|
424
|
+
setItem(key: string, value: string): Promise<void> | void;
|
|
425
|
+
removeItem(key: string): Promise<void> | void;
|
|
426
|
+
}
|
|
418
427
|
interface LinabaseConfig {
|
|
419
428
|
url: string;
|
|
420
429
|
anonKey?: string;
|
|
421
430
|
serviceRoleKey?: string;
|
|
431
|
+
/** Persistent storage for auth sessions (e.g., AsyncStorage for React Native). */
|
|
432
|
+
auth?: {
|
|
433
|
+
storage?: SessionStorage;
|
|
434
|
+
storageKey?: string;
|
|
435
|
+
persistSession?: boolean;
|
|
436
|
+
};
|
|
422
437
|
}
|
|
423
438
|
interface LinabaseClient {
|
|
424
439
|
from: (table: string) => DatabaseClient;
|
|
@@ -439,4 +454,4 @@ interface LinabaseClient {
|
|
|
439
454
|
}
|
|
440
455
|
declare function createClient(config: LinabaseConfig): LinabaseClient;
|
|
441
456
|
|
|
442
|
-
export { AuthClient, type AuthSession, type AuthUser, BucketClient, type CsvQueryResult, DatabaseClient, type FunctionInvokeOptions, FunctionsClient, type LinabaseClient, type LinabaseConfig, type OAuthProvider, type PostgrestError, type QueryResult, RpcClient, type SingleQueryResult, StorageClient, createClient };
|
|
457
|
+
export { AuthClient, type AuthSession, type AuthUser, BucketClient, type CsvQueryResult, DatabaseClient, type FunctionInvokeOptions, FunctionsClient, type LinabaseClient, type LinabaseConfig, type OAuthProvider, type PostgrestError, type QueryResult, RpcClient, type SessionStorage, type SingleQueryResult, StorageClient, createClient };
|
package/dist/index.js
CHANGED
|
@@ -599,13 +599,28 @@ var AuthClient = class {
|
|
|
599
599
|
/**
|
|
600
600
|
* Set (or clear) the current session. Use this to restore a persisted
|
|
601
601
|
* session on app launch (e.g., from AsyncStorage / SecureStore).
|
|
602
|
+
* Automatically refreshes the token if it has expired.
|
|
602
603
|
* Emits INITIAL_SESSION to onAuthStateChange listeners when called externally.
|
|
603
604
|
* Internal callers (signIn, signUp, etc.) pass _internal=true to skip the emit.
|
|
604
605
|
*/
|
|
605
606
|
setSession(session, _internal) {
|
|
606
607
|
this.currentSession = session;
|
|
607
608
|
if (this.onSessionChange) this.onSessionChange(session);
|
|
608
|
-
if (!_internal)
|
|
609
|
+
if (!_internal) {
|
|
610
|
+
if (session && session.expires_at <= Math.floor(Date.now() / 1e3)) {
|
|
611
|
+
this.refreshSession().then(({ error }) => {
|
|
612
|
+
if (error) {
|
|
613
|
+
this.currentSession = null;
|
|
614
|
+
if (this.onSessionChange) this.onSessionChange(null);
|
|
615
|
+
this.emit("SIGNED_OUT", null);
|
|
616
|
+
} else {
|
|
617
|
+
this.emit("INITIAL_SESSION", this.currentSession);
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
} else {
|
|
621
|
+
this.emit("INITIAL_SESSION", session);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
609
624
|
}
|
|
610
625
|
// ─── Email/Password ────────────────────────────────────────
|
|
611
626
|
async signUp(params) {
|
|
@@ -1025,9 +1040,30 @@ function createClient(config) {
|
|
|
1025
1040
|
});
|
|
1026
1041
|
}
|
|
1027
1042
|
const authClient = new AuthClient(request);
|
|
1043
|
+
const persistSession = config.auth?.persistSession !== false && !!config.auth?.storage;
|
|
1044
|
+
const sessionStorage = config.auth?.storage;
|
|
1045
|
+
const sessionKey = config.auth?.storageKey || "@linabase/session";
|
|
1028
1046
|
authClient.onSessionChange = (session) => {
|
|
1029
1047
|
accessToken = session?.access_token || null;
|
|
1030
1048
|
};
|
|
1049
|
+
if (persistSession && sessionStorage) {
|
|
1050
|
+
authClient.onAuthStateChange((event, session) => {
|
|
1051
|
+
if (session) {
|
|
1052
|
+
sessionStorage.setItem(sessionKey, JSON.stringify(session));
|
|
1053
|
+
} else if (event === "SIGNED_OUT") {
|
|
1054
|
+
sessionStorage.removeItem(sessionKey);
|
|
1055
|
+
}
|
|
1056
|
+
});
|
|
1057
|
+
Promise.resolve(sessionStorage.getItem(sessionKey)).then((raw) => {
|
|
1058
|
+
if (raw) {
|
|
1059
|
+
try {
|
|
1060
|
+
authClient.setSession(JSON.parse(raw));
|
|
1061
|
+
} catch {
|
|
1062
|
+
sessionStorage.removeItem(sessionKey);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1031
1067
|
function buildClient(reqFn, branchSlug) {
|
|
1032
1068
|
const rc = new RpcClient(reqFn);
|
|
1033
1069
|
const ac = new AuthClient(reqFn);
|