@redmix/auth-supabase-middleware 0.0.1
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/LICENSE +21 -0
- package/README.md +129 -0
- package/dist/cjs/defaultGetRoles.d.ts +41 -0
- package/dist/cjs/defaultGetRoles.d.ts.map +1 -0
- package/dist/cjs/defaultGetRoles.js +39 -0
- package/dist/cjs/index.d.ts +12 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +74 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/util.d.ts +16 -0
- package/dist/cjs/util.d.ts.map +1 -0
- package/dist/cjs/util.js +72 -0
- package/dist/defaultGetRoles.d.ts +41 -0
- package/dist/defaultGetRoles.d.ts.map +1 -0
- package/dist/defaultGetRoles.js +15 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/util.d.ts +16 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +47 -0
- package/package.json +63 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Redmix
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
# Supabase Middleware
|
2
|
+
|
3
|
+
```tsx filename='entry.server.tsx'
|
4
|
+
import type { TagDescriptor } from '@redmix/web'
|
5
|
+
|
6
|
+
import App from './App'
|
7
|
+
import initSupabaseMiddleware from '@redmix/auth-supabase-middleware'
|
8
|
+
import { Document } from './Document'
|
9
|
+
|
10
|
+
import { getCurrentUser } from '$api/src/lib/auth'
|
11
|
+
|
12
|
+
interface Props {
|
13
|
+
css: string[]
|
14
|
+
meta?: TagDescriptor[]
|
15
|
+
}
|
16
|
+
|
17
|
+
export const registerMiddleware = () => {
|
18
|
+
const supabaseAuthMiddleware = initSupabaseMiddleware({
|
19
|
+
// Optional. If not set, Supabase will use its own `currentUser` function
|
20
|
+
// instead of your app's
|
21
|
+
getCurrentUser,
|
22
|
+
// Optional. If you wish to enforce RBAC, define a function to return roles.
|
23
|
+
// Typically, one will define roles in Supabase in the user's app_metadata.
|
24
|
+
getRoles,
|
25
|
+
})
|
26
|
+
|
27
|
+
return [supabaseAuthMiddleware]
|
28
|
+
}
|
29
|
+
|
30
|
+
export const ServerEntry: React.FC<Props> = ({ css, meta }) => {
|
31
|
+
return (
|
32
|
+
<Document css={css} meta={meta}>
|
33
|
+
<App />
|
34
|
+
</Document>
|
35
|
+
)
|
36
|
+
}
|
37
|
+
```
|
38
|
+
|
39
|
+
## About Roles
|
40
|
+
|
41
|
+
### How To Set Roles in Supabase
|
42
|
+
|
43
|
+
Typically, one will define roles in Supabase in the user's `app_metadata`.
|
44
|
+
|
45
|
+
Supabase `app_metadata` includes the provider attribute indicates the first provider that the user used to sign up with. The providers attribute indicates the list of providers that the user can use to login with.
|
46
|
+
|
47
|
+
You can add information to `app_metadata` via `SQL`:
|
48
|
+
|
49
|
+
You can set a single role:
|
50
|
+
|
51
|
+
```sql
|
52
|
+
update AUTH.users
|
53
|
+
set raw_app_meta_data = raw_app_meta_data || '{"roles": "admin"}'
|
54
|
+
where
|
55
|
+
id = '11111111-1111-1111-1111-111111111111';
|
56
|
+
```
|
57
|
+
|
58
|
+
Or multiple roles:
|
59
|
+
|
60
|
+
```sql
|
61
|
+
update AUTH.users
|
62
|
+
set raw_app_meta_data = raw_app_meta_data || '{"roles": ["admin", "owner"]}'
|
63
|
+
where
|
64
|
+
id = '11111111-1111-1111-1111-111111111111';
|
65
|
+
```
|
66
|
+
|
67
|
+
Alternatively, you can update the user's `app_metadata` via the [Auth Admin `update user` api](https://supabase.com/docs/reference/javascript/auth-admin-updateuserbyid). Only a service role api request can modify the user app_metadata.
|
68
|
+
|
69
|
+
> A custom data object to store the user's application specific metadata. This maps to the `auth.users.app_metadata` column. Only a service role can modify. The `app_metadata` should be a JSON object that includes app-specific info, such as identity providers, roles, and other access control information.
|
70
|
+
|
71
|
+
```ts
|
72
|
+
const { data: user, error } = await supabase.auth.admin.updateUserById(
|
73
|
+
'11111111-1111-1111-1111-111111111111',
|
74
|
+
{ app_metadata: { roles: ['admin', 'owner'] } },
|
75
|
+
)
|
76
|
+
```
|
77
|
+
|
78
|
+
Note: You may see a `role` attribute on the Supabase user. This is an internal claim used by Postgres to perform Row Level Security (RLS) checks.
|
79
|
+
|
80
|
+
### What is the default implementation?
|
81
|
+
|
82
|
+
If you do not supply a `getRoles` function, we look in the `app_metadata.roles` property.
|
83
|
+
|
84
|
+
If you only had a string, e.g.
|
85
|
+
|
86
|
+
```
|
87
|
+
{
|
88
|
+
app_metadata: {
|
89
|
+
provider: 'email',
|
90
|
+
providers: ['email'],
|
91
|
+
roles: 'admin', // <-- ⭐ notice this is a string
|
92
|
+
},
|
93
|
+
user_metadata: {
|
94
|
+
...
|
95
|
+
}
|
96
|
+
```
|
97
|
+
|
98
|
+
it will convert the roles here to `['admin']`.
|
99
|
+
|
100
|
+
If you place your roles somewhere else, you will need to provide an implementation of the `getRoles` function. e.g.
|
101
|
+
|
102
|
+
```
|
103
|
+
{
|
104
|
+
app_metadata: {
|
105
|
+
provider: 'email',
|
106
|
+
providers: ['email'],
|
107
|
+
organization: {
|
108
|
+
name: 'acme',
|
109
|
+
userRoles: ['admin']
|
110
|
+
}
|
111
|
+
},
|
112
|
+
user_metadata: {
|
113
|
+
...
|
114
|
+
}
|
115
|
+
```
|
116
|
+
|
117
|
+
```js
|
118
|
+
// In entry.server.jsx
|
119
|
+
export const registerMiddleware = () => {
|
120
|
+
const supabaseAuthMiddleware = initSupabaseMiddleware({
|
121
|
+
// Customise where you get your roles from
|
122
|
+
getRoles: (decoded) => {
|
123
|
+
return decoded.app_metadata.organization?.userRoles
|
124
|
+
},
|
125
|
+
})
|
126
|
+
|
127
|
+
return [supabaseAuthMiddleware]
|
128
|
+
}
|
129
|
+
```
|
@@ -0,0 +1,41 @@
|
|
1
|
+
/**
|
2
|
+
* Currently the supabase auth decoder returns something like this:
|
3
|
+
{
|
4
|
+
"aud": "authenticated",
|
5
|
+
"exp": 1716806712,
|
6
|
+
"iat": 1716803112,
|
7
|
+
"iss": "https://bubnfbrfzfdryapcuybr.supabase.co/auth/v1",
|
8
|
+
"sub": "75fd8091-e0a7-4e7d-8a8d-138d0eb3ca5a",
|
9
|
+
"email": "dannychoudhury+1@gmail.com",
|
10
|
+
"phone": "",
|
11
|
+
"app_metadata": {
|
12
|
+
"provider": "email",
|
13
|
+
"providers": [
|
14
|
+
"email"
|
15
|
+
],
|
16
|
+
"roles": "admin" <-- this the role we're looking for
|
17
|
+
},
|
18
|
+
"user_metadata": {
|
19
|
+
"full-name": "Danny Choudhury 1"
|
20
|
+
},
|
21
|
+
"role": "authenticated", <-- this is the role supabase sets
|
22
|
+
"aal": "aal1",
|
23
|
+
"amr": [
|
24
|
+
{
|
25
|
+
"method": "password",
|
26
|
+
"timestamp": 1716803107
|
27
|
+
}
|
28
|
+
],
|
29
|
+
"session_id": "39b4ae31-c57a-4ac1-8f01-e9d6ccbd9365",
|
30
|
+
"is_anonymous": false
|
31
|
+
}
|
32
|
+
*/
|
33
|
+
interface PartialSupabaseDecoded {
|
34
|
+
app_metadata: {
|
35
|
+
[key: string]: unknown;
|
36
|
+
roles?: string | undefined;
|
37
|
+
};
|
38
|
+
}
|
39
|
+
export declare const defaultGetRoles: (decoded: PartialSupabaseDecoded) => string[];
|
40
|
+
export {};
|
41
|
+
//# sourceMappingURL=defaultGetRoles.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"defaultGetRoles.d.ts","sourceRoot":"","sources":["../../src/defaultGetRoles.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,UAAU,sBAAsB;IAC9B,YAAY,EAAE;QACZ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;QACtB,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAC3B,CAAA;CACF;AAED,eAAO,MAAM,eAAe,YAAa,sBAAsB,KAAG,MAAM,EAYvE,CAAA"}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var defaultGetRoles_exports = {};
|
20
|
+
__export(defaultGetRoles_exports, {
|
21
|
+
defaultGetRoles: () => defaultGetRoles
|
22
|
+
});
|
23
|
+
module.exports = __toCommonJS(defaultGetRoles_exports);
|
24
|
+
const defaultGetRoles = (decoded) => {
|
25
|
+
try {
|
26
|
+
const roles = decoded?.app_metadata?.roles;
|
27
|
+
if (Array.isArray(roles)) {
|
28
|
+
return roles;
|
29
|
+
} else {
|
30
|
+
return roles ? [roles] : [];
|
31
|
+
}
|
32
|
+
} catch {
|
33
|
+
return [];
|
34
|
+
}
|
35
|
+
};
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
37
|
+
0 && (module.exports = {
|
38
|
+
defaultGetRoles
|
39
|
+
});
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { GetCurrentUser } from '@redmix/graphql-server';
|
2
|
+
import type { Middleware } from '@redmix/web/middleware';
|
3
|
+
export interface SupabaseAuthMiddlewareOptions {
|
4
|
+
getCurrentUser: GetCurrentUser;
|
5
|
+
getRoles?: (decoded: any) => string[];
|
6
|
+
}
|
7
|
+
/**
|
8
|
+
* Create Supabase Auth Middleware that sets the `serverAuthState` based on the Supabase cookie.
|
9
|
+
*/
|
10
|
+
declare const initSupabaseAuthMiddleware: ({ getCurrentUser, getRoles, }: SupabaseAuthMiddlewareOptions) => [Middleware, "*"];
|
11
|
+
export default initSupabaseAuthMiddleware;
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAC5D,OAAO,KAAK,EACV,UAAU,EAGX,MAAM,wBAAwB,CAAA;AAI/B,MAAM,WAAW,6BAA6B;IAC5C,cAAc,EAAE,cAAc,CAAA;IAC9B,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,MAAM,EAAE,CAAA;CACtC;AAED;;GAEG;AACH,QAAA,MAAM,0BAA0B,kCAG7B,6BAA6B,KAAG,CAAC,UAAU,EAAE,GAAG,CAmElD,CAAA;AACD,eAAe,0BAA0B,CAAA"}
|
@@ -0,0 +1,74 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var index_exports = {};
|
20
|
+
__export(index_exports, {
|
21
|
+
default: () => index_default
|
22
|
+
});
|
23
|
+
module.exports = __toCommonJS(index_exports);
|
24
|
+
var import_api = require("@redmix/api");
|
25
|
+
var import_auth_supabase_api = require("@redmix/auth-supabase-api");
|
26
|
+
var import_util = require("./util.js");
|
27
|
+
const initSupabaseAuthMiddleware = ({
|
28
|
+
getCurrentUser,
|
29
|
+
getRoles
|
30
|
+
}) => {
|
31
|
+
const middleware = async (req, res) => {
|
32
|
+
const type = "supabase";
|
33
|
+
const cookieHeader = req.headers.get("cookie");
|
34
|
+
if (!cookieHeader) {
|
35
|
+
return res;
|
36
|
+
}
|
37
|
+
try {
|
38
|
+
const authProviderCookie = req.cookies.get(import_api.AUTH_PROVIDER_HEADER);
|
39
|
+
if (!authProviderCookie || authProviderCookie !== type) {
|
40
|
+
return res;
|
41
|
+
}
|
42
|
+
const decoded = await (0, import_auth_supabase_api.authDecoder)(cookieHeader, type, {
|
43
|
+
event: req
|
44
|
+
});
|
45
|
+
const currentUser = await getCurrentUser(
|
46
|
+
decoded,
|
47
|
+
{ type, token: cookieHeader, schema: "cookie" },
|
48
|
+
{ event: req }
|
49
|
+
);
|
50
|
+
if (req.url.includes(`/middleware/supabase/currentUser`)) {
|
51
|
+
res.body = // Not sure how currentUser can be string.... but types say so
|
52
|
+
typeof currentUser === "string" ? currentUser : JSON.stringify({ currentUser });
|
53
|
+
return res;
|
54
|
+
}
|
55
|
+
const userMetadata = typeof currentUser === "string" ? null : currentUser?.["user_metadata"];
|
56
|
+
req.serverAuthState.set({
|
57
|
+
currentUser,
|
58
|
+
loading: false,
|
59
|
+
isAuthenticated: !!currentUser,
|
60
|
+
hasError: false,
|
61
|
+
userMetadata: userMetadata || currentUser,
|
62
|
+
cookieHeader,
|
63
|
+
roles: getRoles ? getRoles(decoded) : []
|
64
|
+
});
|
65
|
+
} catch (e) {
|
66
|
+
console.error(e, "Error in Supabase Auth Middleware");
|
67
|
+
(0, import_util.clearAuthState)(req, res);
|
68
|
+
return res;
|
69
|
+
}
|
70
|
+
return res;
|
71
|
+
};
|
72
|
+
return [middleware, "*"];
|
73
|
+
};
|
74
|
+
var index_default = initSupabaseAuthMiddleware;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"type":"commonjs"}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import type { SupabaseClient } from '@supabase/supabase-js';
|
2
|
+
import type { MiddlewareRequest, MiddlewareResponse } from '@redmix/web/middleware';
|
3
|
+
/**
|
4
|
+
* Creates Supabase Server Client used to get the session cookie (only)
|
5
|
+
* from a given collection of auth cookies
|
6
|
+
*/
|
7
|
+
export declare const createSupabaseServerClient: (req: MiddlewareRequest, res: MiddlewareResponse) => {
|
8
|
+
cookieName: string | null;
|
9
|
+
supabase: SupabaseClient;
|
10
|
+
};
|
11
|
+
/**
|
12
|
+
* Clear the Supabase and auth cookies from the request and response
|
13
|
+
* and clear the auth context
|
14
|
+
*/
|
15
|
+
export declare const clearAuthState: (req: MiddlewareRequest, res: MiddlewareResponse) => void;
|
16
|
+
//# sourceMappingURL=util.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAI3D,OAAO,KAAK,EACV,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,wBAAwB,CAAA;AAC/B;;;GAGG;AACH,eAAO,MAAM,0BAA0B,QAChC,iBAAiB,OACjB,kBAAkB,KACtB;IAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,QAAQ,EAAE,cAAc,CAAA;CAmCvD,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,QACpB,iBAAiB,OACjB,kBAAkB,SAgBxB,CAAA"}
|
package/dist/cjs/util.js
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var util_exports = {};
|
20
|
+
__export(util_exports, {
|
21
|
+
clearAuthState: () => clearAuthState,
|
22
|
+
createSupabaseServerClient: () => createSupabaseServerClient
|
23
|
+
});
|
24
|
+
module.exports = __toCommonJS(util_exports);
|
25
|
+
var import_ssr = require("@supabase/ssr");
|
26
|
+
var import_api = require("@redmix/api");
|
27
|
+
var import_auth_supabase_api = require("@redmix/auth-supabase-api");
|
28
|
+
const createSupabaseServerClient = (req, res) => {
|
29
|
+
let cookieName = null;
|
30
|
+
if (!process.env.SUPABASE_URL) {
|
31
|
+
(0, import_auth_supabase_api.throwSupabaseSettingsError)("SUPABASE_URL");
|
32
|
+
}
|
33
|
+
if (!process.env.SUPABASE_KEY) {
|
34
|
+
(0, import_auth_supabase_api.throwSupabaseSettingsError)("SUPABASE_KEY");
|
35
|
+
}
|
36
|
+
const supabase = (0, import_ssr.createServerClient)(
|
37
|
+
process.env.SUPABASE_URL || "",
|
38
|
+
process.env.SUPABASE_KEY || "",
|
39
|
+
{
|
40
|
+
cookies: {
|
41
|
+
get(name) {
|
42
|
+
cookieName = name;
|
43
|
+
return req.cookies.get(name)?.valueOf();
|
44
|
+
},
|
45
|
+
set(name, value, options) {
|
46
|
+
cookieName = name;
|
47
|
+
req.cookies.set(name, value, options);
|
48
|
+
res.cookies.set(name, value, options);
|
49
|
+
},
|
50
|
+
remove(name, options) {
|
51
|
+
cookieName = name;
|
52
|
+
req.cookies.set(name, "", options);
|
53
|
+
res.cookies.set(name, "", options);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
);
|
58
|
+
return { cookieName, supabase };
|
59
|
+
};
|
60
|
+
const clearAuthState = (req, res) => {
|
61
|
+
req.serverAuthState.clear();
|
62
|
+
const { cookieName } = createSupabaseServerClient(req, res);
|
63
|
+
if (cookieName) {
|
64
|
+
res.cookies.unset(cookieName);
|
65
|
+
}
|
66
|
+
res.cookies.unset(import_api.AUTH_PROVIDER_HEADER);
|
67
|
+
};
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
69
|
+
0 && (module.exports = {
|
70
|
+
clearAuthState,
|
71
|
+
createSupabaseServerClient
|
72
|
+
});
|
@@ -0,0 +1,41 @@
|
|
1
|
+
/**
|
2
|
+
* Currently the supabase auth decoder returns something like this:
|
3
|
+
{
|
4
|
+
"aud": "authenticated",
|
5
|
+
"exp": 1716806712,
|
6
|
+
"iat": 1716803112,
|
7
|
+
"iss": "https://bubnfbrfzfdryapcuybr.supabase.co/auth/v1",
|
8
|
+
"sub": "75fd8091-e0a7-4e7d-8a8d-138d0eb3ca5a",
|
9
|
+
"email": "dannychoudhury+1@gmail.com",
|
10
|
+
"phone": "",
|
11
|
+
"app_metadata": {
|
12
|
+
"provider": "email",
|
13
|
+
"providers": [
|
14
|
+
"email"
|
15
|
+
],
|
16
|
+
"roles": "admin" <-- this the role we're looking for
|
17
|
+
},
|
18
|
+
"user_metadata": {
|
19
|
+
"full-name": "Danny Choudhury 1"
|
20
|
+
},
|
21
|
+
"role": "authenticated", <-- this is the role supabase sets
|
22
|
+
"aal": "aal1",
|
23
|
+
"amr": [
|
24
|
+
{
|
25
|
+
"method": "password",
|
26
|
+
"timestamp": 1716803107
|
27
|
+
}
|
28
|
+
],
|
29
|
+
"session_id": "39b4ae31-c57a-4ac1-8f01-e9d6ccbd9365",
|
30
|
+
"is_anonymous": false
|
31
|
+
}
|
32
|
+
*/
|
33
|
+
interface PartialSupabaseDecoded {
|
34
|
+
app_metadata: {
|
35
|
+
[key: string]: unknown;
|
36
|
+
roles?: string | undefined;
|
37
|
+
};
|
38
|
+
}
|
39
|
+
export declare const defaultGetRoles: (decoded: PartialSupabaseDecoded) => string[];
|
40
|
+
export {};
|
41
|
+
//# sourceMappingURL=defaultGetRoles.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"defaultGetRoles.d.ts","sourceRoot":"","sources":["../src/defaultGetRoles.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,UAAU,sBAAsB;IAC9B,YAAY,EAAE;QACZ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;QACtB,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAC3B,CAAA;CACF;AAED,eAAO,MAAM,eAAe,YAAa,sBAAsB,KAAG,MAAM,EAYvE,CAAA"}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
const defaultGetRoles = (decoded) => {
|
2
|
+
try {
|
3
|
+
const roles = decoded?.app_metadata?.roles;
|
4
|
+
if (Array.isArray(roles)) {
|
5
|
+
return roles;
|
6
|
+
} else {
|
7
|
+
return roles ? [roles] : [];
|
8
|
+
}
|
9
|
+
} catch {
|
10
|
+
return [];
|
11
|
+
}
|
12
|
+
};
|
13
|
+
export {
|
14
|
+
defaultGetRoles
|
15
|
+
};
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { GetCurrentUser } from '@redmix/graphql-server';
|
2
|
+
import type { Middleware } from '@redmix/web/middleware';
|
3
|
+
export interface SupabaseAuthMiddlewareOptions {
|
4
|
+
getCurrentUser: GetCurrentUser;
|
5
|
+
getRoles?: (decoded: any) => string[];
|
6
|
+
}
|
7
|
+
/**
|
8
|
+
* Create Supabase Auth Middleware that sets the `serverAuthState` based on the Supabase cookie.
|
9
|
+
*/
|
10
|
+
declare const initSupabaseAuthMiddleware: ({ getCurrentUser, getRoles, }: SupabaseAuthMiddlewareOptions) => [Middleware, "*"];
|
11
|
+
export default initSupabaseAuthMiddleware;
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAC5D,OAAO,KAAK,EACV,UAAU,EAGX,MAAM,wBAAwB,CAAA;AAI/B,MAAM,WAAW,6BAA6B;IAC5C,cAAc,EAAE,cAAc,CAAA;IAC9B,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,MAAM,EAAE,CAAA;CACtC;AAED;;GAEG;AACH,QAAA,MAAM,0BAA0B,kCAG7B,6BAA6B,KAAG,CAAC,UAAU,EAAE,GAAG,CAmElD,CAAA;AACD,eAAe,0BAA0B,CAAA"}
|
package/dist/index.js
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
import { AUTH_PROVIDER_HEADER } from "@redmix/api";
|
2
|
+
import { authDecoder } from "@redmix/auth-supabase-api";
|
3
|
+
import { clearAuthState } from "./util.js";
|
4
|
+
const initSupabaseAuthMiddleware = ({
|
5
|
+
getCurrentUser,
|
6
|
+
getRoles
|
7
|
+
}) => {
|
8
|
+
const middleware = async (req, res) => {
|
9
|
+
const type = "supabase";
|
10
|
+
const cookieHeader = req.headers.get("cookie");
|
11
|
+
if (!cookieHeader) {
|
12
|
+
return res;
|
13
|
+
}
|
14
|
+
try {
|
15
|
+
const authProviderCookie = req.cookies.get(AUTH_PROVIDER_HEADER);
|
16
|
+
if (!authProviderCookie || authProviderCookie !== type) {
|
17
|
+
return res;
|
18
|
+
}
|
19
|
+
const decoded = await authDecoder(cookieHeader, type, {
|
20
|
+
event: req
|
21
|
+
});
|
22
|
+
const currentUser = await getCurrentUser(
|
23
|
+
decoded,
|
24
|
+
{ type, token: cookieHeader, schema: "cookie" },
|
25
|
+
{ event: req }
|
26
|
+
);
|
27
|
+
if (req.url.includes(`/middleware/supabase/currentUser`)) {
|
28
|
+
res.body = // Not sure how currentUser can be string.... but types say so
|
29
|
+
typeof currentUser === "string" ? currentUser : JSON.stringify({ currentUser });
|
30
|
+
return res;
|
31
|
+
}
|
32
|
+
const userMetadata = typeof currentUser === "string" ? null : currentUser?.["user_metadata"];
|
33
|
+
req.serverAuthState.set({
|
34
|
+
currentUser,
|
35
|
+
loading: false,
|
36
|
+
isAuthenticated: !!currentUser,
|
37
|
+
hasError: false,
|
38
|
+
userMetadata: userMetadata || currentUser,
|
39
|
+
cookieHeader,
|
40
|
+
roles: getRoles ? getRoles(decoded) : []
|
41
|
+
});
|
42
|
+
} catch (e) {
|
43
|
+
console.error(e, "Error in Supabase Auth Middleware");
|
44
|
+
clearAuthState(req, res);
|
45
|
+
return res;
|
46
|
+
}
|
47
|
+
return res;
|
48
|
+
};
|
49
|
+
return [middleware, "*"];
|
50
|
+
};
|
51
|
+
var index_default = initSupabaseAuthMiddleware;
|
52
|
+
export {
|
53
|
+
index_default as default
|
54
|
+
};
|
package/dist/util.d.ts
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
import type { SupabaseClient } from '@supabase/supabase-js';
|
2
|
+
import type { MiddlewareRequest, MiddlewareResponse } from '@redmix/web/middleware';
|
3
|
+
/**
|
4
|
+
* Creates Supabase Server Client used to get the session cookie (only)
|
5
|
+
* from a given collection of auth cookies
|
6
|
+
*/
|
7
|
+
export declare const createSupabaseServerClient: (req: MiddlewareRequest, res: MiddlewareResponse) => {
|
8
|
+
cookieName: string | null;
|
9
|
+
supabase: SupabaseClient;
|
10
|
+
};
|
11
|
+
/**
|
12
|
+
* Clear the Supabase and auth cookies from the request and response
|
13
|
+
* and clear the auth context
|
14
|
+
*/
|
15
|
+
export declare const clearAuthState: (req: MiddlewareRequest, res: MiddlewareResponse) => void;
|
16
|
+
//# sourceMappingURL=util.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAI3D,OAAO,KAAK,EACV,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,wBAAwB,CAAA;AAC/B;;;GAGG;AACH,eAAO,MAAM,0BAA0B,QAChC,iBAAiB,OACjB,kBAAkB,KACtB;IAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,QAAQ,EAAE,cAAc,CAAA;CAmCvD,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,QACpB,iBAAiB,OACjB,kBAAkB,SAgBxB,CAAA"}
|
package/dist/util.js
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
import { createServerClient } from "@supabase/ssr";
|
2
|
+
import { AUTH_PROVIDER_HEADER } from "@redmix/api";
|
3
|
+
import { throwSupabaseSettingsError } from "@redmix/auth-supabase-api";
|
4
|
+
const createSupabaseServerClient = (req, res) => {
|
5
|
+
let cookieName = null;
|
6
|
+
if (!process.env.SUPABASE_URL) {
|
7
|
+
throwSupabaseSettingsError("SUPABASE_URL");
|
8
|
+
}
|
9
|
+
if (!process.env.SUPABASE_KEY) {
|
10
|
+
throwSupabaseSettingsError("SUPABASE_KEY");
|
11
|
+
}
|
12
|
+
const supabase = createServerClient(
|
13
|
+
process.env.SUPABASE_URL || "",
|
14
|
+
process.env.SUPABASE_KEY || "",
|
15
|
+
{
|
16
|
+
cookies: {
|
17
|
+
get(name) {
|
18
|
+
cookieName = name;
|
19
|
+
return req.cookies.get(name)?.valueOf();
|
20
|
+
},
|
21
|
+
set(name, value, options) {
|
22
|
+
cookieName = name;
|
23
|
+
req.cookies.set(name, value, options);
|
24
|
+
res.cookies.set(name, value, options);
|
25
|
+
},
|
26
|
+
remove(name, options) {
|
27
|
+
cookieName = name;
|
28
|
+
req.cookies.set(name, "", options);
|
29
|
+
res.cookies.set(name, "", options);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
);
|
34
|
+
return { cookieName, supabase };
|
35
|
+
};
|
36
|
+
const clearAuthState = (req, res) => {
|
37
|
+
req.serverAuthState.clear();
|
38
|
+
const { cookieName } = createSupabaseServerClient(req, res);
|
39
|
+
if (cookieName) {
|
40
|
+
res.cookies.unset(cookieName);
|
41
|
+
}
|
42
|
+
res.cookies.unset(AUTH_PROVIDER_HEADER);
|
43
|
+
};
|
44
|
+
export {
|
45
|
+
clearAuthState,
|
46
|
+
createSupabaseServerClient
|
47
|
+
};
|
package/package.json
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
{
|
2
|
+
"name": "@redmix/auth-supabase-middleware",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"repository": {
|
5
|
+
"type": "git",
|
6
|
+
"url": "git+https://github.com/redmix-run/redmix.git",
|
7
|
+
"directory": "packages/auth-providers/supabase/middleware"
|
8
|
+
},
|
9
|
+
"license": "MIT",
|
10
|
+
"type": "module",
|
11
|
+
"exports": {
|
12
|
+
".": {
|
13
|
+
"import": {
|
14
|
+
"types": "./dist/index.d.ts",
|
15
|
+
"default": "./dist/index.js"
|
16
|
+
},
|
17
|
+
"require": {
|
18
|
+
"types": "./dist/cjs/index.d.ts",
|
19
|
+
"default": "./dist/cjs/index.js"
|
20
|
+
}
|
21
|
+
}
|
22
|
+
},
|
23
|
+
"main": "./dist/index.js",
|
24
|
+
"types": "./dist/index.d.ts",
|
25
|
+
"files": [
|
26
|
+
"dist",
|
27
|
+
"cjsWrappers"
|
28
|
+
],
|
29
|
+
"scripts": {
|
30
|
+
"build": "tsx ./build.mts",
|
31
|
+
"build:pack": "yarn pack -o redmix-auth-supabase-middleware.tgz",
|
32
|
+
"build:types": "tsc --build --verbose ./tsconfig.build.json",
|
33
|
+
"build:types-cjs": "tsc --build --verbose ./tsconfig.cjs.json",
|
34
|
+
"check:attw": "yarn attw -P",
|
35
|
+
"check:package": "concurrently npm:check:attw yarn:publint",
|
36
|
+
"prepublishOnly": "NODE_ENV=production yarn build",
|
37
|
+
"test": "vitest run",
|
38
|
+
"test:watch": "vitest watch"
|
39
|
+
},
|
40
|
+
"dependencies": {
|
41
|
+
"@redmix/auth-supabase-api": "0.0.1",
|
42
|
+
"@redmix/web": "0.0.1",
|
43
|
+
"@supabase/ssr": "0.5.1"
|
44
|
+
},
|
45
|
+
"devDependencies": {
|
46
|
+
"@arethetypeswrong/cli": "0.16.4",
|
47
|
+
"@redmix/api": "0.0.1",
|
48
|
+
"@redmix/auth": "0.0.1",
|
49
|
+
"@redmix/framework-tools": "0.0.1",
|
50
|
+
"@redmix/graphql-server": "0.0.1",
|
51
|
+
"@types/aws-lambda": "8.10.145",
|
52
|
+
"concurrently": "8.2.2",
|
53
|
+
"publint": "0.3.11",
|
54
|
+
"ts-toolbelt": "9.6.0",
|
55
|
+
"tsx": "4.19.3",
|
56
|
+
"typescript": "5.6.2",
|
57
|
+
"vitest": "2.1.9"
|
58
|
+
},
|
59
|
+
"publishConfig": {
|
60
|
+
"access": "public"
|
61
|
+
},
|
62
|
+
"gitHead": "25a2481ac394049b7c864eda26381814a0124a79"
|
63
|
+
}
|