@pinooxhq/auth 0.1.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.
@@ -0,0 +1,176 @@
1
+ type AuthMode = 'jwt' | 'cookie' | 'session';
2
+ type AuthStrategy = 'local' | 'remote';
3
+ type AuthEvent = 'unauthorized' | 'mismatch' | 'error' | 'login' | 'logout' | 'config';
4
+ interface BootAuthConfig {
5
+ mode?: string;
6
+ key?: string;
7
+ provider?: string | null;
8
+ source?: string | null;
9
+ strategy?: AuthStrategy | string;
10
+ loginUrl?: string;
11
+ /** Prefix for relative endpoint paths (e.g. `/account/api/v1`). */
12
+ baseUrl?: string | null;
13
+ endpoints?: AuthEndpoints;
14
+ }
15
+ interface PinooxBootstrap {
16
+ url?: {
17
+ API?: string;
18
+ APP?: string;
19
+ BASE?: string;
20
+ SITE?: string;
21
+ [key: string]: string | undefined;
22
+ };
23
+ locale?: string;
24
+ auth?: BootAuthConfig;
25
+ [key: string]: unknown;
26
+ }
27
+ interface AuthEndpoints {
28
+ login?: string;
29
+ logout?: string;
30
+ me?: string;
31
+ }
32
+ interface ResolvedAuthConfig {
33
+ mode: AuthMode;
34
+ key: string;
35
+ provider: string | null;
36
+ source: string | null;
37
+ strategy: AuthStrategy;
38
+ loginUrl: string | null;
39
+ /** Optional prefix used to resolve relative endpoint paths. */
40
+ baseUrl: string | null;
41
+ endpoints: Required<AuthEndpoints>;
42
+ apiBase: string;
43
+ /** Site origin from __PINOOX__.url.SITE (for Vite HMR absolute redirects). */
44
+ siteOrigin: string | null;
45
+ /** App path prefix e.g. /account — blocked as post-login return target. */
46
+ appPath: string | null;
47
+ debug: boolean;
48
+ }
49
+ interface LoginCredentials {
50
+ username?: string;
51
+ email?: string;
52
+ mobile?: string;
53
+ password: string;
54
+ remember?: boolean;
55
+ [key: string]: unknown;
56
+ }
57
+ interface AuthUser {
58
+ [key: string]: unknown;
59
+ }
60
+ interface LoginResult {
61
+ token: string | null;
62
+ user: AuthUser | null;
63
+ raw: unknown;
64
+ }
65
+ interface AuthRequestAuth {
66
+ header: string | null;
67
+ credentials: RequestCredentials;
68
+ mode: AuthMode;
69
+ key: string;
70
+ }
71
+ interface AuthLogEvent {
72
+ level: 'debug' | 'info' | 'warn' | 'error';
73
+ code: string;
74
+ message: string;
75
+ context?: Record<string, unknown>;
76
+ }
77
+ type AuthEventHandler = (payload?: unknown) => void;
78
+ type AuthLogSink = (event: AuthLogEvent) => void;
79
+
80
+ interface AuthLogger {
81
+ emit: (event: AuthLogEvent) => void;
82
+ debug: (code: string, message: string, context?: Record<string, unknown>) => void;
83
+ info: (code: string, message: string, context?: Record<string, unknown>) => void;
84
+ warn: (code: string, message: string, context?: Record<string, unknown>) => void;
85
+ error: (code: string, message: string, context?: Record<string, unknown>) => void;
86
+ setSink: (sink: AuthLogSink | null) => void;
87
+ }
88
+ declare function createLogger(debug?: boolean, sink?: AuthLogSink | null): AuthLogger;
89
+
90
+ interface CreateAuthOptions {
91
+ mode?: AuthMode;
92
+ key?: string;
93
+ strategy?: AuthStrategy;
94
+ loginUrl?: string;
95
+ /** Prefix for relative endpoint paths (e.g. `/account/api/v1`). */
96
+ baseUrl?: string | null;
97
+ endpoints?: AuthEndpoints;
98
+ apiBase?: string;
99
+ /** Override site origin (defaults to __PINOOX__.url.SITE). */
100
+ siteOrigin?: string | null;
101
+ debug?: boolean;
102
+ boot?: PinooxBootstrap | null;
103
+ }
104
+ /** Join base + relative path. Absolute http(s) or leading-/ paths pass through. */
105
+ declare function joinUrl(base: string, path: string): string;
106
+ /**
107
+ * Resolve an endpoint against optional baseUrl.
108
+ * - absolute URL or site path (`/…`) → unchanged
109
+ * - relative (`auth/get`) + baseUrl → joined
110
+ * - relative without baseUrl → returned as-is (caller should prefer absolute defaults)
111
+ */
112
+ declare function resolveEndpoint(path: string | undefined, fallback: string, baseUrl?: string | null): string;
113
+ declare function resolveConfig(options?: CreateAuthOptions, logger?: AuthLogger): ResolvedAuthConfig;
114
+
115
+ interface HttpRequestInput {
116
+ url: string;
117
+ method?: string;
118
+ headers?: Record<string, string>;
119
+ body?: unknown;
120
+ credentials?: RequestCredentials;
121
+ }
122
+ interface HttpResponse<T = unknown> {
123
+ status: number;
124
+ data: T;
125
+ headers: Headers;
126
+ ok: boolean;
127
+ }
128
+ interface HttpClient {
129
+ request: <T = unknown>(input: HttpRequestInput) => Promise<HttpResponse<T>>;
130
+ }
131
+ declare function createFetchProvider(): HttpClient;
132
+
133
+ interface AuthInstance {
134
+ readonly config: ResolvedAuthConfig;
135
+ readonly logger: AuthLogger;
136
+ user: AuthUser | null;
137
+ token: string | null;
138
+ isAuthenticated: boolean;
139
+ login: (credentials?: LoginCredentials) => Promise<LoginResult | void>;
140
+ logout: () => Promise<void>;
141
+ me: () => Promise<AuthUser | null>;
142
+ getToken: () => string | null;
143
+ setToken: (token: string | null) => void;
144
+ clearToken: () => void;
145
+ getAuthHeader: () => string | null;
146
+ authorize: (headers?: Record<string, string>) => AuthRequestAuth & {
147
+ headers: Record<string, string>;
148
+ };
149
+ getRequestAuth: () => AuthRequestAuth;
150
+ setHttp: (client: HttpClient) => void;
151
+ loginFromResponse: (payload: unknown) => LoginResult;
152
+ redirectToLogin: (returnPath?: string) => void;
153
+ /** Safe path from ?redirect= (or query object). */
154
+ getReturnPath: (queryOrPath?: unknown, fallback?: string) => string;
155
+ /** Absolute return URL (uses SITE origin when Vite origin differs). */
156
+ getReturnUrl: (queryOrPath?: unknown, fallback?: string) => string;
157
+ /** Navigate to post-login return URL. */
158
+ redirectBack: (queryOrPath?: unknown, fallback?: string) => void;
159
+ /** `{ redirect }` for router-link query preservation. */
160
+ getRedirectQuery: (queryOrPath?: unknown, fallback?: string) => {
161
+ redirect: string;
162
+ };
163
+ /** Clear session and emit `unauthorized` (e.g. from HTTP 401). */
164
+ notifyUnauthorized: (payload?: unknown) => void;
165
+ on: (event: AuthEvent, handler: AuthEventHandler) => () => void;
166
+ off: (event: AuthEvent, handler: AuthEventHandler) => void;
167
+ }
168
+ interface CreateAuthConfig extends CreateAuthOptions {
169
+ http?: HttpClient;
170
+ logger?: AuthLogger;
171
+ }
172
+ declare function getAuth(): AuthInstance;
173
+ declare function createAuth(options?: CreateAuthConfig): AuthInstance;
174
+ declare function buildLoginRedirectUrl(returnPath?: string): string;
175
+
176
+ export { type AuthLogger as A, type BootAuthConfig as B, type CreateAuthConfig as C, type HttpClient as H, type LoginCredentials as L, type PinooxBootstrap as P, type ResolvedAuthConfig as R, type AuthInstance as a, type AuthEndpoints as b, type AuthEvent as c, type AuthLogEvent as d, type AuthMode as e, type AuthRequestAuth as f, type AuthStrategy as g, type AuthUser as h, type CreateAuthOptions as i, type HttpRequestInput as j, type HttpResponse as k, type LoginResult as l, buildLoginRedirectUrl as m, createAuth as n, createFetchProvider as o, createLogger as p, getAuth as q, joinUrl as r, resolveConfig as s, resolveEndpoint as t };
@@ -0,0 +1,176 @@
1
+ type AuthMode = 'jwt' | 'cookie' | 'session';
2
+ type AuthStrategy = 'local' | 'remote';
3
+ type AuthEvent = 'unauthorized' | 'mismatch' | 'error' | 'login' | 'logout' | 'config';
4
+ interface BootAuthConfig {
5
+ mode?: string;
6
+ key?: string;
7
+ provider?: string | null;
8
+ source?: string | null;
9
+ strategy?: AuthStrategy | string;
10
+ loginUrl?: string;
11
+ /** Prefix for relative endpoint paths (e.g. `/account/api/v1`). */
12
+ baseUrl?: string | null;
13
+ endpoints?: AuthEndpoints;
14
+ }
15
+ interface PinooxBootstrap {
16
+ url?: {
17
+ API?: string;
18
+ APP?: string;
19
+ BASE?: string;
20
+ SITE?: string;
21
+ [key: string]: string | undefined;
22
+ };
23
+ locale?: string;
24
+ auth?: BootAuthConfig;
25
+ [key: string]: unknown;
26
+ }
27
+ interface AuthEndpoints {
28
+ login?: string;
29
+ logout?: string;
30
+ me?: string;
31
+ }
32
+ interface ResolvedAuthConfig {
33
+ mode: AuthMode;
34
+ key: string;
35
+ provider: string | null;
36
+ source: string | null;
37
+ strategy: AuthStrategy;
38
+ loginUrl: string | null;
39
+ /** Optional prefix used to resolve relative endpoint paths. */
40
+ baseUrl: string | null;
41
+ endpoints: Required<AuthEndpoints>;
42
+ apiBase: string;
43
+ /** Site origin from __PINOOX__.url.SITE (for Vite HMR absolute redirects). */
44
+ siteOrigin: string | null;
45
+ /** App path prefix e.g. /account — blocked as post-login return target. */
46
+ appPath: string | null;
47
+ debug: boolean;
48
+ }
49
+ interface LoginCredentials {
50
+ username?: string;
51
+ email?: string;
52
+ mobile?: string;
53
+ password: string;
54
+ remember?: boolean;
55
+ [key: string]: unknown;
56
+ }
57
+ interface AuthUser {
58
+ [key: string]: unknown;
59
+ }
60
+ interface LoginResult {
61
+ token: string | null;
62
+ user: AuthUser | null;
63
+ raw: unknown;
64
+ }
65
+ interface AuthRequestAuth {
66
+ header: string | null;
67
+ credentials: RequestCredentials;
68
+ mode: AuthMode;
69
+ key: string;
70
+ }
71
+ interface AuthLogEvent {
72
+ level: 'debug' | 'info' | 'warn' | 'error';
73
+ code: string;
74
+ message: string;
75
+ context?: Record<string, unknown>;
76
+ }
77
+ type AuthEventHandler = (payload?: unknown) => void;
78
+ type AuthLogSink = (event: AuthLogEvent) => void;
79
+
80
+ interface AuthLogger {
81
+ emit: (event: AuthLogEvent) => void;
82
+ debug: (code: string, message: string, context?: Record<string, unknown>) => void;
83
+ info: (code: string, message: string, context?: Record<string, unknown>) => void;
84
+ warn: (code: string, message: string, context?: Record<string, unknown>) => void;
85
+ error: (code: string, message: string, context?: Record<string, unknown>) => void;
86
+ setSink: (sink: AuthLogSink | null) => void;
87
+ }
88
+ declare function createLogger(debug?: boolean, sink?: AuthLogSink | null): AuthLogger;
89
+
90
+ interface CreateAuthOptions {
91
+ mode?: AuthMode;
92
+ key?: string;
93
+ strategy?: AuthStrategy;
94
+ loginUrl?: string;
95
+ /** Prefix for relative endpoint paths (e.g. `/account/api/v1`). */
96
+ baseUrl?: string | null;
97
+ endpoints?: AuthEndpoints;
98
+ apiBase?: string;
99
+ /** Override site origin (defaults to __PINOOX__.url.SITE). */
100
+ siteOrigin?: string | null;
101
+ debug?: boolean;
102
+ boot?: PinooxBootstrap | null;
103
+ }
104
+ /** Join base + relative path. Absolute http(s) or leading-/ paths pass through. */
105
+ declare function joinUrl(base: string, path: string): string;
106
+ /**
107
+ * Resolve an endpoint against optional baseUrl.
108
+ * - absolute URL or site path (`/…`) → unchanged
109
+ * - relative (`auth/get`) + baseUrl → joined
110
+ * - relative without baseUrl → returned as-is (caller should prefer absolute defaults)
111
+ */
112
+ declare function resolveEndpoint(path: string | undefined, fallback: string, baseUrl?: string | null): string;
113
+ declare function resolveConfig(options?: CreateAuthOptions, logger?: AuthLogger): ResolvedAuthConfig;
114
+
115
+ interface HttpRequestInput {
116
+ url: string;
117
+ method?: string;
118
+ headers?: Record<string, string>;
119
+ body?: unknown;
120
+ credentials?: RequestCredentials;
121
+ }
122
+ interface HttpResponse<T = unknown> {
123
+ status: number;
124
+ data: T;
125
+ headers: Headers;
126
+ ok: boolean;
127
+ }
128
+ interface HttpClient {
129
+ request: <T = unknown>(input: HttpRequestInput) => Promise<HttpResponse<T>>;
130
+ }
131
+ declare function createFetchProvider(): HttpClient;
132
+
133
+ interface AuthInstance {
134
+ readonly config: ResolvedAuthConfig;
135
+ readonly logger: AuthLogger;
136
+ user: AuthUser | null;
137
+ token: string | null;
138
+ isAuthenticated: boolean;
139
+ login: (credentials?: LoginCredentials) => Promise<LoginResult | void>;
140
+ logout: () => Promise<void>;
141
+ me: () => Promise<AuthUser | null>;
142
+ getToken: () => string | null;
143
+ setToken: (token: string | null) => void;
144
+ clearToken: () => void;
145
+ getAuthHeader: () => string | null;
146
+ authorize: (headers?: Record<string, string>) => AuthRequestAuth & {
147
+ headers: Record<string, string>;
148
+ };
149
+ getRequestAuth: () => AuthRequestAuth;
150
+ setHttp: (client: HttpClient) => void;
151
+ loginFromResponse: (payload: unknown) => LoginResult;
152
+ redirectToLogin: (returnPath?: string) => void;
153
+ /** Safe path from ?redirect= (or query object). */
154
+ getReturnPath: (queryOrPath?: unknown, fallback?: string) => string;
155
+ /** Absolute return URL (uses SITE origin when Vite origin differs). */
156
+ getReturnUrl: (queryOrPath?: unknown, fallback?: string) => string;
157
+ /** Navigate to post-login return URL. */
158
+ redirectBack: (queryOrPath?: unknown, fallback?: string) => void;
159
+ /** `{ redirect }` for router-link query preservation. */
160
+ getRedirectQuery: (queryOrPath?: unknown, fallback?: string) => {
161
+ redirect: string;
162
+ };
163
+ /** Clear session and emit `unauthorized` (e.g. from HTTP 401). */
164
+ notifyUnauthorized: (payload?: unknown) => void;
165
+ on: (event: AuthEvent, handler: AuthEventHandler) => () => void;
166
+ off: (event: AuthEvent, handler: AuthEventHandler) => void;
167
+ }
168
+ interface CreateAuthConfig extends CreateAuthOptions {
169
+ http?: HttpClient;
170
+ logger?: AuthLogger;
171
+ }
172
+ declare function getAuth(): AuthInstance;
173
+ declare function createAuth(options?: CreateAuthConfig): AuthInstance;
174
+ declare function buildLoginRedirectUrl(returnPath?: string): string;
175
+
176
+ export { type AuthLogger as A, type BootAuthConfig as B, type CreateAuthConfig as C, type HttpClient as H, type LoginCredentials as L, type PinooxBootstrap as P, type ResolvedAuthConfig as R, type AuthInstance as a, type AuthEndpoints as b, type AuthEvent as c, type AuthLogEvent as d, type AuthMode as e, type AuthRequestAuth as f, type AuthStrategy as g, type AuthUser as h, type CreateAuthOptions as i, type HttpRequestInput as j, type HttpResponse as k, type LoginResult as l, buildLoginRedirectUrl as m, createAuth as n, createFetchProvider as o, createLogger as p, getAuth as q, joinUrl as r, resolveConfig as s, resolveEndpoint as t };