@quatrain/auth-supabase 1.2.3 → 1.2.4
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,9 +1,24 @@
|
|
|
1
1
|
import { User } from '@quatrain/backend';
|
|
2
2
|
import { AbstractAuthAdapter, AuthParameters } from '@quatrain/auth';
|
|
3
3
|
import { ApiMiddleware } from '@quatrain/api';
|
|
4
|
+
/**
|
|
5
|
+
* Authentication adapter implementing the Supabase SDK ecosystem.
|
|
6
|
+
* Acts as a centralized bridge handling signup, tokens, and middleware enforcement.
|
|
7
|
+
*/
|
|
4
8
|
export declare class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
5
9
|
protected _client: any;
|
|
10
|
+
/**
|
|
11
|
+
* Initializes a new instance securely with Supabase configuration keys.
|
|
12
|
+
*
|
|
13
|
+
* @param config - Must contain `supabaseUrl` and `supabaseKey`.
|
|
14
|
+
* @returns A constructed SupabaseAuthAdapter or null on invalid params.
|
|
15
|
+
*/
|
|
6
16
|
static factory(config: any): SupabaseAuthAdapter | null;
|
|
17
|
+
/**
|
|
18
|
+
* Express middleware capturing Bearer JWT tokens to execute auth verification.
|
|
19
|
+
*
|
|
20
|
+
* @returns The middleware function.
|
|
21
|
+
*/
|
|
7
22
|
middleware(): ApiMiddleware;
|
|
8
23
|
constructor(params?: AuthParameters);
|
|
9
24
|
/**
|
|
@@ -12,15 +27,63 @@ export declare class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
12
27
|
* @returns user unique id
|
|
13
28
|
*/
|
|
14
29
|
register(user: User, clearPassword?: string): Promise<any>;
|
|
30
|
+
/**
|
|
31
|
+
* Resolves a raw Bearer token via the Supabase Auth API (`getUser`).
|
|
32
|
+
*
|
|
33
|
+
* @param bearer - Raw JWT string.
|
|
34
|
+
* @returns The decoded user object.
|
|
35
|
+
* @throws {Error} If verification fails.
|
|
36
|
+
*/
|
|
15
37
|
getAuthToken(bearer: string): Promise<any>;
|
|
38
|
+
/**
|
|
39
|
+
* Obtains a new JWT access token by exchanging the persistent refresh token.
|
|
40
|
+
*
|
|
41
|
+
* @param refreshToken - The token.
|
|
42
|
+
* @returns Resolves the token packet.
|
|
43
|
+
*/
|
|
16
44
|
refreshToken(refreshToken: string): Promise<any>;
|
|
45
|
+
/**
|
|
46
|
+
* Instructs the Supabase client to destroy the current session.
|
|
47
|
+
*
|
|
48
|
+
* @param token - Target token.
|
|
49
|
+
*/
|
|
17
50
|
revokeAuthToken(token: string): Promise<false | undefined>;
|
|
51
|
+
/**
|
|
52
|
+
* Executes a direct login / session instantiation via `signInWithPassword`.
|
|
53
|
+
*
|
|
54
|
+
* @param login - Email string.
|
|
55
|
+
* @param password - Raw password.
|
|
56
|
+
* @returns Resolved user session.
|
|
57
|
+
*/
|
|
18
58
|
signup(login: string, password: string): Promise<false | {
|
|
19
59
|
user: any;
|
|
20
60
|
session: any;
|
|
21
61
|
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Disconnects and destroys the active session context.
|
|
64
|
+
*
|
|
65
|
+
* @returns True if successful.
|
|
66
|
+
*/
|
|
22
67
|
signout(): Promise<any>;
|
|
68
|
+
/**
|
|
69
|
+
* Modifies a Supabase Auth user record.
|
|
70
|
+
*
|
|
71
|
+
* @param user - Target user.
|
|
72
|
+
* @param updatable - The delta properties.
|
|
73
|
+
*/
|
|
23
74
|
update(user: User, updatable: any): Promise<any>;
|
|
75
|
+
/**
|
|
76
|
+
* (Unimplemented) Destroys the user context inside Supabase.
|
|
77
|
+
*
|
|
78
|
+
* @param user - Target user.
|
|
79
|
+
*/
|
|
24
80
|
delete(user: User): Promise<any>;
|
|
81
|
+
/**
|
|
82
|
+
* Merges specific attributes into the Supabase `user_metadata` field using the admin API.
|
|
83
|
+
*
|
|
84
|
+
* @param id - The target user ID.
|
|
85
|
+
* @param claims - The payload of new claims.
|
|
86
|
+
* @returns Resolved updated user wrapper.
|
|
87
|
+
*/
|
|
25
88
|
setCustomUserClaims(id: string, claims: any): Promise<any>;
|
|
26
89
|
}
|
|
@@ -47,12 +47,27 @@ const auth_1 = require("@quatrain/auth");
|
|
|
47
47
|
const supabase_js_1 = require("@supabase/supabase-js");
|
|
48
48
|
const nativeFetch = __importStar(require("node-fetch-native"));
|
|
49
49
|
// Create a single supabase client for interacting with your database
|
|
50
|
+
/**
|
|
51
|
+
* Authentication adapter implementing the Supabase SDK ecosystem.
|
|
52
|
+
* Acts as a centralized bridge handling signup, tokens, and middleware enforcement.
|
|
53
|
+
*/
|
|
50
54
|
class SupabaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
55
|
+
/**
|
|
56
|
+
* Initializes a new instance securely with Supabase configuration keys.
|
|
57
|
+
*
|
|
58
|
+
* @param config - Must contain `supabaseUrl` and `supabaseKey`.
|
|
59
|
+
* @returns A constructed SupabaseAuthAdapter or null on invalid params.
|
|
60
|
+
*/
|
|
51
61
|
static factory(config) {
|
|
52
62
|
if (!config.supabaseUrl || !config.supabaseKey)
|
|
53
63
|
return null;
|
|
54
64
|
return new SupabaseAuthAdapter({ config });
|
|
55
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Express middleware capturing Bearer JWT tokens to execute auth verification.
|
|
68
|
+
*
|
|
69
|
+
* @returns The middleware function.
|
|
70
|
+
*/
|
|
56
71
|
middleware() {
|
|
57
72
|
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
58
73
|
var _a;
|
|
@@ -113,6 +128,13 @@ class SupabaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
|
113
128
|
}
|
|
114
129
|
});
|
|
115
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Resolves a raw Bearer token via the Supabase Auth API (`getUser`).
|
|
133
|
+
*
|
|
134
|
+
* @param bearer - Raw JWT string.
|
|
135
|
+
* @returns The decoded user object.
|
|
136
|
+
* @throws {Error} If verification fails.
|
|
137
|
+
*/
|
|
116
138
|
getAuthToken(bearer) {
|
|
117
139
|
return __awaiter(this, void 0, void 0, function* () {
|
|
118
140
|
const token = yield this._client.auth.getUser(bearer);
|
|
@@ -122,6 +144,12 @@ class SupabaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
|
122
144
|
throw new Error('Unable to retrieve auth token from Supabase');
|
|
123
145
|
});
|
|
124
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Obtains a new JWT access token by exchanging the persistent refresh token.
|
|
149
|
+
*
|
|
150
|
+
* @param refreshToken - The token.
|
|
151
|
+
* @returns Resolves the token packet.
|
|
152
|
+
*/
|
|
125
153
|
refreshToken(refreshToken) {
|
|
126
154
|
return __awaiter(this, void 0, void 0, function* () {
|
|
127
155
|
const url = `${this._params.config.supabaseUrl}/auth/v1/token?grant_type=refresh_token`;
|
|
@@ -138,6 +166,11 @@ class SupabaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
|
138
166
|
return data;
|
|
139
167
|
});
|
|
140
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Instructs the Supabase client to destroy the current session.
|
|
171
|
+
*
|
|
172
|
+
* @param token - Target token.
|
|
173
|
+
*/
|
|
141
174
|
revokeAuthToken(token) {
|
|
142
175
|
return __awaiter(this, void 0, void 0, function* () {
|
|
143
176
|
// Careful, this only delete tokens on client side, not on server side
|
|
@@ -148,6 +181,13 @@ class SupabaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
|
148
181
|
}
|
|
149
182
|
});
|
|
150
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Executes a direct login / session instantiation via `signInWithPassword`.
|
|
186
|
+
*
|
|
187
|
+
* @param login - Email string.
|
|
188
|
+
* @param password - Raw password.
|
|
189
|
+
* @returns Resolved user session.
|
|
190
|
+
*/
|
|
151
191
|
signup(login, password) {
|
|
152
192
|
return __awaiter(this, void 0, void 0, function* () {
|
|
153
193
|
const { data, error } = yield this._client.auth.signInWithPassword({
|
|
@@ -161,6 +201,11 @@ class SupabaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
|
161
201
|
return { user: data.user, session: data.session };
|
|
162
202
|
});
|
|
163
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Disconnects and destroys the active session context.
|
|
206
|
+
*
|
|
207
|
+
* @returns True if successful.
|
|
208
|
+
*/
|
|
164
209
|
signout() {
|
|
165
210
|
return __awaiter(this, void 0, void 0, function* () {
|
|
166
211
|
const { error } = yield this._client.auth.signOut();
|
|
@@ -171,6 +216,12 @@ class SupabaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
|
171
216
|
return true;
|
|
172
217
|
});
|
|
173
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Modifies a Supabase Auth user record.
|
|
221
|
+
*
|
|
222
|
+
* @param user - Target user.
|
|
223
|
+
* @param updatable - The delta properties.
|
|
224
|
+
*/
|
|
174
225
|
update(user, updatable) {
|
|
175
226
|
return __awaiter(this, void 0, void 0, function* () {
|
|
176
227
|
auth_1.Auth.debug('auth data to update', JSON.stringify(updatable));
|
|
@@ -189,11 +240,23 @@ class SupabaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
|
189
240
|
}
|
|
190
241
|
});
|
|
191
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* (Unimplemented) Destroys the user context inside Supabase.
|
|
245
|
+
*
|
|
246
|
+
* @param user - Target user.
|
|
247
|
+
*/
|
|
192
248
|
delete(user) {
|
|
193
249
|
return __awaiter(this, void 0, void 0, function* () {
|
|
194
250
|
// return await getAuth().deleteUser(user.uid)
|
|
195
251
|
});
|
|
196
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Merges specific attributes into the Supabase `user_metadata` field using the admin API.
|
|
255
|
+
*
|
|
256
|
+
* @param id - The target user ID.
|
|
257
|
+
* @param claims - The payload of new claims.
|
|
258
|
+
* @returns Resolved updated user wrapper.
|
|
259
|
+
*/
|
|
197
260
|
setCustomUserClaims(id, claims) {
|
|
198
261
|
return __awaiter(this, void 0, void 0, function* () {
|
|
199
262
|
auth_1.Auth.debug(`Updating user ${id} with claims ${JSON.stringify(claims)}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quatrain/auth-supabase",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
},
|
|
20
20
|
"author": "Quatrain Développement SAS <developers@quatrain.com>",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@quatrain/api": "^1.1.
|
|
23
|
-
"@quatrain/auth": "^1.2.
|
|
22
|
+
"@quatrain/api": "^1.1.5",
|
|
23
|
+
"@quatrain/auth": "^1.2.2",
|
|
24
24
|
"@quatrain/backend": "^1.2.6",
|
|
25
25
|
"@supabase/supabase-js": "^2.87.3",
|
|
26
26
|
"node-fetch-native": "^1.6.4"
|
|
@@ -10,14 +10,29 @@ import { createClient } from '@supabase/supabase-js'
|
|
|
10
10
|
import * as nativeFetch from 'node-fetch-native'
|
|
11
11
|
|
|
12
12
|
// Create a single supabase client for interacting with your database
|
|
13
|
+
/**
|
|
14
|
+
* Authentication adapter implementing the Supabase SDK ecosystem.
|
|
15
|
+
* Acts as a centralized bridge handling signup, tokens, and middleware enforcement.
|
|
16
|
+
*/
|
|
13
17
|
export class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
14
18
|
protected _client: any
|
|
15
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Initializes a new instance securely with Supabase configuration keys.
|
|
22
|
+
*
|
|
23
|
+
* @param config - Must contain `supabaseUrl` and `supabaseKey`.
|
|
24
|
+
* @returns A constructed SupabaseAuthAdapter or null on invalid params.
|
|
25
|
+
*/
|
|
16
26
|
static factory(config: any): SupabaseAuthAdapter | null {
|
|
17
27
|
if (!config.supabaseUrl || !config.supabaseKey) return null
|
|
18
28
|
return new SupabaseAuthAdapter({ config })
|
|
19
29
|
}
|
|
20
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Express middleware capturing Bearer JWT tokens to execute auth verification.
|
|
33
|
+
*
|
|
34
|
+
* @returns The middleware function.
|
|
35
|
+
*/
|
|
21
36
|
public middleware(): ApiMiddleware {
|
|
22
37
|
return async (req: ApiRequest, res: ApiResponse): Promise<boolean> => {
|
|
23
38
|
const bearer = ((req.headers?.authorization as string) || '').split(' ')[1] || ''
|
|
@@ -90,6 +105,13 @@ export class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
90
105
|
}
|
|
91
106
|
}
|
|
92
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Resolves a raw Bearer token via the Supabase Auth API (`getUser`).
|
|
110
|
+
*
|
|
111
|
+
* @param bearer - Raw JWT string.
|
|
112
|
+
* @returns The decoded user object.
|
|
113
|
+
* @throws {Error} If verification fails.
|
|
114
|
+
*/
|
|
93
115
|
async getAuthToken(bearer: string) {
|
|
94
116
|
const token = await this._client.auth.getUser(bearer)
|
|
95
117
|
if (token.data && token.data.user) {
|
|
@@ -98,6 +120,12 @@ export class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
98
120
|
throw new Error('Unable to retrieve auth token from Supabase')
|
|
99
121
|
}
|
|
100
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Obtains a new JWT access token by exchanging the persistent refresh token.
|
|
125
|
+
*
|
|
126
|
+
* @param refreshToken - The token.
|
|
127
|
+
* @returns Resolves the token packet.
|
|
128
|
+
*/
|
|
101
129
|
async refreshToken(refreshToken: string) {
|
|
102
130
|
const url = `${this._params.config.supabaseUrl}/auth/v1/token?grant_type=refresh_token`
|
|
103
131
|
const response = await nativeFetch.fetch(url, {
|
|
@@ -115,6 +143,11 @@ export class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
115
143
|
return data
|
|
116
144
|
}
|
|
117
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Instructs the Supabase client to destroy the current session.
|
|
148
|
+
*
|
|
149
|
+
* @param token - Target token.
|
|
150
|
+
*/
|
|
118
151
|
async revokeAuthToken(token: string) {
|
|
119
152
|
// Careful, this only delete tokens on client side, not on server side
|
|
120
153
|
const { error } = await this.signout()
|
|
@@ -124,6 +157,13 @@ export class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
124
157
|
}
|
|
125
158
|
}
|
|
126
159
|
|
|
160
|
+
/**
|
|
161
|
+
* Executes a direct login / session instantiation via `signInWithPassword`.
|
|
162
|
+
*
|
|
163
|
+
* @param login - Email string.
|
|
164
|
+
* @param password - Raw password.
|
|
165
|
+
* @returns Resolved user session.
|
|
166
|
+
*/
|
|
127
167
|
async signup(login: string, password: string) {
|
|
128
168
|
const { data, error } = await this._client.auth.signInWithPassword({
|
|
129
169
|
email: login,
|
|
@@ -138,6 +178,11 @@ export class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
138
178
|
return { user: data.user, session: data.session }
|
|
139
179
|
}
|
|
140
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Disconnects and destroys the active session context.
|
|
183
|
+
*
|
|
184
|
+
* @returns True if successful.
|
|
185
|
+
*/
|
|
141
186
|
async signout(): Promise<any> {
|
|
142
187
|
const { error } = await this._client.auth.signOut()
|
|
143
188
|
if (error !== null) {
|
|
@@ -147,6 +192,12 @@ export class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
147
192
|
return true
|
|
148
193
|
}
|
|
149
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Modifies a Supabase Auth user record.
|
|
197
|
+
*
|
|
198
|
+
* @param user - Target user.
|
|
199
|
+
* @param updatable - The delta properties.
|
|
200
|
+
*/
|
|
150
201
|
async update(user: User, updatable: any): Promise<any> {
|
|
151
202
|
Auth.debug('auth data to update', JSON.stringify(updatable))
|
|
152
203
|
|
|
@@ -167,10 +218,22 @@ export class SupabaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
167
218
|
}
|
|
168
219
|
}
|
|
169
220
|
|
|
221
|
+
/**
|
|
222
|
+
* (Unimplemented) Destroys the user context inside Supabase.
|
|
223
|
+
*
|
|
224
|
+
* @param user - Target user.
|
|
225
|
+
*/
|
|
170
226
|
async delete(user: User): Promise<any> {
|
|
171
227
|
// return await getAuth().deleteUser(user.uid)
|
|
172
228
|
}
|
|
173
229
|
|
|
230
|
+
/**
|
|
231
|
+
* Merges specific attributes into the Supabase `user_metadata` field using the admin API.
|
|
232
|
+
*
|
|
233
|
+
* @param id - The target user ID.
|
|
234
|
+
* @param claims - The payload of new claims.
|
|
235
|
+
* @returns Resolved updated user wrapper.
|
|
236
|
+
*/
|
|
174
237
|
async setCustomUserClaims(id: string, claims: any) {
|
|
175
238
|
Auth.debug(`Updating user ${id} with claims ${JSON.stringify(claims)}`)
|
|
176
239
|
const { data, error } = await this._client.auth.admin.updateUserById(id, {
|