Back

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

Aug 17, 20247 minute read

Hey there, fellow JavaScript enthusiasts! Ready to dive into the world of SafetyCulture integrations? Today, we're going to walk through building a rock-solid authorization flow for your user-facing integration. Buckle up, because we're about to make auth both secure and painless!

Introduction

SafetyCulture integrations are a fantastic way to extend the platform's capabilities, but let's face it: without proper authorization, we're not going anywhere. That's why we're focusing on building a bulletproof auth flow. It's the foundation of any good integration, so let's get it right!

Prerequisites

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

  • A SafetyCulture developer account (if you don't have one, go grab it!)
  • A solid grasp on OAuth 2.0 (don't worry, we'll refresh your memory)
  • Node.js and Express.js set up and ready to roll

Got all that? Great! Let's build something awesome.

Setting up the SafetyCulture Application

First things first, let's get our ducks in a row with SafetyCulture:

  1. Head over to the SafetyCulture developer portal and create a new application.
  2. Snag your client ID and client secret – guard these with your life!
  3. Set up your redirect URI. This is where SafetyCulture will send your users after they've logged in.

Implementing the Authorization Flow

Now for the fun part – let's build this flow!

Initiating the auth request

We'll start by constructing our authorization URL:

const authUrl = `https://api.safetyculture.io/auth?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}`;

When your user wants to connect, send them to this URL. They'll log in to SafetyCulture, and then – boom! – they're sent back to your redirect URI.

Handling the callback

Once the user's back on your turf, you'll have an authorization code. Time to swap it for the good stuff – access and refresh tokens:

app.get('/callback', async (req, res) => { const { code } = req.query; const tokenResponse = await axios.post('https://api.safetyculture.io/auth/token', { grant_type: 'authorization_code', client_id: clientId, client_secret: clientSecret, code, redirect_uri: redirectUri }); // Store these tokens securely! const { access_token, refresh_token } = tokenResponse.data; // ... store tokens ... });

Token Management

Access tokens don't last forever, so let's keep things fresh:

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

Making Authenticated Requests

Now that we've got our tokens, let's put them to work:

async function makeApiRequest(endpoint, accessToken) { try { const response = await axios.get(`https://api.safetyculture.io/api/v3/${endpoint}`, { headers: { Authorization: `Bearer ${accessToken}` } }); return response.data; } catch (error) { if (error.response && error.response.status === 401) { // Time to refresh that token! } throw error; } }

Security Considerations

Security isn't just a feature, it's a lifestyle. Here are some tips to keep your integration Fort Knox-level secure:

  • Always use HTTPS. Always.
  • Store tokens securely. Consider encryption at rest.
  • Implement CSRF protection. Express has some great middleware for this.

Testing the Auth Flow

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

  1. Can you initiate the auth flow?
  2. Does the callback correctly exchange the code for tokens?
  3. Can you make authenticated requests?
  4. Does token refresh work as expected?

Consider setting up some automated tests to keep things running smoothly as you develop.

Conclusion

And there you have it! You've just built a robust authorization flow for your SafetyCulture integration. With this foundation, you're ready to start building some truly awesome features. Remember, a secure auth flow is the bedrock of any great integration.

Additional Resources

Want to dive deeper? Check out:

Now go forth and integrate! Your users are going to love what you build. Happy coding!