|
| 1 | +--- |
| 2 | +slug: /integrations/permit.io |
| 3 | +sidebar_label: Permit.io |
| 4 | +sidebar_custom_props: |
| 5 | + description: Integrate Logto with Permit.io. |
| 6 | +--- |
| 7 | + |
| 8 | +# Set up authorization with Permit.io |
| 9 | + |
| 10 | +[Permit.io](https://permit.io/) is an authorization platform that provides fine-grained access control for applications. You can use Logto in conjunction with Permit.io to manage user authentication and authorization. |
| 11 | + |
| 12 | +:::tip |
| 13 | +To understand the key differences between authentication and authorization, see [Authentication vs. authorization](/concepts/authn-vs-authz). |
| 14 | +::: |
| 15 | + |
| 16 | +This guide will show you how to connect Logto with Permit.io to implement authorization in your application. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- A [Logto Cloud](https://cloud.logto.io/) account or a [self-hosted Logto](https://docs.logto.io/introduction/set-up-logto-oss). |
| 21 | +- A Next.js application with Logto authentication already set up (follow our [Next.js guide](https://docs.logto.io/quick-starts/next) if you haven't) |
| 22 | +- A [Permit.io](https://app.permit.io/) account. Follow the official [Permit.io quickstart guide](https://docs.permit.io/quickstart/) to set up your Permit.io project before proceeding. |
| 23 | + |
| 24 | +## Integration \{#integration} |
| 25 | + |
| 26 | +### Install Permit.io SDK \{#install-permitio-sdk} |
| 27 | + |
| 28 | +Add the Permit.io SDK to your application: |
| 29 | + |
| 30 | +```bash |
| 31 | +npm install permitio |
| 32 | +``` |
| 33 | + |
| 34 | +### Create a Permit.io project \{#create-a-permitio-project} |
| 35 | + |
| 36 | +1. Sign up for a free account at [permit.io](https://app.permit.io/) |
| 37 | +2. Create a new project and obtain your API key |
| 38 | +3. Add the API key to your environment variables: |
| 39 | + |
| 40 | +```bash |
| 41 | +PERMIT_API_KEY=your-permit-api-key |
| 42 | +``` |
| 43 | + |
| 44 | +### Set up Permit.io client \{#set-up-permitio-client} |
| 45 | + |
| 46 | +Create a file to handle the Permit.io integration: |
| 47 | + |
| 48 | +```jsx |
| 49 | +// libraries/permit.js |
| 50 | +const { Permit } = require('permitio'); |
| 51 | + |
| 52 | +// Initialize the Permit.io client |
| 53 | +const permit = new Permit({ |
| 54 | + pdp: 'https://cloudpdp.api.permit.io', |
| 55 | + token: 'your-permitio-api-key', |
| 56 | +}); |
| 57 | + |
| 58 | +// Sync a user with Permit.io |
| 59 | +export const syncUserToPermit = async (userId, email, firstName, lastName, role = 'viewer') => { |
| 60 | + // First, sync the user |
| 61 | + await permit.api.syncUser({ |
| 62 | + key: userId, |
| 63 | + email: email || undefined, |
| 64 | + first_name: firstName || undefined, |
| 65 | + last_name: lastName || undefined, |
| 66 | + }); |
| 67 | + |
| 68 | + // Then assign a role to the user |
| 69 | + await permit.api.assignRole({ |
| 70 | + user: userId, |
| 71 | + role: role, |
| 72 | + tenant: 'default', |
| 73 | + }); |
| 74 | + |
| 75 | + return true; |
| 76 | +}; |
| 77 | +``` |
| 78 | + |
| 79 | +### Create a Logto webhook for user registration \{#create-a-logto-webhook-for-user-registration} |
| 80 | + |
| 81 | +Logto provides [webhooks](/developers/webhooks) that can notify your application when events occur. We'll use the `PostRegister` webhook to [sync users to Permit.io](https://docs.permit.io/authentication/permit-and-authentication) when they sign up. |
| 82 | + |
| 83 | +Create a webhook endpoint in your application: |
| 84 | + |
| 85 | +```jsx |
| 86 | +// pages/api/webhooks/logto.js |
| 87 | +import { syncUserToPermit } from '../../../libraries/permit'; |
| 88 | + |
| 89 | +export default async function handler(req, res) { |
| 90 | + // Log the webhook payload for debugging |
| 91 | + console.log('Webhook payload:', req.body); |
| 92 | + |
| 93 | + const { event, user } = req.body; |
| 94 | + |
| 95 | + // Process user registration events |
| 96 | + if (event === 'PostRegister') { |
| 97 | + try { |
| 98 | + // Determine the user's role (you can implement your own logic here) |
| 99 | + let role = 'viewer'; // Default role |
| 100 | + |
| 101 | + // Sync the user to Permit.io |
| 102 | + await syncUserToPermit(user.id, user.primaryEmail, user.name, undefined, role); |
| 103 | + |
| 104 | + return res.status(200).json({ success: true }); |
| 105 | + } catch (error) { |
| 106 | + console.error('Error syncing user:', error); |
| 107 | + return res.status(500).json({ error: 'Failed to sync user' }); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return res.status(200).json({ message: 'Event ignored' }); |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### Configure the webhook in Logto Console \{#configure-the-webhook-in-logto-console} |
| 116 | + |
| 117 | +1. Go to the Webhooks section in your Logto Console |
| 118 | +2. Click "Create Webhook" |
| 119 | +3. Give your webhook a name |
| 120 | +4. Enter your endpoint URL (e.g., `https://your-app.com/api/webhooks/logto`) |
| 121 | +5. Select the `PostRegister` event |
| 122 | +6. Save the webhook |
| 123 | + |
| 124 | +:::note |
| 125 | +For local development, you can use tools like [ngrok](https://ngrok.com/) to expose your local server to the internet. |
| 126 | +::: |
| 127 | + |
| 128 | +### Test user sync \{#test-user-sync} |
| 129 | + |
| 130 | +To test that users are being synced correctly: |
| 131 | + |
| 132 | +1. Create a new user account in your application |
| 133 | +2. Check the Permit.io dashboard under "Directory" → "Users" to verify the user was synced |
| 134 | +3. Verify that the correct role was assigned to the user upon sign-up |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +### Use Permit.io for authorization \{#use-permitio-for-authorization} |
| 139 | + |
| 140 | +Once users are synced, you can use Permit.io to check permissions: |
| 141 | + |
| 142 | +```jsx |
| 143 | +// Example of checking a permission |
| 144 | +const isPermitted = await permit.check(userId, 'view', 'reports'); |
| 145 | + |
| 146 | +if (isPermitted) { |
| 147 | + // User is allowed to view resources |
| 148 | + // Show the resources UI |
| 149 | +} else { |
| 150 | + // User is not allowed to view resources |
| 151 | + // Show an access denied message |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +## Conclusion \{#conclusion} |
| 156 | + |
| 157 | +You've successfully connected Logto with Permit.io to automatically sync users and implement authorization in your application. With this integration: |
| 158 | + |
| 159 | +1. Users authenticate through Logto |
| 160 | +2. New users are automatically synced to Permit.io via webhooks |
| 161 | +3. You can use Permit.io to check permissions and implement access control |
| 162 | + |
| 163 | +This setup provides a strong foundation for implementing more advanced authorization patterns as your application grows. |
| 164 | + |
| 165 | +For more advanced authorization use cases, explore [Permit.io's documentation](https://docs.permit.io/) on creating policies, enforcing permissions, and implementing role-based access control. |
0 commit comments