-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.js
81 lines (69 loc) · 3.1 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
require('dotenv').config();
// 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://sutter.innovint.us/api/v1/wineries'});
// 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 API Key you supplied is incorrect',
'AuthenticationError',
response.status,
);
}
return response;
};
// This function runs before every outbound request. You can have as many as you
// need. They'll need to each be registered in your index.js file.
const includeApiKey = (request, z, bundle) => {
// Use API key from bundle.authData, or fallback to the one from .env
const apiKey = bundle.authData.apiKey || process.env.API_KEY;
if (apiKey) {
// Use these lines to include the API key in the querystring
// request.params = request.params || {};
// request.params.api_key = bundle.authData.apiKey;
// If you want to include the API key in the header instead, uncomment this:
request.headers.Authorization = `Access-Token ${apiKey}`;
}
return request;
};
module.exports = {
config: {
// "custom" is the catch-all auth type. The user supplies some info and Zapier can
// make authenticated requests with it
type: 'custom',
// Define any input app's auth requires here. The user will be prompted to enter
// this info when they connect their account.
fields: [
{
key: 'apiKey',
label: 'API Key',
required: true,
type: 'string',
helpText: 'Go to the [API Details](https://cellar.innovint.us/#/developer/personal-access-token) ' +
'page in your account settings to find your Personal Access Tokens for the API Key.',
},
],
// 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.username}}',
},
befores: [includeApiKey],
afters: [handleBadResponses],
};