Back

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

Aug 15, 20247 minute read

Hey there, fellow JavaScript aficionado! Ready to dive into the world of Tidio 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 Tidio integration dreams a reality!

Prerequisites

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

  • Your favorite code editor
  • Node.js installed
  • Tidio API credentials (if you don't have these yet, hop over to the Tidio developer portal and grab 'em)

Setting up the project

Let's kick things off by setting up our project:

mkdir tidio-integration cd tidio-integration npm init -y npm install express axios dotenv

Create a .env file in your project root and add your Tidio credentials:

TIDIO_CLIENT_ID=your_client_id
TIDIO_CLIENT_SECRET=your_client_secret

Understanding Tidio's OAuth 2.0 flow

Tidio uses OAuth 2.0 for authorization. Here's the gist:

  1. We redirect the user to Tidio's auth page
  2. User grants permission
  3. Tidio sends us an authorization code
  4. We exchange that code for an access token
  5. We use the access token to make API calls

Simple, right? Let's make it happen!

Implementing the authorization request

Create an index.js file and let's get this party started:

require('dotenv').config(); const express = require('express'); const app = express(); app.get('/auth', (req, res) => { const authUrl = `https://www.tidio.com/oauth/authorize?client_id=${process.env.TIDIO_CLIENT_ID}&redirect_uri=${encodeURIComponent('http://localhost:3000/callback')}&response_type=code`; res.redirect(authUrl); }); app.listen(3000, () => console.log('Server running on http://localhost:3000'));

When a user hits /auth, we'll redirect them to Tidio's authorization page. Neat!

Handling the callback

Now, let's handle that callback:

const axios = require('axios'); app.get('/callback', async (req, res) => { const { code } = req.query; try { const response = await axios.post('https://www.tidio.com/oauth/token', { client_id: process.env.TIDIO_CLIENT_ID, client_secret: process.env.TIDIO_CLIENT_SECRET, code, grant_type: 'authorization_code', redirect_uri: 'http://localhost:3000/callback' }); const { access_token, refresh_token } = response.data; // Store these tokens securely! res.send('Authorization successful!'); } catch (error) { console.error('Error exchanging code for token:', error); res.status(500).send('Authorization failed'); } });

Refreshing the access token

Access tokens don't last forever, so let's add a refresh function:

async function refreshAccessToken(refresh_token) { try { const response = await axios.post('https://www.tidio.com/oauth/token', { client_id: process.env.TIDIO_CLIENT_ID, client_secret: process.env.TIDIO_CLIENT_SECRET, refresh_token, grant_type: 'refresh_token' }); return response.data.access_token; } catch (error) { console.error('Error refreshing token:', error); throw error; } }

Securing the token storage

Storing tokens securely is crucial. In a production environment, you'd want to use a database and encrypt the tokens. For now, let's use in-memory storage:

const tokens = new Map(); // In the callback route: tokens.set('access_token', access_token); tokens.set('refresh_token', refresh_token);

Making authenticated requests to Tidio API

Now for the fun part - using our shiny new access token:

app.get('/api-call', async (req, res) => { try { const response = await axios.get('https://api.tidio.com/v1/some-endpoint', { headers: { Authorization: `Bearer ${tokens.get('access_token')}` } }); res.json(response.data); } catch (error) { if (error.response && error.response.status === 401) { // Token expired, let's refresh it const newAccessToken = await refreshAccessToken(tokens.get('refresh_token')); tokens.set('access_token', newAccessToken); // Retry the request // ... } else { res.status(500).send('API call failed'); } } });

Error handling and edge cases

Always be prepared for things to go sideways. Handle token expiration (as shown above), user denials, and network issues gracefully. Your future self will thank you!

Testing the auth flow

Time to put our creation to the test:

  1. Start your server: node index.js
  2. Visit http://localhost:3000/auth in your browser
  3. Grant permissions on the Tidio page
  4. Check your server logs and browser response

If all goes well, you should see "Authorization successful!" in your browser. Congratulations, you've just implemented a Tidio auth flow!

Conclusion

And there you have it, folks! You've successfully built the authorization flow for a Tidio integration. From here, you can expand your integration by adding more API calls, implementing a proper database for token storage, and adding user-specific functionality.

Remember, the auth flow is the foundation of your integration. With this solid base, you're well on your way to creating something truly awesome with Tidio. Keep coding, keep learning, and most importantly, have fun building!