Back

How to build a public FareHarbor integration: Building the Auth Flow

Aug 16, 2024 • 7 minute read

Hey there, fellow JavaScript enthusiasts! Ready to dive into the world of FareHarbor integrations? Today, we're going to tackle one of the most crucial aspects of building a public integration: the authorization flow. Buckle up, because we're about to make your integration secure and user-friendly in no time!

Introduction

FareHarbor's API is a powerful tool for integrating booking functionality into your applications. But before we can start making those sweet, sweet API calls, we need to set up a robust authorization flow. This isn't just a box to tick – it's the foundation of a secure and reliable integration. Trust me, your users (and your future self) will thank you for getting this right.

Prerequisites

Before we jump in, make sure you've got:

  • FareHarbor API credentials (if you don't have these yet, hop over to their developer portal and get set up)
  • A Node.js environment with Express.js ready to roll

Got all that? Great! Let's get our hands dirty.

OAuth 2.0 Flow Overview

We'll be implementing the Authorization Code Grant flow – it's the gold standard for user-facing integrations. Here's what you need to know:

  • Client ID: Your application's identifier
  • Client Secret: Keep this safe! It's the secret sauce of your auth flow
  • Redirect URI: Where FareHarbor will send your users after they authorize your app

Implementing the Authorization Flow

Initiating the Auth Request

First things first, let's get that authorization URL set up:

const authUrl = `https://fareharbor.com/oauth/authorize/?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=code`; app.get('/auth', (req, res) => { res.redirect(authUrl); });

When a user hits this endpoint, they'll be whisked away to FareHarbor's authorization page. Magic!

Handling the Callback

Now, let's set up our redirect endpoint to catch that authorization code:

app.get('/callback', async (req, res) => { const { code } = req.query; // We'll use this code in the next step });

Exchanging the Code for Access Token

Time to trade in that code for an access token:

const tokenResponse = await axios.post('https://fareharbor.com/oauth/token/', { grant_type: 'authorization_code', code, client_id: clientId, client_secret: clientSecret, redirect_uri: redirectUri }); const { access_token, refresh_token } = tokenResponse.data; // Store these securely - we'll need them later!

Refreshing the Access Token

Access tokens don't last forever, so let's implement a refresh mechanism:

async function refreshAccessToken(refreshToken) { const response = await axios.post('https://fareharbor.com/oauth/token/', { grant_type: 'refresh_token', refresh_token: refreshToken, client_id: clientId, client_secret: clientSecret }); return response.data.access_token; }

Making Authenticated Requests

Now for the fun part – using that access token:

async function makeApiRequest(endpoint) { try { const response = await axios.get(`https://fareharbor.com/api/external/v1/${endpoint}`, { headers: { 'Authorization': `Bearer ${accessToken}` } }); return response.data; } catch (error) { if (error.response && error.response.status === 401) { // Time to refresh that token! accessToken = await refreshAccessToken(refreshToken); return makeApiRequest(endpoint); // Try again with the new token } throw error; } }

Security Considerations

Listen up, because this part's important:

  • Never, ever expose your client secret on the client-side
  • Always use HTTPS in production
  • Consider implementing PKCE for an extra layer of security

Testing the Auth Flow

Before you ship it, test it! Here's a quick checklist:

  1. Start the auth flow and make sure you're redirected to FareHarbor
  2. Authorize the app and ensure you're redirected back with a code
  3. Check that you can exchange the code for tokens
  4. Make an API call and verify you get a valid response
  5. Let your access token expire and confirm the refresh flow works

Conclusion

And there you have it! You've just built a rock-solid authorization flow for your FareHarbor integration. With this foundation, you're all set to start building out the rest of your integration. Remember, a secure auth flow is the backbone of any good API integration, so pat yourself on the back – you've done the hard part!

Additional Resources

Want to dive deeper? Check out:

Now go forth and integrate with confidence! Happy coding!