-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthentication.js
54 lines (47 loc) · 2.14 KB
/
authentication.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"use strict";
// You want to make a request to an endpoint that is either specifically designed
// to test auth, or one that every user will have access to. eg: `/me`.
// By returning the entire request object, you have access to the request and
// response data for testing purposes. Your connection label can access any data
// from the returned response using the `json.` prefix. eg: `{{json.username}}`.
const test = (z, bundle) =>
z.request({
url: "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/auth-status",
});
// This function runs after every outbound request. You can use it to check for
// errors or modify the response. You can have as many as you need. They'll need
// to each be registered in your index.js file.
const handleBadResponses = (response, z, bundle) => {
if (response.status === 401) {
throw new z.errors.Error(
// This message is surfaced to the user
"The token and/or secret you supplied is incorrect",
"AuthenticationError",
response.status
);
}
return response;
};
module.exports = {
config: {
// "basic" auth automatically creates "username" and "password" input fields. It
// also registers default middleware to create the authentication header.
type: "basic",
// Define any input app's auth requires here. The user will be prompted to enter
// this info when they connect their account.
fields: [],
// The test method allows Zapier to verify that the credentials a user provides
// are valid. We'll execute this method whenever a user connects their account for
// the first time.
test,
// This template string can access all the data returned from the auth test. If
// you return the test object, you'll access the returned data with a label like
// `{{json.X}}`. If you return `response.data` from your test, then your label can
// be `{{X}}`. This can also be a function that returns a label. That function has
// the standard args `(z, bundle)` and data returned from the test can be accessed
// in `bundle.inputData.X`.
connectionLabel: "{{json.Account.Name}}",
},
befores: [],
afters: [handleBadResponses],
};