@gopherium/react-auth 0.4.0 → 0.6.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 +24 -0
- package/dist/admin/index.js +69 -1
- package/dist/api.d.ts +1 -0
- package/dist/api.js +1 -0
- package/dist/languages/es-ES.json +1 -1
- package/dist/languages/languages/es-ES.json +1 -1
- package/dist/session.d.ts +1 -0
- package/dist/testing/index.d.ts +11 -0
- package/dist/testing/index.js +27 -0
- package/dist/transport.d.ts +1 -0
- package/package.json +1 -1
package/dist/admin/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ declare const userSchema: z.ZodObject<{
|
|
|
5
5
|
email: z.ZodString;
|
|
6
6
|
name: z.ZodString;
|
|
7
7
|
disabled: z.ZodBoolean;
|
|
8
|
+
role: z.ZodDefault<z.ZodString>;
|
|
8
9
|
created_at: z.ZodCoercedDate<unknown>;
|
|
9
10
|
}, z.core.$strip>;
|
|
10
11
|
export type User = z.infer<typeof userSchema>;
|
|
@@ -12,6 +13,7 @@ export interface NewUser {
|
|
|
12
13
|
email: string;
|
|
13
14
|
name: string;
|
|
14
15
|
password: string;
|
|
16
|
+
role?: string;
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
19
|
* EmailTakenError is thrown when a new user's email is already in use.
|
|
@@ -24,6 +26,22 @@ export declare class EmailTakenError extends Error {
|
|
|
24
26
|
*/
|
|
25
27
|
export declare class ValidationError extends Error {
|
|
26
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* RoleRefusedError is thrown when the actor holds no role the server admits.
|
|
31
|
+
*/
|
|
32
|
+
export declare class RoleRefusedError extends Error {
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* SelfRoleError is thrown when an account changes its own role.
|
|
36
|
+
*/
|
|
37
|
+
export declare class SelfRoleError extends Error {
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* LastPrivilegedError is thrown when a write would leave no enabled
|
|
41
|
+
* account holding a privileged role.
|
|
42
|
+
*/
|
|
43
|
+
export declare class LastPrivilegedError extends Error {
|
|
44
|
+
}
|
|
27
45
|
/**
|
|
28
46
|
* Returns every user account.
|
|
29
47
|
* @param signal - Aborts the in-flight request.
|
|
@@ -42,4 +60,10 @@ export declare function createUser(input: NewUser): Promise<User>;
|
|
|
42
60
|
* @param disabled - Whether the account should be disabled.
|
|
43
61
|
*/
|
|
44
62
|
export declare function setUserDisabled(id: string, disabled: boolean): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Sets the role an account holds.
|
|
65
|
+
* @param id - The identifier of the account to update.
|
|
66
|
+
* @param role - The role the account is to hold.
|
|
67
|
+
*/
|
|
68
|
+
export declare function setUserRole(id: string, role: string): Promise<void>;
|
|
45
69
|
export {};
|
package/dist/admin/index.js
CHANGED
|
@@ -10,6 +10,7 @@ const userSchema = z.object({
|
|
|
10
10
|
email: z.string(),
|
|
11
11
|
name: z.string(),
|
|
12
12
|
disabled: z.boolean(),
|
|
13
|
+
role: z.string().default(''),
|
|
13
14
|
created_at: z.coerce.date(),
|
|
14
15
|
});
|
|
15
16
|
/**
|
|
@@ -23,7 +24,23 @@ export class EmailTakenError extends Error {
|
|
|
23
24
|
*/
|
|
24
25
|
export class ValidationError extends Error {
|
|
25
26
|
}
|
|
26
|
-
|
|
27
|
+
/**
|
|
28
|
+
* RoleRefusedError is thrown when the actor holds no role the server admits.
|
|
29
|
+
*/
|
|
30
|
+
export class RoleRefusedError extends Error {
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* SelfRoleError is thrown when an account changes its own role.
|
|
34
|
+
*/
|
|
35
|
+
export class SelfRoleError extends Error {
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* LastPrivilegedError is thrown when a write would leave no enabled
|
|
39
|
+
* account holding a privileged role.
|
|
40
|
+
*/
|
|
41
|
+
export class LastPrivilegedError extends Error {
|
|
42
|
+
}
|
|
43
|
+
const errorSchema = z.object({ error: z.string(), code: z.string().optional() });
|
|
27
44
|
/**
|
|
28
45
|
* Extracts the backend's explanation from a failed response body.
|
|
29
46
|
* @param response - The failed HTTP response.
|
|
@@ -96,6 +113,49 @@ async function restSetUserDisabled(id, disabled) {
|
|
|
96
113
|
throw new Error(`updating user failed with status ${response.status}`);
|
|
97
114
|
}
|
|
98
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Writes the role an account holds over REST.
|
|
118
|
+
* @param id - The identifier of the account to update.
|
|
119
|
+
* @param role - The role the account is to hold.
|
|
120
|
+
*/
|
|
121
|
+
async function restSetUserRole(id, role) {
|
|
122
|
+
const response = await fetch(`/api/users/${id}/role`, {
|
|
123
|
+
method: 'PUT',
|
|
124
|
+
headers: { 'Content-Type': 'application/json' },
|
|
125
|
+
body: JSON.stringify({ role }),
|
|
126
|
+
});
|
|
127
|
+
if (response.ok) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (response.status === 401) {
|
|
131
|
+
throw new UnauthorizedError('session expired');
|
|
132
|
+
}
|
|
133
|
+
const refusal = await refusalOf(response);
|
|
134
|
+
if (refusal.code === 'role_insufficient') {
|
|
135
|
+
throw new RoleRefusedError(refusal.message);
|
|
136
|
+
}
|
|
137
|
+
if (refusal.code === 'self_role_refused') {
|
|
138
|
+
throw new SelfRoleError(refusal.message);
|
|
139
|
+
}
|
|
140
|
+
if (refusal.code === 'last_privileged_refused') {
|
|
141
|
+
throw new LastPrivilegedError(refusal.message);
|
|
142
|
+
}
|
|
143
|
+
throw new Error(`updating role failed with status ${response.status}`);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Returns the code and message a refused response carries.
|
|
147
|
+
* @param response - The refused HTTP response.
|
|
148
|
+
* @returns The refusal code, if any, and its message.
|
|
149
|
+
*/
|
|
150
|
+
async function refusalOf(response) {
|
|
151
|
+
try {
|
|
152
|
+
const held = errorSchema.parse(await response.json());
|
|
153
|
+
return { code: held.code, message: held.error };
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return { message: __('the account could not be updated', DOMAIN) };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
99
159
|
/**
|
|
100
160
|
* Returns every user account.
|
|
101
161
|
* @param signal - Aborts the in-flight request.
|
|
@@ -120,3 +180,11 @@ export async function createUser(input) {
|
|
|
120
180
|
export async function setUserDisabled(id, disabled) {
|
|
121
181
|
await resolveTransport('setUserDisabled', restSetUserDisabled)(id, disabled);
|
|
122
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* Sets the role an account holds.
|
|
185
|
+
* @param id - The identifier of the account to update.
|
|
186
|
+
* @param role - The role the account is to hold.
|
|
187
|
+
*/
|
|
188
|
+
export async function setUserRole(id, role) {
|
|
189
|
+
await resolveTransport('setUserRole', restSetUserRole)(id, role);
|
|
190
|
+
}
|
package/dist/api.d.ts
CHANGED
package/dist/api.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "": { "lang": "es-ES", "plural-forms": "nplurals=2; plural=(n != 1);" }, "Actions": ["Acciones"], "Active": ["Activo"], "Create user": ["Crear usuario"], "Disable": ["Desactivar"], "Disable %s": ["Desactivar a %s"], "Disabled": ["Desactivado"], "Enable": ["Activar"], "Enable %s": ["Activar a %s"], "Invalid email or password.": ["Correo o contraseña no válidos."], "Loading users…": ["Cargando usuarios…"], "Loading…": ["Cargando…"], "Log in": ["Iniciar sesión"], "Log out": ["Cerrar sesión"], "Login failed, please try again.": ["No se pudo iniciar sesión, inténtalo de nuevo."], "Logout failed, please try again.": ["No se pudo cerrar sesión, inténtalo de nuevo."], "Password": ["Contraseña"], "Something went wrong.": ["Algo ha ido mal."], "That email is already in use.": ["Ese correo ya está en uso."], "The user could not be created.": ["No se pudo crear el usuario."], "Too many attempts. Please wait a minute and try again.": ["Demasiados intentos. Espera un minuto e inténtalo de nuevo."], "Update failed.": ["No se pudo actualizar."], "Users could not be loaded.": ["No se pudieron cargar los usuarios."], "admin section\u0004Users": ["Usuarios"], "button\u0004New user": ["Nuevo usuario"], "column\u0004Email": ["Correo"], "column\u0004Name": ["Nombre"], "column\u0004Status": ["Estado"], "field label\u0004Email": ["Correo"], "field label\u0004Name": ["Nombre"], "invalid user details": ["datos de usuario no válidos"], "page heading\u0004New user": ["Nuevo usuario"], "page heading\u0004Users": ["Usuarios"] }
|
|
1
|
+
{ "": { "lang": "es-ES", "plural-forms": "nplurals=2; plural=(n != 1);" }, "Actions": ["Acciones"], "Active": ["Activo"], "Create user": ["Crear usuario"], "Disable": ["Desactivar"], "Disable %s": ["Desactivar a %s"], "Disabled": ["Desactivado"], "Enable": ["Activar"], "Enable %s": ["Activar a %s"], "Invalid email or password.": ["Correo o contraseña no válidos."], "Loading users…": ["Cargando usuarios…"], "Loading…": ["Cargando…"], "Log in": ["Iniciar sesión"], "Log out": ["Cerrar sesión"], "Login failed, please try again.": ["No se pudo iniciar sesión, inténtalo de nuevo."], "Logout failed, please try again.": ["No se pudo cerrar sesión, inténtalo de nuevo."], "Password": ["Contraseña"], "Something went wrong.": ["Algo ha ido mal."], "That email is already in use.": ["Ese correo ya está en uso."], "The user could not be created.": ["No se pudo crear el usuario."], "Too many attempts. Please wait a minute and try again.": ["Demasiados intentos. Espera un minuto e inténtalo de nuevo."], "Update failed.": ["No se pudo actualizar."], "Users could not be loaded.": ["No se pudieron cargar los usuarios."], "admin section\u0004Users": ["Usuarios"], "button\u0004New user": ["Nuevo usuario"], "column\u0004Email": ["Correo"], "column\u0004Name": ["Nombre"], "column\u0004Status": ["Estado"], "field label\u0004Email": ["Correo"], "field label\u0004Name": ["Nombre"], "invalid user details": ["datos de usuario no válidos"], "page heading\u0004New user": ["Nuevo usuario"], "page heading\u0004Users": ["Usuarios"], "the account could not be updated": ["no se ha podido actualizar la cuenta"] }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"":{"lang":"es-ES","plural-forms":"nplurals=2; plural=(n != 1);"},"Actions":["Acciones"],"Active":["Activo"],"Create user":["Crear usuario"],"Disable":["Desactivar"],"Disable %s":["Desactivar a %s"],"Disabled":["Desactivado"],"Enable":["Activar"],"Enable %s":["Activar a %s"],"Invalid email or password.":["Correo o contraseña no válidos."],"Loading users…":["Cargando usuarios…"],"Loading…":["Cargando…"],"Log in":["Iniciar sesión"],"Log out":["Cerrar sesión"],"Login failed, please try again.":["No se pudo iniciar sesión, inténtalo de nuevo."],"Logout failed, please try again.":["No se pudo cerrar sesión, inténtalo de nuevo."],"Password":["Contraseña"],"Something went wrong.":["Algo ha ido mal."],"That email is already in use.":["Ese correo ya está en uso."],"The user could not be created.":["No se pudo crear el usuario."],"Too many attempts. Please wait a minute and try again.":["Demasiados intentos. Espera un minuto e inténtalo de nuevo."],"Update failed.":["No se pudo actualizar."],"Users could not be loaded.":["No se pudieron cargar los usuarios."],"admin section\u0004Users":["Usuarios"],"button\u0004New user":["Nuevo usuario"],"column\u0004Email":["Correo"],"column\u0004Name":["Nombre"],"column\u0004Status":["Estado"],"field label\u0004Email":["Correo"],"field label\u0004Name":["Nombre"],"invalid user details":["datos de usuario no válidos"],"page heading\u0004New user":["Nuevo usuario"],"page heading\u0004Users":["Usuarios"]}
|
|
1
|
+
{"":{"lang":"es-ES","plural-forms":"nplurals=2; plural=(n != 1);"},"Actions":["Acciones"],"Active":["Activo"],"Create user":["Crear usuario"],"Disable":["Desactivar"],"Disable %s":["Desactivar a %s"],"Disabled":["Desactivado"],"Enable":["Activar"],"Enable %s":["Activar a %s"],"Invalid email or password.":["Correo o contraseña no válidos."],"Loading users…":["Cargando usuarios…"],"Loading…":["Cargando…"],"Log in":["Iniciar sesión"],"Log out":["Cerrar sesión"],"Login failed, please try again.":["No se pudo iniciar sesión, inténtalo de nuevo."],"Logout failed, please try again.":["No se pudo cerrar sesión, inténtalo de nuevo."],"Password":["Contraseña"],"Something went wrong.":["Algo ha ido mal."],"That email is already in use.":["Ese correo ya está en uso."],"The user could not be created.":["No se pudo crear el usuario."],"Too many attempts. Please wait a minute and try again.":["Demasiados intentos. Espera un minuto e inténtalo de nuevo."],"Update failed.":["No se pudo actualizar."],"Users could not be loaded.":["No se pudieron cargar los usuarios."],"admin section\u0004Users":["Usuarios"],"button\u0004New user":["Nuevo usuario"],"column\u0004Email":["Correo"],"column\u0004Name":["Nombre"],"column\u0004Status":["Estado"],"field label\u0004Email":["Correo"],"field label\u0004Name":["Nombre"],"invalid user details":["datos de usuario no válidos"],"page heading\u0004New user":["Nuevo usuario"],"page heading\u0004Users":["Usuarios"],"the account could not be updated":["no se ha podido actualizar la cuenta"]}
|
package/dist/session.d.ts
CHANGED
package/dist/testing/index.d.ts
CHANGED
|
@@ -11,6 +11,17 @@ export declare function installTestEnvironment(): void;
|
|
|
11
11
|
* defaultUser is the canned account tests sign in as.
|
|
12
12
|
*/
|
|
13
13
|
export declare const defaultUser: User;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a canned account holding the given role, one identifier per role.
|
|
16
|
+
* @param role - The role the account holds.
|
|
17
|
+
* @returns The account.
|
|
18
|
+
*/
|
|
19
|
+
export declare function userWithRole(role: string): User;
|
|
20
|
+
/**
|
|
21
|
+
* Accepts a role change.
|
|
22
|
+
* @returns The msw handler.
|
|
23
|
+
*/
|
|
24
|
+
export declare function roleOk(): import("msw").HttpHandler;
|
|
14
25
|
/**
|
|
15
26
|
* Seeds the cached session so components render signed in without a fetch.
|
|
16
27
|
* @param client - The query client under test.
|
package/dist/testing/index.js
CHANGED
|
@@ -25,7 +25,34 @@ export const defaultUser = {
|
|
|
25
25
|
id: '0198b2f0-0000-7000-8000-000000000001',
|
|
26
26
|
email: 'grace@example.com',
|
|
27
27
|
name: 'Grace Hopper',
|
|
28
|
+
role: '',
|
|
28
29
|
};
|
|
30
|
+
/**
|
|
31
|
+
* Returns a canned account holding the given role, one identifier per role.
|
|
32
|
+
* @param role - The role the account holds.
|
|
33
|
+
* @returns The account.
|
|
34
|
+
*/
|
|
35
|
+
export function userWithRole(role) {
|
|
36
|
+
let held = 2166136261;
|
|
37
|
+
for (const letter of role) {
|
|
38
|
+
held ^= letter.charCodeAt(0);
|
|
39
|
+
held = Math.imul(held, 16777619);
|
|
40
|
+
}
|
|
41
|
+
held >>>= 0;
|
|
42
|
+
return {
|
|
43
|
+
id: `0198b2f0-0000-7000-8000-${String(held).padStart(12, '0')}`,
|
|
44
|
+
email: `${role}@example.com`,
|
|
45
|
+
name: `Maria Perez the ${role}`,
|
|
46
|
+
role,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Accepts a role change.
|
|
51
|
+
* @returns The msw handler.
|
|
52
|
+
*/
|
|
53
|
+
export function roleOk() {
|
|
54
|
+
return http.put('/api/users/:id/role', () => new HttpResponse(null, { status: 204 }));
|
|
55
|
+
}
|
|
29
56
|
/**
|
|
30
57
|
* Seeds the cached session so components render signed in without a fetch.
|
|
31
58
|
* @param client - The query client under test.
|
package/dist/transport.d.ts
CHANGED
|
@@ -14,6 +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
|
+
setUserRole(id: string, role: string): Promise<void>;
|
|
17
18
|
}
|
|
18
19
|
/**
|
|
19
20
|
* Replaces backend calls with the given implementations, keeping the REST
|