@quatrain/auth-supabase 1.2.2 → 1.2.3

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,7 +1,10 @@
1
1
  import { User } from '@quatrain/backend';
2
2
  import { AbstractAuthAdapter, AuthParameters } from '@quatrain/auth';
3
+ import { ApiMiddleware } from '@quatrain/api';
3
4
  export declare class SupabaseAuthAdapter extends AbstractAuthAdapter {
4
5
  protected _client: any;
6
+ static factory(config: any): SupabaseAuthAdapter | null;
7
+ middleware(): ApiMiddleware;
5
8
  constructor(params?: AuthParameters);
6
9
  /**
7
10
  * Register new user in authentication
@@ -48,6 +48,31 @@ 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
50
  class SupabaseAuthAdapter extends auth_1.AbstractAuthAdapter {
51
+ static factory(config) {
52
+ if (!config.supabaseUrl || !config.supabaseKey)
53
+ return null;
54
+ return new SupabaseAuthAdapter({ config });
55
+ }
56
+ middleware() {
57
+ return (req, res) => __awaiter(this, void 0, void 0, function* () {
58
+ var _a;
59
+ const bearer = (((_a = req.headers) === null || _a === void 0 ? void 0 : _a.authorization) || '').split(' ')[1] || '';
60
+ if (bearer) {
61
+ try {
62
+ const user = yield this.getAuthToken(bearer);
63
+ if (user) {
64
+ return true; // Authorized
65
+ }
66
+ }
67
+ catch (e) {
68
+ auth_1.Auth.error(`[SupabaseAuthAdapter] Middleware token verification failed: ${e.message}`);
69
+ }
70
+ }
71
+ res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"');
72
+ res.status(401).send('Authentication required.');
73
+ return false;
74
+ });
75
+ }
51
76
  constructor(params = {}) {
52
77
  super(params);
53
78
  this._client = (0, supabase_js_1.createClient)(params.config.supabaseUrl, params.config.supabaseKey, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/auth-supabase",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "license": "AGPL-3.0-only",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,8 +19,9 @@
19
19
  },
20
20
  "author": "Quatrain Développement SAS <developers@quatrain.com>",
21
21
  "dependencies": {
22
+ "@quatrain/api": "^1.1.4",
22
23
  "@quatrain/auth": "^1.2.1",
23
- "@quatrain/backend": "^1.2.5",
24
+ "@quatrain/backend": "^1.2.6",
24
25
  "@supabase/supabase-js": "^2.87.3",
25
26
  "node-fetch-native": "^1.6.4"
26
27
  },
@@ -5,6 +5,7 @@ import {
5
5
  AuthenticationError,
6
6
  AuthParameters,
7
7
  } from '@quatrain/auth'
8
+ import { ApiMiddleware, ApiRequest, ApiResponse } from '@quatrain/api'
8
9
  import { createClient } from '@supabase/supabase-js'
9
10
  import * as nativeFetch from 'node-fetch-native'
10
11
 
@@ -12,6 +13,32 @@ import * as nativeFetch from 'node-fetch-native'
12
13
  export class SupabaseAuthAdapter extends AbstractAuthAdapter {
13
14
  protected _client: any
14
15
 
16
+ static factory(config: any): SupabaseAuthAdapter | null {
17
+ if (!config.supabaseUrl || !config.supabaseKey) return null
18
+ return new SupabaseAuthAdapter({ config })
19
+ }
20
+
21
+ public middleware(): ApiMiddleware {
22
+ return async (req: ApiRequest, res: ApiResponse): Promise<boolean> => {
23
+ const bearer = ((req.headers?.authorization as string) || '').split(' ')[1] || ''
24
+
25
+ if (bearer) {
26
+ try {
27
+ const user = await this.getAuthToken(bearer)
28
+ if (user) {
29
+ return true // Authorized
30
+ }
31
+ } catch(e) {
32
+ Auth.error(`[SupabaseAuthAdapter] Middleware token verification failed: ${(e as Error).message}`)
33
+ }
34
+ }
35
+
36
+ res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"')
37
+ res.status(401).send('Authentication required.')
38
+ return false
39
+ }
40
+ }
41
+
15
42
  constructor(params: AuthParameters = {}) {
16
43
  super(params)
17
44
  this._client = createClient(