@gopherium/react-auth 0.5.0 → 0.7.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.
- package/dist/admin/index.d.ts +10 -10
- package/dist/admin/index.js +25 -25
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/session.d.ts +1 -1
- package/dist/testing/index.d.ts +5 -5
- package/dist/testing/index.js +11 -11
- package/dist/transport.d.ts +1 -1
- package/package.json +3 -3
package/dist/admin/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ declare const userSchema: z.ZodObject<{
|
|
|
5
5
|
email: z.ZodString;
|
|
6
6
|
name: z.ZodString;
|
|
7
7
|
disabled: z.ZodBoolean;
|
|
8
|
-
|
|
8
|
+
role: z.ZodDefault<z.ZodString>;
|
|
9
9
|
created_at: z.ZodCoercedDate<unknown>;
|
|
10
10
|
}, z.core.$strip>;
|
|
11
11
|
export type User = z.infer<typeof userSchema>;
|
|
@@ -13,7 +13,7 @@ export interface NewUser {
|
|
|
13
13
|
email: string;
|
|
14
14
|
name: string;
|
|
15
15
|
password: string;
|
|
16
|
-
|
|
16
|
+
role?: string;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* EmailTakenError is thrown when a new user's email is already in use.
|
|
@@ -27,18 +27,18 @@ export declare class EmailTakenError extends Error {
|
|
|
27
27
|
export declare class ValidationError extends Error {
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
30
|
+
* RoleRefusedError is thrown when the actor holds no role the server admits.
|
|
31
31
|
*/
|
|
32
|
-
export declare class
|
|
32
|
+
export declare class RoleRefusedError extends Error {
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
35
|
+
* SelfRoleError is thrown when an account changes its own role.
|
|
36
36
|
*/
|
|
37
|
-
export declare class
|
|
37
|
+
export declare class SelfRoleError extends Error {
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
40
|
* LastPrivilegedError is thrown when a write would leave no enabled
|
|
41
|
-
* account holding a privileged
|
|
41
|
+
* account holding a privileged role.
|
|
42
42
|
*/
|
|
43
43
|
export declare class LastPrivilegedError extends Error {
|
|
44
44
|
}
|
|
@@ -61,9 +61,9 @@ export declare function createUser(input: NewUser): Promise<User>;
|
|
|
61
61
|
*/
|
|
62
62
|
export declare function setUserDisabled(id: string, disabled: boolean): Promise<void>;
|
|
63
63
|
/**
|
|
64
|
-
* Sets the
|
|
64
|
+
* Sets the role an account holds.
|
|
65
65
|
* @param id - The identifier of the account to update.
|
|
66
|
-
* @param
|
|
66
|
+
* @param role - The role the account is to hold.
|
|
67
67
|
*/
|
|
68
|
-
export declare function
|
|
68
|
+
export declare function setUserRole(id: string, role: string): Promise<void>;
|
|
69
69
|
export {};
|
package/dist/admin/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const userSchema = z.object({
|
|
|
10
10
|
email: z.string(),
|
|
11
11
|
name: z.string(),
|
|
12
12
|
disabled: z.boolean(),
|
|
13
|
-
|
|
13
|
+
role: z.string().default(''),
|
|
14
14
|
created_at: z.coerce.date(),
|
|
15
15
|
});
|
|
16
16
|
/**
|
|
@@ -25,18 +25,18 @@ export class EmailTakenError extends Error {
|
|
|
25
25
|
export class ValidationError extends Error {
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* RoleRefusedError is thrown when the actor holds no role the server admits.
|
|
29
29
|
*/
|
|
30
|
-
export class
|
|
30
|
+
export class RoleRefusedError extends Error {
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
33
|
+
* SelfRoleError is thrown when an account changes its own role.
|
|
34
34
|
*/
|
|
35
|
-
export class
|
|
35
|
+
export class SelfRoleError extends Error {
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
38
|
* LastPrivilegedError is thrown when a write would leave no enabled
|
|
39
|
-
* account holding a privileged
|
|
39
|
+
* account holding a privileged role.
|
|
40
40
|
*/
|
|
41
41
|
export class LastPrivilegedError extends Error {
|
|
42
42
|
}
|
|
@@ -114,15 +114,15 @@ async function restSetUserDisabled(id, disabled) {
|
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
117
|
-
* Writes the
|
|
117
|
+
* Writes the role an account holds over REST.
|
|
118
118
|
* @param id - The identifier of the account to update.
|
|
119
|
-
* @param
|
|
119
|
+
* @param role - The role the account is to hold.
|
|
120
120
|
*/
|
|
121
|
-
async function
|
|
122
|
-
const response = await fetch(`/api/users/${id}/
|
|
121
|
+
async function restSetUserRole(id, role) {
|
|
122
|
+
const response = await fetch(`/api/users/${id}/role`, {
|
|
123
123
|
method: 'PUT',
|
|
124
124
|
headers: { 'Content-Type': 'application/json' },
|
|
125
|
-
body: JSON.stringify({
|
|
125
|
+
body: JSON.stringify({ role }),
|
|
126
126
|
});
|
|
127
127
|
if (response.ok) {
|
|
128
128
|
return;
|
|
@@ -130,24 +130,24 @@ async function restSetUserRank(id, rank) {
|
|
|
130
130
|
if (response.status === 401) {
|
|
131
131
|
throw new UnauthorizedError('session expired');
|
|
132
132
|
}
|
|
133
|
-
const
|
|
134
|
-
if (
|
|
135
|
-
throw new
|
|
133
|
+
const failed = await errorOf(response);
|
|
134
|
+
if (failed.code === 'role_insufficient') {
|
|
135
|
+
throw new RoleRefusedError(failed.message);
|
|
136
136
|
}
|
|
137
|
-
if (
|
|
138
|
-
throw new
|
|
137
|
+
if (failed.code === 'self_role_refused') {
|
|
138
|
+
throw new SelfRoleError(failed.message);
|
|
139
139
|
}
|
|
140
|
-
if (
|
|
141
|
-
throw new LastPrivilegedError(
|
|
140
|
+
if (failed.code === 'last_privileged_refused') {
|
|
141
|
+
throw new LastPrivilegedError(failed.message);
|
|
142
142
|
}
|
|
143
|
-
throw new Error(`updating
|
|
143
|
+
throw new Error(`updating role failed with status ${response.status}`);
|
|
144
144
|
}
|
|
145
145
|
/**
|
|
146
146
|
* Returns the code and message a refused response carries.
|
|
147
147
|
* @param response - The refused HTTP response.
|
|
148
|
-
* @returns The
|
|
148
|
+
* @returns The error code, if any, and its message.
|
|
149
149
|
*/
|
|
150
|
-
async function
|
|
150
|
+
async function errorOf(response) {
|
|
151
151
|
try {
|
|
152
152
|
const held = errorSchema.parse(await response.json());
|
|
153
153
|
return { code: held.code, message: held.error };
|
|
@@ -181,10 +181,10 @@ export async function setUserDisabled(id, disabled) {
|
|
|
181
181
|
await resolveTransport('setUserDisabled', restSetUserDisabled)(id, disabled);
|
|
182
182
|
}
|
|
183
183
|
/**
|
|
184
|
-
* Sets the
|
|
184
|
+
* Sets the role an account holds.
|
|
185
185
|
* @param id - The identifier of the account to update.
|
|
186
|
-
* @param
|
|
186
|
+
* @param role - The role the account is to hold.
|
|
187
187
|
*/
|
|
188
|
-
export async function
|
|
189
|
-
await resolveTransport('
|
|
188
|
+
export async function setUserRole(id, role) {
|
|
189
|
+
await resolveTransport('setUserRole', restSetUserRole)(id, role);
|
|
190
190
|
}
|
package/dist/api.d.ts
CHANGED
package/dist/api.js
CHANGED
package/dist/session.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare function useSession(): import("@tanstack/react-query").UseQueryRe
|
|
|
10
10
|
id: string;
|
|
11
11
|
email: string;
|
|
12
12
|
name: string;
|
|
13
|
-
|
|
13
|
+
role: string;
|
|
14
14
|
} | null>, Error>;
|
|
15
15
|
/**
|
|
16
16
|
* Ends the current session and drops all cached data belonging to the
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -12,16 +12,16 @@ export declare function installTestEnvironment(): void;
|
|
|
12
12
|
*/
|
|
13
13
|
export declare const defaultUser: User;
|
|
14
14
|
/**
|
|
15
|
-
* Returns a canned account holding the given
|
|
16
|
-
* @param
|
|
15
|
+
* Returns a canned account holding the given role, one identifier per role.
|
|
16
|
+
* @param role - The role the account holds.
|
|
17
17
|
* @returns The account.
|
|
18
18
|
*/
|
|
19
|
-
export declare function
|
|
19
|
+
export declare function userWithRole(role: string): User;
|
|
20
20
|
/**
|
|
21
|
-
* Accepts a
|
|
21
|
+
* Accepts a role change.
|
|
22
22
|
* @returns The msw handler.
|
|
23
23
|
*/
|
|
24
|
-
export declare function
|
|
24
|
+
export declare function roleOk(): import("msw").HttpHandler;
|
|
25
25
|
/**
|
|
26
26
|
* Seeds the cached session so components render signed in without a fetch.
|
|
27
27
|
* @param client - The query client under test.
|
package/dist/testing/index.js
CHANGED
|
@@ -25,33 +25,33 @@ export const defaultUser = {
|
|
|
25
25
|
id: '0198b2f0-0000-7000-8000-000000000001',
|
|
26
26
|
email: 'grace@example.com',
|
|
27
27
|
name: 'Grace Hopper',
|
|
28
|
-
|
|
28
|
+
role: '',
|
|
29
29
|
};
|
|
30
30
|
/**
|
|
31
|
-
* Returns a canned account holding the given
|
|
32
|
-
* @param
|
|
31
|
+
* Returns a canned account holding the given role, one identifier per role.
|
|
32
|
+
* @param role - The role the account holds.
|
|
33
33
|
* @returns The account.
|
|
34
34
|
*/
|
|
35
|
-
export function
|
|
35
|
+
export function userWithRole(role) {
|
|
36
36
|
let held = 2166136261;
|
|
37
|
-
for (const letter of
|
|
37
|
+
for (const letter of role) {
|
|
38
38
|
held ^= letter.charCodeAt(0);
|
|
39
39
|
held = Math.imul(held, 16777619);
|
|
40
40
|
}
|
|
41
41
|
held >>>= 0;
|
|
42
42
|
return {
|
|
43
43
|
id: `0198b2f0-0000-7000-8000-${String(held).padStart(12, '0')}`,
|
|
44
|
-
email: `${
|
|
45
|
-
name: `Maria Perez the ${
|
|
46
|
-
|
|
44
|
+
email: `${role}@example.com`,
|
|
45
|
+
name: `Maria Perez the ${role}`,
|
|
46
|
+
role,
|
|
47
47
|
};
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
-
* Accepts a
|
|
50
|
+
* Accepts a role change.
|
|
51
51
|
* @returns The msw handler.
|
|
52
52
|
*/
|
|
53
|
-
export function
|
|
54
|
-
return http.put('/api/users/:id/
|
|
53
|
+
export function roleOk() {
|
|
54
|
+
return http.put('/api/users/:id/role', () => new HttpResponse(null, { status: 204 }));
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
57
57
|
* Seeds the cached session so components render signed in without a fetch.
|
package/dist/transport.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export interface AuthTransport {
|
|
|
14
14
|
fetchUsers(signal?: AbortSignal): Promise<Account[]>;
|
|
15
15
|
createUser(input: NewUser): Promise<Account>;
|
|
16
16
|
setUserDisabled(id: string, disabled: boolean): Promise<void>;
|
|
17
|
-
|
|
17
|
+
setUserRole(id: string, role: string): Promise<void>;
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* Replaces backend calls with the given implementations, keeping the REST
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gopherium/react-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Session authentication for React apps backed by gouncer's authkit.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@tanstack/react-query": "^5.101.2",
|
|
51
|
-
"@testing-library/jest-dom": "^6.9.1",
|
|
51
|
+
"@testing-library/jest-dom": "^6.9.1 || ^7.0.0",
|
|
52
52
|
"@testing-library/react": "^16.3.2",
|
|
53
53
|
"@wordpress/i18n": ">=6.26.0 <7.0.0",
|
|
54
54
|
"@wordpress/theme": ">=1.0.0 <2.0.0",
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
81
|
"@tanstack/react-query": "^5.101.2",
|
|
82
|
-
"@testing-library/jest-dom": "^
|
|
82
|
+
"@testing-library/jest-dom": "^7.0.0",
|
|
83
83
|
"@testing-library/react": "^16.3.2",
|
|
84
84
|
"@testing-library/user-event": "^14.6.1",
|
|
85
85
|
"@types/gettext-parser": "9.0.0",
|