@supatype/client 0.1.0-alpha.6 → 0.1.0-alpha.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.
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +17 -1
- package/.turbo/turbo-typecheck.log +1 -1
- package/dist/auth.d.ts +37 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +289 -1
- package/dist/auth.js.map +1 -1
- package/dist/error-codes-doc.d.ts +1 -1
- package/dist/error-codes-doc.js +1 -1
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +20 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +67 -7
- package/dist/index.js.map +1 -1
- package/dist/query.d.ts +2 -0
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +48 -0
- package/dist/query.js.map +1 -1
- package/dist/storage.d.ts +23 -0
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +45 -6
- package/dist/storage.js.map +1 -1
- package/dist/types.d.ts +72 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/auth.ts +312 -1
- package/src/error-codes-doc.ts +1 -1
- package/src/errors.ts +1 -1
- package/src/index.ts +116 -16
- package/src/query.ts +51 -0
- package/src/storage.ts +58 -6
- package/src/types.ts +81 -1
- package/tests/auth.test.ts +69 -0
- package/tests/query.test.ts +1 -1
- package/tests/storage-quota-integration.test.ts +1 -1
- package/tests/storage-upload-limit-integration.test.ts +1 -1
- package/tests/types.test.ts +15 -1
- package/tsconfig.tsbuildinfo +1 -1
package/dist/types.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export interface User {
|
|
|
22
22
|
email?: string | undefined;
|
|
23
23
|
phone?: string | undefined;
|
|
24
24
|
role?: string | undefined;
|
|
25
|
+
isAnonymous?: boolean | undefined;
|
|
25
26
|
appMetadata: Record<string, unknown>;
|
|
26
27
|
userMetadata: Record<string, unknown>;
|
|
27
28
|
identities?: UserIdentity[] | undefined;
|
|
@@ -99,9 +100,60 @@ export interface RpcResult<TData> {
|
|
|
99
100
|
}
|
|
100
101
|
export interface TableDef {
|
|
101
102
|
Row: Record<string, unknown>;
|
|
103
|
+
/**
|
|
104
|
+
* Columns the database fills (`gen_random_uuid()`, `now()`, sequences, etc.)
|
|
105
|
+
* are optional here even when required on {@link TableDef.Row}.
|
|
106
|
+
*/
|
|
102
107
|
Insert: Record<string, unknown>;
|
|
103
108
|
Update: Record<string, unknown>;
|
|
104
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Module augmentation anchors used by generated `supatype` types output.
|
|
112
|
+
*
|
|
113
|
+
* Example generated output:
|
|
114
|
+
* declare module "@supatype/client" {
|
|
115
|
+
* interface SupatypeModels {
|
|
116
|
+
* post: { Row: PostRow; Insert: PostInsert; Update: PostUpdate }
|
|
117
|
+
* }
|
|
118
|
+
* }
|
|
119
|
+
*/
|
|
120
|
+
export interface SupatypeModels {
|
|
121
|
+
}
|
|
122
|
+
export interface SupatypeBuckets {
|
|
123
|
+
}
|
|
124
|
+
export interface SupatypeFunctions {
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Insert payload for an augmented table name (from generated `supatype` output).
|
|
128
|
+
*
|
|
129
|
+
* For inserts from plain objects, prefer this over re-stating column unions.
|
|
130
|
+
*/
|
|
131
|
+
export type TableInsert<TTable extends keyof SupatypeModels & string> = [
|
|
132
|
+
keyof SupatypeModels
|
|
133
|
+
] extends [never] ? Record<string, unknown> : TTable extends keyof SupatypeModels ? SupatypeModels[TTable] extends {
|
|
134
|
+
Insert: infer I;
|
|
135
|
+
} ? I : Record<string, unknown> : Record<string, unknown>;
|
|
136
|
+
type ModelDefFromAugmented<T> = T extends TableDef ? T : T extends Record<string, unknown> ? {
|
|
137
|
+
Row: T;
|
|
138
|
+
Insert: Partial<T>;
|
|
139
|
+
Update: Partial<T>;
|
|
140
|
+
} : TableDef;
|
|
141
|
+
export type AugmentedTables = [
|
|
142
|
+
keyof SupatypeModels
|
|
143
|
+
] extends [never] ? Record<string, TableDef> : {
|
|
144
|
+
[K in keyof SupatypeModels & string]: ModelDefFromAugmented<SupatypeModels[K]>;
|
|
145
|
+
};
|
|
146
|
+
export type AugmentedFunctions = [
|
|
147
|
+
keyof SupatypeFunctions
|
|
148
|
+
] extends [never] ? Record<string, FunctionDef> : {
|
|
149
|
+
[K in keyof SupatypeFunctions & string]: SupatypeFunctions[K] extends FunctionDef ? SupatypeFunctions[K] : FunctionDef;
|
|
150
|
+
};
|
|
151
|
+
export interface AugmentedDatabase {
|
|
152
|
+
public: {
|
|
153
|
+
Tables: AugmentedTables;
|
|
154
|
+
Functions?: AugmentedFunctions | undefined;
|
|
155
|
+
};
|
|
156
|
+
}
|
|
105
157
|
/**
|
|
106
158
|
* Describes the shape of a Postgres function for typed `.rpc()` calls.
|
|
107
159
|
* Generated by the Supatype engine from detected Postgres functions.
|
|
@@ -117,15 +169,27 @@ export interface AnyDatabase {
|
|
|
117
169
|
};
|
|
118
170
|
}
|
|
119
171
|
export interface SupatypeClientConfig {
|
|
120
|
-
/** Base URL of the Supatype gateway (e.g. http://localhost:
|
|
172
|
+
/** Base URL of the Supatype gateway (e.g. http://localhost:18473) */
|
|
121
173
|
url: string;
|
|
122
174
|
/** Anon JWT key */
|
|
123
175
|
anonKey: string;
|
|
176
|
+
/**
|
|
177
|
+
* Service role key — bypasses row-level security.
|
|
178
|
+
* Only set this in trusted server-side or developer-tool contexts.
|
|
179
|
+
* Never expose to end-user clients.
|
|
180
|
+
*/
|
|
181
|
+
serviceRoleKey?: string | undefined;
|
|
124
182
|
auth?: {
|
|
125
183
|
/** Persist session across page reloads (browser only). Default: true */
|
|
126
184
|
persistSession?: boolean | undefined;
|
|
127
185
|
/** localStorage key to use for session storage. Default: "supatype.auth.session" */
|
|
128
186
|
storageKey?: string | undefined;
|
|
187
|
+
/**
|
|
188
|
+
* Cookie prefix used for browser-written auth cookie.
|
|
189
|
+
* `@supatype/ssr` reads `<prefix>-auth-token` (and `<prefix>-*-auth-token` forms).
|
|
190
|
+
* Default: `"st"`.
|
|
191
|
+
*/
|
|
192
|
+
cookiePrefix?: string | undefined;
|
|
129
193
|
} | undefined;
|
|
130
194
|
/**
|
|
131
195
|
* Disable automatic retry for transient errors.
|
|
@@ -137,5 +201,12 @@ export interface SupatypeClientConfig {
|
|
|
137
201
|
* Applied to every HTTP request made by the client.
|
|
138
202
|
*/
|
|
139
203
|
timeout?: number | undefined;
|
|
204
|
+
/**
|
|
205
|
+
* Pre-load a session at construction time (SSR use case).
|
|
206
|
+
* Used by `@supatype/ssr` to inject a cookie-parsed session into the client
|
|
207
|
+
* before any requests are made.
|
|
208
|
+
*/
|
|
209
|
+
initialSession?: Session | undefined;
|
|
140
210
|
}
|
|
211
|
+
export {};
|
|
141
212
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACrC,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAClC;AAED,MAAM,WAAW,MAAM;IACrB,EAAE,EAAE,MAAM,CAAA;IACV,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,UAAU,EAAE,MAAM,GAAG,OAAO,GAAG,UAAU,CAAA;IACzC,MAAM,EAAE,UAAU,GAAG,YAAY,CAAA;IACjC,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IAC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,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IAC3B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5B;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CACrC"}
|
package/package.json
CHANGED
package/src/auth.ts
CHANGED
|
@@ -12,16 +12,92 @@ import type {
|
|
|
12
12
|
|
|
13
13
|
type AuthListener = (event: AuthChangeEvent, session: Session | null) => void
|
|
14
14
|
|
|
15
|
+
interface AuthStorageAdapter {
|
|
16
|
+
getItem(key: string): string | null
|
|
17
|
+
setItem(key: string, value: string): void
|
|
18
|
+
removeItem(key: string): void
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface AuthClientOptions {
|
|
22
|
+
initialSession?: Session | undefined
|
|
23
|
+
persistSession?: boolean | undefined
|
|
24
|
+
storageKey?: string | undefined
|
|
25
|
+
cookiePrefix?: string | undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const DEFAULT_STORAGE_KEY = "supatype.auth.session"
|
|
29
|
+
const DEFAULT_COOKIE_PREFIX = "st"
|
|
30
|
+
const DEBUG_AUTH = typeof process !== "undefined" && process.env["NEXT_PUBLIC_SUPATYPE_DEBUG_AUTH"] === "1"
|
|
31
|
+
|
|
15
32
|
export class AuthClient {
|
|
16
33
|
private readonly url: string
|
|
17
34
|
private readonly baseHeaders: Record<string, string>
|
|
18
35
|
private currentSession: Session | null = null
|
|
19
36
|
private readonly listeners = new Map<string, AuthListener>()
|
|
20
37
|
private listenerIdCounter = 0
|
|
38
|
+
private readonly persistSession: boolean
|
|
39
|
+
private readonly storageKey: string
|
|
40
|
+
private readonly cookieName: string
|
|
41
|
+
private readonly storage: AuthStorageAdapter | null
|
|
42
|
+
/** Pending auto-refresh timer; refreshes the access token shortly before it expires. */
|
|
43
|
+
private refreshTimer: ReturnType<typeof setTimeout> | null = null
|
|
21
44
|
|
|
22
|
-
constructor(url: string, baseHeaders: Record<string, string
|
|
45
|
+
constructor(url: string, baseHeaders: Record<string, string>, opts: AuthClientOptions = {}) {
|
|
23
46
|
this.url = url
|
|
24
47
|
this.baseHeaders = baseHeaders
|
|
48
|
+
this.persistSession = opts.persistSession ?? true
|
|
49
|
+
this.storageKey = opts.storageKey ?? DEFAULT_STORAGE_KEY
|
|
50
|
+
const cookiePrefix = opts.cookiePrefix ?? DEFAULT_COOKIE_PREFIX
|
|
51
|
+
this.cookieName = `${cookiePrefix}-auth-token`
|
|
52
|
+
this.storage = this.getBrowserStorage()
|
|
53
|
+
|
|
54
|
+
if (opts.initialSession !== undefined) {
|
|
55
|
+
this.currentSession = opts.initialSession
|
|
56
|
+
if (DEBUG_AUTH) {
|
|
57
|
+
console.debug("[supatype:auth] constructor initialSession provided", {
|
|
58
|
+
hasSession: true,
|
|
59
|
+
userId: opts.initialSession.user.id,
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
} else if (this.persistSession) {
|
|
63
|
+
this.currentSession = this.loadPersistedSession()
|
|
64
|
+
if (DEBUG_AUTH) {
|
|
65
|
+
console.debug("[supatype:auth] constructor loaded persisted session", {
|
|
66
|
+
hasSession: this.currentSession !== null,
|
|
67
|
+
userId: this.currentSession?.user.id ?? null,
|
|
68
|
+
storageKey: this.storageKey,
|
|
69
|
+
cookieName: this.cookieName,
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Keep the access token fresh while the app is open (and refresh immediately
|
|
74
|
+
// if a persisted session loaded already expired).
|
|
75
|
+
this.scheduleAutoRefresh()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Schedule a token refresh ~60s before the current session expires. */
|
|
79
|
+
private scheduleAutoRefresh(): void {
|
|
80
|
+
if (this.refreshTimer !== null) {
|
|
81
|
+
clearTimeout(this.refreshTimer)
|
|
82
|
+
this.refreshTimer = null
|
|
83
|
+
}
|
|
84
|
+
if (typeof setTimeout === "undefined") return
|
|
85
|
+
const session = this.currentSession
|
|
86
|
+
if (session === null || !session.refreshToken) return
|
|
87
|
+
|
|
88
|
+
const expiryMs =
|
|
89
|
+
session.expiresAt !== undefined
|
|
90
|
+
? session.expiresAt * 1000
|
|
91
|
+
: Date.now() + (session.expiresIn || 3600) * 1000
|
|
92
|
+
const delay = Math.max(0, expiryMs - Date.now() - 60_000)
|
|
93
|
+
|
|
94
|
+
this.refreshTimer = setTimeout(() => {
|
|
95
|
+
this.refreshTimer = null
|
|
96
|
+
// refreshSession() flows through _setSession on success, which reschedules.
|
|
97
|
+
void this.refreshSession().catch(() => undefined)
|
|
98
|
+
}, delay)
|
|
99
|
+
// Avoid keeping a Node process alive for this timer (no-op in browsers).
|
|
100
|
+
;(this.refreshTimer as unknown as { unref?: () => void }).unref?.()
|
|
25
101
|
}
|
|
26
102
|
|
|
27
103
|
async signUp(credentials: {
|
|
@@ -65,6 +141,30 @@ export class AuthClient {
|
|
|
65
141
|
return this._parseAuthResponse(res)
|
|
66
142
|
}
|
|
67
143
|
|
|
144
|
+
async signInAnonymously(credentials?: {
|
|
145
|
+
options?: {
|
|
146
|
+
data?: Record<string, unknown> | undefined
|
|
147
|
+
captchaToken?: string | undefined
|
|
148
|
+
} | undefined
|
|
149
|
+
}): Promise<{
|
|
150
|
+
data: { session: Session | null; user: User | null }
|
|
151
|
+
error: SupatypeError | null
|
|
152
|
+
}> {
|
|
153
|
+
const body: Record<string, unknown> = {}
|
|
154
|
+
if (credentials?.options?.data !== undefined) {
|
|
155
|
+
body["data"] = credentials.options.data
|
|
156
|
+
}
|
|
157
|
+
if (credentials?.options?.captchaToken !== undefined) {
|
|
158
|
+
body["gotrue_meta_security"] = { captcha_token: credentials.options.captchaToken }
|
|
159
|
+
}
|
|
160
|
+
const res = await fetch(`${this.url}/signup`, {
|
|
161
|
+
method: "POST",
|
|
162
|
+
headers: this.baseHeaders,
|
|
163
|
+
body: JSON.stringify(body),
|
|
164
|
+
})
|
|
165
|
+
return this._parseAuthResponse(res)
|
|
166
|
+
}
|
|
167
|
+
|
|
68
168
|
async signInWithOAuth(opts: {
|
|
69
169
|
provider: string
|
|
70
170
|
options?: { redirectTo?: string | undefined } | undefined
|
|
@@ -617,9 +717,219 @@ export class AuthClient {
|
|
|
617
717
|
|
|
618
718
|
// ─── Internal ────────────────────────────────────────────────────────────────
|
|
619
719
|
|
|
720
|
+
/** Returns the current session's access token, or null when signed out. */
|
|
721
|
+
get currentAccessToken(): string | null {
|
|
722
|
+
return this.currentSession?.accessToken ?? null
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
private getBrowserStorage(): AuthStorageAdapter | null {
|
|
726
|
+
if (typeof window === "undefined") return null
|
|
727
|
+
try {
|
|
728
|
+
return window.localStorage
|
|
729
|
+
} catch {
|
|
730
|
+
return null
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
private loadPersistedSession(): Session | null {
|
|
735
|
+
const fromStorage = this.storage?.getItem(this.storageKey) ?? null
|
|
736
|
+
if (fromStorage !== null) {
|
|
737
|
+
const parsed = this.parsePersistedSession(fromStorage)
|
|
738
|
+
if (DEBUG_AUTH) {
|
|
739
|
+
console.debug("[supatype:auth] loadPersistedSession localStorage", {
|
|
740
|
+
found: true,
|
|
741
|
+
parsed: parsed !== null,
|
|
742
|
+
})
|
|
743
|
+
}
|
|
744
|
+
if (parsed !== null) return parsed
|
|
745
|
+
}
|
|
746
|
+
if (typeof document !== "undefined") {
|
|
747
|
+
const fromCookie = this.readCookie(this.cookieName)
|
|
748
|
+
if (fromCookie !== null) {
|
|
749
|
+
const parsed = this.parsePersistedSession(fromCookie)
|
|
750
|
+
if (DEBUG_AUTH) {
|
|
751
|
+
console.debug("[supatype:auth] loadPersistedSession cookie", {
|
|
752
|
+
found: true,
|
|
753
|
+
parsed: parsed !== null,
|
|
754
|
+
cookieName: this.cookieName,
|
|
755
|
+
})
|
|
756
|
+
}
|
|
757
|
+
if (parsed !== null) return parsed
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return null
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
private parsePersistedSession(raw: string): Session | null {
|
|
764
|
+
try {
|
|
765
|
+
const parsed = JSON.parse(raw) as unknown
|
|
766
|
+
return this.normalizePersistedSession(parsed)
|
|
767
|
+
} catch {
|
|
768
|
+
return null
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
private normalizePersistedSession(raw: unknown): Session | null {
|
|
773
|
+
if (typeof raw !== "object" || raw === null) return null
|
|
774
|
+
const r = raw as Record<string, unknown>
|
|
775
|
+
const accessToken =
|
|
776
|
+
typeof r["accessToken"] === "string"
|
|
777
|
+
? r["accessToken"]
|
|
778
|
+
: typeof r["access_token"] === "string"
|
|
779
|
+
? r["access_token"]
|
|
780
|
+
: null
|
|
781
|
+
if (accessToken === null) return null
|
|
782
|
+
const refreshToken =
|
|
783
|
+
typeof r["refreshToken"] === "string"
|
|
784
|
+
? r["refreshToken"]
|
|
785
|
+
: typeof r["refresh_token"] === "string"
|
|
786
|
+
? r["refresh_token"]
|
|
787
|
+
: ""
|
|
788
|
+
const tokenType =
|
|
789
|
+
typeof r["tokenType"] === "string"
|
|
790
|
+
? r["tokenType"]
|
|
791
|
+
: typeof r["token_type"] === "string"
|
|
792
|
+
? r["token_type"]
|
|
793
|
+
: "bearer"
|
|
794
|
+
const expiresIn =
|
|
795
|
+
typeof r["expiresIn"] === "number"
|
|
796
|
+
? r["expiresIn"]
|
|
797
|
+
: typeof r["expires_in"] === "number"
|
|
798
|
+
? r["expires_in"]
|
|
799
|
+
: 3600
|
|
800
|
+
const userRaw =
|
|
801
|
+
typeof r["user"] === "object" && r["user"] !== null
|
|
802
|
+
? (r["user"] as Record<string, unknown>)
|
|
803
|
+
: null
|
|
804
|
+
if (userRaw === null) return null
|
|
805
|
+
const normalizedUserRaw: Record<string, unknown> = {
|
|
806
|
+
...userRaw,
|
|
807
|
+
...(userRaw["app_metadata"] === undefined &&
|
|
808
|
+
userRaw["appMetadata"] !== undefined && { app_metadata: userRaw["appMetadata"] }),
|
|
809
|
+
...(userRaw["user_metadata"] === undefined &&
|
|
810
|
+
userRaw["userMetadata"] !== undefined && { user_metadata: userRaw["userMetadata"] }),
|
|
811
|
+
...(userRaw["created_at"] === undefined &&
|
|
812
|
+
userRaw["createdAt"] !== undefined && { created_at: userRaw["createdAt"] }),
|
|
813
|
+
...(userRaw["updated_at"] === undefined &&
|
|
814
|
+
userRaw["updatedAt"] !== undefined && { updated_at: userRaw["updatedAt"] }),
|
|
815
|
+
}
|
|
816
|
+
const session: Session = {
|
|
817
|
+
accessToken,
|
|
818
|
+
refreshToken,
|
|
819
|
+
tokenType,
|
|
820
|
+
expiresIn,
|
|
821
|
+
user: this._parseUser(normalizedUserRaw),
|
|
822
|
+
...(typeof r["expiresAt"] === "number"
|
|
823
|
+
? { expiresAt: r["expiresAt"] }
|
|
824
|
+
: typeof r["expires_at"] === "number"
|
|
825
|
+
? { expiresAt: r["expires_at"] }
|
|
826
|
+
: {}),
|
|
827
|
+
}
|
|
828
|
+
return session
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
private syncPersistedSession(session: Session | null): void {
|
|
832
|
+
if (!this.persistSession) return
|
|
833
|
+
const storage = this.storage
|
|
834
|
+
const cookieExpires = session?.expiresAt
|
|
835
|
+
if (session === null) {
|
|
836
|
+
try {
|
|
837
|
+
storage?.removeItem(this.storageKey)
|
|
838
|
+
} catch {
|
|
839
|
+
// Ignore storage write failures.
|
|
840
|
+
}
|
|
841
|
+
this.writeCookie(this.cookieName, "", -1)
|
|
842
|
+
if (DEBUG_AUTH) {
|
|
843
|
+
console.debug("[supatype:auth] cleared persisted session", {
|
|
844
|
+
storageKey: this.storageKey,
|
|
845
|
+
cookieName: this.cookieName,
|
|
846
|
+
})
|
|
847
|
+
}
|
|
848
|
+
return
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
const json = JSON.stringify(session)
|
|
852
|
+
const cookiePayload = JSON.stringify({
|
|
853
|
+
access_token: session.accessToken,
|
|
854
|
+
refresh_token: session.refreshToken,
|
|
855
|
+
token_type: session.tokenType,
|
|
856
|
+
expires_in: session.expiresIn,
|
|
857
|
+
...(session.expiresAt !== undefined && { expires_at: session.expiresAt }),
|
|
858
|
+
user: this.serializeUserForCookie(session.user),
|
|
859
|
+
})
|
|
860
|
+
try {
|
|
861
|
+
storage?.setItem(this.storageKey, json)
|
|
862
|
+
} catch {
|
|
863
|
+
// Ignore storage write failures.
|
|
864
|
+
}
|
|
865
|
+
this.writeCookie(this.cookieName, cookiePayload, cookieExpires)
|
|
866
|
+
if (DEBUG_AUTH) {
|
|
867
|
+
console.debug("[supatype:auth] persisted session", {
|
|
868
|
+
userId: session.user.id,
|
|
869
|
+
storageKey: this.storageKey,
|
|
870
|
+
cookieName: this.cookieName,
|
|
871
|
+
hasExpiresAt: session.expiresAt !== undefined,
|
|
872
|
+
})
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
private serializeUserForCookie(user: User): Record<string, unknown> {
|
|
877
|
+
return {
|
|
878
|
+
id: user.id,
|
|
879
|
+
...(user.email !== undefined && { email: user.email }),
|
|
880
|
+
...(user.phone !== undefined && { phone: user.phone }),
|
|
881
|
+
...(user.role !== undefined && { role: user.role }),
|
|
882
|
+
app_metadata: user.appMetadata,
|
|
883
|
+
user_metadata: user.userMetadata,
|
|
884
|
+
created_at: user.createdAt,
|
|
885
|
+
updated_at: user.updatedAt,
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
private readCookie(name: string): string | null {
|
|
890
|
+
if (typeof document === "undefined") return null
|
|
891
|
+
const all = document.cookie ? document.cookie.split("; ") : []
|
|
892
|
+
for (const item of all) {
|
|
893
|
+
const idx = item.indexOf("=")
|
|
894
|
+
if (idx <= 0) continue
|
|
895
|
+
const key = item.slice(0, idx)
|
|
896
|
+
if (key !== name) continue
|
|
897
|
+
const value = item.slice(idx + 1)
|
|
898
|
+
try {
|
|
899
|
+
return decodeURIComponent(value)
|
|
900
|
+
} catch {
|
|
901
|
+
return value
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
return null
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
private writeCookie(name: string, value: string, expiresAtSeconds?: number): void {
|
|
908
|
+
if (typeof document === "undefined") return
|
|
909
|
+
const secure = typeof window !== "undefined" && window.location.protocol === "https:"
|
|
910
|
+
const attrs = [
|
|
911
|
+
`Path=/`,
|
|
912
|
+
"SameSite=Lax",
|
|
913
|
+
...(secure ? ["Secure"] : []),
|
|
914
|
+
]
|
|
915
|
+
if (expiresAtSeconds !== undefined) {
|
|
916
|
+
attrs.push(`Expires=${new Date(expiresAtSeconds * 1000).toUTCString()}`)
|
|
917
|
+
} else if (value === "") {
|
|
918
|
+
attrs.push("Expires=Thu, 01 Jan 1970 00:00:00 GMT")
|
|
919
|
+
}
|
|
920
|
+
document.cookie = `${name}=${encodeURIComponent(value)}; ${attrs.join("; ")}`
|
|
921
|
+
}
|
|
922
|
+
|
|
620
923
|
_setSession(session: Session | null): void {
|
|
621
924
|
const prev = this.currentSession
|
|
622
925
|
this.currentSession = session
|
|
926
|
+
this.syncPersistedSession(session)
|
|
927
|
+
if (session !== null) {
|
|
928
|
+
this.scheduleAutoRefresh()
|
|
929
|
+
} else if (this.refreshTimer !== null) {
|
|
930
|
+
clearTimeout(this.refreshTimer)
|
|
931
|
+
this.refreshTimer = null
|
|
932
|
+
}
|
|
623
933
|
const event: AuthChangeEvent =
|
|
624
934
|
session !== null
|
|
625
935
|
? prev !== null
|
|
@@ -680,6 +990,7 @@ export class AuthClient {
|
|
|
680
990
|
...(raw["email"] !== undefined && { email: String(raw["email"]) }),
|
|
681
991
|
...(raw["phone"] !== undefined && { phone: String(raw["phone"]) }),
|
|
682
992
|
...(raw["role"] !== undefined && { role: String(raw["role"]) }),
|
|
993
|
+
...(raw["is_anonymous"] !== undefined && { isAnonymous: Boolean(raw["is_anonymous"]) }),
|
|
683
994
|
appMetadata: (raw["app_metadata"] ?? {}) as Record<string, unknown>,
|
|
684
995
|
userMetadata: (raw["user_metadata"] ?? {}) as Record<string, unknown>,
|
|
685
996
|
createdAt: String(raw["created_at"] ?? ""),
|
package/src/error-codes-doc.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Error codes documentation — Gap Appendices task 35
|
|
3
3
|
*
|
|
4
4
|
* Published list of all Supatype error codes with descriptions and suggested
|
|
5
|
-
* fixes. Accessible at docs.supatype.
|
|
5
|
+
* fixes. Accessible at docs.supatype.com/errors.
|
|
6
6
|
*
|
|
7
7
|
* Each SDK error includes a `helpUrl` field linking to the relevant section
|
|
8
8
|
* on the documentation page.
|
package/src/errors.ts
CHANGED