prostgles-client 4.0.141 → 4.0.142
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/dist/Auth.d.ts +20 -19
- package/dist/Auth.d.ts.map +1 -1
- package/dist/Auth.js +47 -34
- package/dist/index.js +2 -1
- package/dist/index.js.LICENSE.txt +9 -0
- package/dist/index.no-sync.js +2 -1
- package/dist/index.no-sync.js.LICENSE.txt +9 -0
- package/lib/Auth.ts +71 -43
- package/lib/SyncedTable/SyncedTable.ts +1 -1
- package/package.json +2 -2
- package/tests/package-lock.json +2 -2
package/lib/Auth.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type AnyObject, type AuthGuardLocation, type AuthGuardLocationResponse, type AuthSocketSchema, CHANNELS, type IdentityProvider, type EmailAuthType } from "prostgles-types";
|
|
1
|
+
import { type AnyObject, type AuthGuardLocation, type AuthGuardLocationResponse, type AuthSocketSchema, CHANNELS, type IdentityProvider, type EmailAuthType, isEmpty } from "prostgles-types";
|
|
2
2
|
import { hasWnd } from "./prostgles";
|
|
3
3
|
|
|
4
4
|
type Args = {
|
|
@@ -7,62 +7,41 @@ type Args = {
|
|
|
7
7
|
onReload: VoidFunction | undefined;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
type WithProviderLogin = Partial<Record<IdentityProvider, VoidFunction>>;
|
|
11
|
+
|
|
12
|
+
type EmailAuth =
|
|
13
|
+
| {
|
|
14
|
+
withPassword?: (params: { username: string; password: string; remember_me?: boolean; totp_token?: string; totp_recovery_code?: string; }) => Promise<any>;
|
|
15
|
+
withMagicLink?: undefined;
|
|
16
|
+
}
|
|
17
|
+
| {
|
|
18
|
+
withPassword?: undefined;
|
|
19
|
+
withMagicLink?: (params: { username: string; }) => Promise<any>;
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
type AuthStateLoggedOut = {
|
|
11
23
|
isLoggedin: false;
|
|
12
24
|
user?: undefined;
|
|
13
25
|
prefferedLogin: string;
|
|
14
26
|
login?: {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
withPassword: (params: { email: string; password: string }) => Promise<any>;
|
|
21
|
-
} | {
|
|
22
|
-
magicLink: (params: { email: string; }) => Promise<any>;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
27
|
+
withProvider?: WithProviderLogin;
|
|
28
|
+
} & EmailAuth;
|
|
29
|
+
register?: EmailAuth
|
|
30
|
+
};
|
|
31
|
+
|
|
25
32
|
type AuthStateLoggedIn = {
|
|
26
33
|
isLoggedin: true;
|
|
27
34
|
user: AnyObject;
|
|
28
35
|
prefferedLogin: string;
|
|
29
36
|
logout: VoidFunction;
|
|
30
|
-
}
|
|
37
|
+
};
|
|
38
|
+
|
|
31
39
|
export type AuthHandler =
|
|
32
40
|
| AuthStateLoggedOut
|
|
33
41
|
| AuthStateLoggedIn;
|
|
34
42
|
|
|
35
43
|
export const setupAuth = ({ authData: authConfig, socket, onReload }: Args): AuthHandler => {
|
|
36
|
-
|
|
37
|
-
const emit = (channel: string, params: any) => {
|
|
38
|
-
return new Promise((resolve, reject) => {
|
|
39
|
-
socket.emit(channel, params, (err, res) => {
|
|
40
|
-
if (err) reject(err);
|
|
41
|
-
else resolve(res);
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if(!authConfig?.user){
|
|
47
|
-
return {
|
|
48
|
-
isLoggedin: false,
|
|
49
|
-
user: undefined,
|
|
50
|
-
prefferedLogin: "",
|
|
51
|
-
login: {
|
|
52
|
-
withEmailAndPassword: (params) => emit(CHANNELS.LOGIN, { type: "withPassword" satisfies EmailAuthType, params }),
|
|
53
|
-
withMagicLink: (params) => emit(CHANNELS.LOGIN, { type: "magicLink" satisfies EmailAuthType, params }),
|
|
54
|
-
withProvider: (provider) => {
|
|
55
|
-
const url = authConfig?.providers?.[provider]?.url;
|
|
56
|
-
if(!url) throw new Error(`Provider ${provider} not enabled`);
|
|
57
|
-
window.location.assign(url);
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
register: authConfig?.register? {
|
|
61
|
-
[authConfig.register]: (params) => emit(CHANNELS.REGISTER, { type: authConfig.register, params })
|
|
62
|
-
} as any : undefined
|
|
63
|
-
} satisfies AuthStateLoggedOut;
|
|
64
|
-
}
|
|
65
|
-
if (authConfig.pathGuard && hasWnd) {
|
|
44
|
+
if (authConfig?.pathGuard && hasWnd) {
|
|
66
45
|
const doReload = (res?: AuthGuardLocationResponse) => {
|
|
67
46
|
if (res?.shouldReload) {
|
|
68
47
|
if (onReload) onReload();
|
|
@@ -80,11 +59,60 @@ export const setupAuth = ({ authData: authConfig, socket, onReload }: Args): Aut
|
|
|
80
59
|
doReload(res);
|
|
81
60
|
});
|
|
82
61
|
}
|
|
62
|
+
|
|
63
|
+
if(!authConfig?.user){
|
|
64
|
+
const { login, providers, register } = authConfig ?? {};
|
|
65
|
+
const withProvider: WithProviderLogin | undefined = isEmpty(providers)? undefined : providers && Object.entries(providers).reduce((acc, [provider, { url }]) => {
|
|
66
|
+
acc[provider as IdentityProvider] = () => {
|
|
67
|
+
window.location.assign(url);
|
|
68
|
+
}
|
|
69
|
+
return acc;
|
|
70
|
+
}, {});
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
isLoggedin: false,
|
|
74
|
+
user: undefined,
|
|
75
|
+
prefferedLogin: "",
|
|
76
|
+
login: (login || providers) && {
|
|
77
|
+
withProvider,
|
|
78
|
+
...(login && {
|
|
79
|
+
[login.type]: async (params) => {
|
|
80
|
+
return POST(login.url, params);
|
|
81
|
+
},
|
|
82
|
+
}),
|
|
83
|
+
},
|
|
84
|
+
register: register?.type? {
|
|
85
|
+
[register.type]: (params) => {
|
|
86
|
+
POST(register.url, params);
|
|
87
|
+
}
|
|
88
|
+
} : undefined
|
|
89
|
+
} satisfies AuthStateLoggedOut;
|
|
90
|
+
}
|
|
83
91
|
|
|
84
92
|
return {
|
|
85
93
|
isLoggedin: true,
|
|
86
94
|
user: authConfig.user,
|
|
87
|
-
logout: () =>
|
|
95
|
+
logout: () => {
|
|
96
|
+
|
|
97
|
+
},
|
|
88
98
|
prefferedLogin: "",
|
|
89
99
|
} satisfies AuthStateLoggedIn;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export const POST = async (path: string, data: object) => {
|
|
103
|
+
const rawResponse = await fetch(path, {
|
|
104
|
+
method: "POST",
|
|
105
|
+
headers: {
|
|
106
|
+
"Accept": "application/json",
|
|
107
|
+
"Content-Type": "application/json"
|
|
108
|
+
},
|
|
109
|
+
body: JSON.stringify(data)
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
if(!rawResponse.ok){
|
|
113
|
+
const error = await rawResponse.json().catch(() => rawResponse.text()).catch(() => rawResponse.statusText);
|
|
114
|
+
throw new Error(error);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return rawResponse;
|
|
90
118
|
}
|
|
@@ -1072,7 +1072,7 @@ export function quickClone<T>(obj: T): T {
|
|
|
1072
1072
|
const s: Sync<{ a: number, b: string; }> = 1 as any;
|
|
1073
1073
|
const sh = s({ a: 1 }, { } as any, (d) => { d });
|
|
1074
1074
|
|
|
1075
|
-
const syncTyped: Sync<{ col1: string; }, ()=>any> = 1 as any;
|
|
1075
|
+
const syncTyped: Sync<{ col1: string; }, ()=> any> = 1 as any;
|
|
1076
1076
|
|
|
1077
1077
|
// const sUntyped: Sync<AnyObject, any> = syncTyped;
|
|
1078
1078
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prostgles-client",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.142",
|
|
4
4
|
"description": "Reactive client for Postgres",
|
|
5
5
|
"main": "dist/prostgles-full.js",
|
|
6
6
|
"types": "dist/prostgles-full.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"i": "^0.3.7",
|
|
32
32
|
"npm": "^10.9.1",
|
|
33
|
-
"prostgles-types": "^4.0.
|
|
33
|
+
"prostgles-types": "^4.0.99"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/node": "^14.14.14",
|
package/tests/package-lock.json
CHANGED
|
@@ -17,12 +17,12 @@
|
|
|
17
17
|
},
|
|
18
18
|
"..": {
|
|
19
19
|
"name": "prostgles-client",
|
|
20
|
-
"version": "4.0.
|
|
20
|
+
"version": "4.0.141",
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"i": "^0.3.7",
|
|
24
24
|
"npm": "^10.9.1",
|
|
25
|
-
"prostgles-types": "^4.0.
|
|
25
|
+
"prostgles-types": "^4.0.99"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/node": "^14.14.14",
|