UNPKG

1.92 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/auth
4 *
5 * (c) AdonisJS Auth
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.defineTestsBindings = void 0;
12/**
13 * Define test bindings
14 */
15function defineTestsBindings(ApiRequest, ApiClient, AuthManager) {
16 /**
17 * Set "sessionClient" on the api request
18 */
19 ApiRequest.getter('authManager', function () {
20 return AuthManager;
21 }, true);
22 /**
23 * Login user using the default guard
24 */
25 ApiRequest.macro('loginAs', function (user) {
26 this['authData'] = {
27 client: this.authManager.client(this.authManager.defaultGuard),
28 args: [user],
29 };
30 return this;
31 });
32 /**
33 * Login user using a custom guard
34 */
35 ApiRequest.macro('guard', function (mapping) {
36 return {
37 loginAs: (...args) => {
38 this['authData'] = {
39 client: this.authManager.client(mapping),
40 args,
41 };
42 return this;
43 },
44 };
45 });
46 /**
47 * Hook into the request and login the user
48 */
49 ApiClient.setup(async (request) => {
50 const authData = request['authData'];
51 if (!authData) {
52 return;
53 }
54 const requestData = await authData.client.login(...authData.args);
55 if (requestData.headers) {
56 request.headers(requestData.headers);
57 }
58 if (requestData.session) {
59 request.session(requestData.session);
60 }
61 if (requestData.cookies) {
62 request.cookies(requestData.cookies);
63 }
64 return async () => {
65 await authData.client.logout(...authData.args);
66 };
67 });
68}
69exports.defineTestsBindings = defineTestsBindings;