Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Multi-tenant authentication flow

How do companies handle authentication in their multi-tenant web apps?

Essentially I have a single PostgreSQL database instance with many tables. Each table has a workspace_id column which I will use to use to grant/deny access. You can think of a workspace as a client and a single user can be associated with multiple workspaces.

My initial thought was to:

  1. Use the frontend app and let the user send the email and password to the backend.
  2. Backend validates details and returns all the workspaces the user belongs to.
  3. The frontend app displays the workspaces.
  4. User selects the workspace they want to login into. The id of the workspace and the user details that were passed in step 1 is again to the backend.
  5. The backend validates again all the details and issues a jwt token containing the user details and the workspace id.
  6. Later when the user tries to access any resource I will extract the workspace id from the token to check if the user has access to a resource or not.

I am halfway through implementing what I've described above but I am not sure if that's the best approach. What do you think?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm not sure I would call this multi-tenancy - really it is just a case of different users with different claims:

  • Users should log in once, then have access to the data that their user identity is entitled to

When your UI calls APIs, the back end should receive a JWT access token with either of these payloads. The second of these is preferred, but not all systems support that:

  • The user ID only
  • The user ID plus an array of workspace IDs

SIMPLEST OPTION

This might just be to look up the user's workspace IDs whenever an API request is received, based on the user ID in the JWT access token, as in Joe's comment above.

CLAIMS PRINCIPAL

If workspace IDs are used frequently for authorization across many API requests, then a better option is to design a Claims Principal object, containing data commonly used by the API for authorization, and containing the important IDs. It might look like this for a particular user:

{
  sub: "wdvohjkerwt8",
  userID: 234,
  workspaceIDs: [2, 7, 19]
}

This object typically needs to be comprised of both Identity Data (stored by the Authorization Server) and also domain specific data. The above userID might be a database key, whereas the subject claim is often a generated value.

When an API request is received, you can either read all claims from the JWT access token, or combine domain specific data with the JWT data.

The Claims Principal is then injected where needed, so that your API authorization logic can be coded in a simple way. In your case this will involve filtering workspaces when working with collections, or denying access if the user specifically tries to access a workspace they are not entitled to.

Here is some sample Node.js code of mine that does this, using a region array claim:

  • Claims Principal Code
  • Authorizing Code
  • Doc
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!