My app requires the following auth flow:
User gets redirected to a URL on my website. They login, grant access etc. In the end the get redirected back to the original URL, but in the query params there is specified a token and some extra information about the user. (Similar to how Spotify does it)
I looked over all the options for authentication on Zapier, but it seems like only Oauth has the option of redirecting to an external URL for authentication. How can I solve this problem?
Also, is there a way of unit testing the authentication?
Best answer by ikbelkirasan
- Use oauth2Config.authorizeUrl to build the login URL that the users are redirected to in order to grant access. Use the redirect_uri query param to redirect them back to zapier. Make sure when you redirect them back to include the state query param that was generated by zapier and also it MUST include code query param with some dummy value so that zapier doesn't complain about it. Of course you can include any custom query params that are relevant to your authentication flow.
- At this point, zapier would call getAccessToken method. Now, you have all the query params you returned from step (1) in the bundle.cleanedRequest.querystring . Make sure to return an object that contains access_token key to let zapier know that the oauth2-ish authentication succeeded. Feel free to include that extra information you got in that object so that you can access it in future requests from bundle.authData .
- Set oauth2Config.autoRefresh to false because using this authentication flow you may not auto refresh tokens.
To answer your question about unit testing authentication, check out the oauth2 example to see how that can be done https://github.com/zapier/zapier-platform-example-app-oauth2/blob/master/test/basic.js
I personally don't use mock servers. I only use node.js nock library to intercept requests and return whatever response I need in my tests. Hope this helps.