refactor: 💡 mock authenticate should handle multiple auth types
This commit is contained in:
6 files changed
+36
-69
No files matched your search
@@ -23,28 +23,34 @@ import { Response } from 'miragejs';
|
||||
// }
|
||||
// }
|
||||
|
||||
const methodHandlers = {
|
||||
password: {
|
||||
authenticate: (scopeAttrs) =>
|
||||
new Response(200, {}, {
|
||||
scope: scopeAttrs,
|
||||
id: 'token123',
|
||||
token: 'thetokenstring',
|
||||
account_id: '1',
|
||||
user_id: 'user123',
|
||||
auth_method_id: 'authmethod123',
|
||||
created_time: '',
|
||||
updated_time: '',
|
||||
last_used_time: '',
|
||||
expiration_time: ''
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
export function authHandler({ scopes, authMethods }, request) {
|
||||
const payload = JSON.parse(request.requestBody);
|
||||
if (payload.credentials.login_name === 'error') {
|
||||
return new Response(400);
|
||||
} else {
|
||||
const id = request.params.id_method.split(':')[0];
|
||||
const [, id, method] = request.params.id_method.match(/(?<id>.[^:]*)(?::)?(?<method>.*)?/);
|
||||
const authMethod = authMethods.find(id);
|
||||
const scope = scopes.find(authMethod.scopeId);
|
||||
const scopeAttrs =
|
||||
this.serialize(scopes.find(scope.id));
|
||||
return new Response(200, {}, {
|
||||
scope: scopeAttrs,
|
||||
id: 'token123',
|
||||
token: 'thetokenstring',
|
||||
account_id: '1',
|
||||
user_id: 'user123',
|
||||
auth_method_id: 'authmethod123',
|
||||
created_time: '',
|
||||
updated_time: '',
|
||||
last_used_time: '',
|
||||
expiration_time: ''
|
||||
});
|
||||
const scopeAttrs = this.serialize(scopes.find(scope.id));
|
||||
return methodHandlers[authMethod.type][method](scopeAttrs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user