Dittofi
  • Tutorials
    • Tutorial Index
    • Quick Start
    • Dittofi Essentials
      • Part 1: Frontend terms and concepts
      • Part 2: Backend terms and concepts
      • Part 3: Connecting frontend and backend
      • Part 4: Dittofi app structure
    • Dittofi App Design
      • 🏗️App architecture 101
      • ◼️Backend app development 101
      • 🟪Frontend app development 101
      • 🔰(optional) Web developer beginner series
  • Learn Frontend
    • 👨‍💻Getting Started
      • Welcome to Dittofi
      • Intro to the Dittofi Design Studio (Frontend)
    • 🟩Pages
    • 🟪Elements
      • Types
        • Button Element
        • Div Block Element
        • Form Block Element
        • Link Block Element
        • List Element
        • Modal Box Element
        • Tabs Element
        • Columns Element
        • Image Element
      • Partials
      • Prefabs
      • Custom
    • 🟧Variables
      • Array
      • Checkbox
      • Collection
      • Collection (Custom Model)
      • Collection (System Custom Model)
      • Custom Model
      • Date
      • Datetime
      • File
      • Json
      • Number
      • Number with decimal
      • Model
      • System Custom Model
      • Text
      • UUID
    • 🟨Events
      • 🟨External
        • HTTP Request
        • Run Endpoint
      • 🟨Flow Control
        • Condition
        • Run Action
        • Loop
        • Sleep
      • 🟨Maths
        • Add
        • Divide
        • Modulus
        • Multiply
        • Subtract
      • 🟨Navigation
        • Open Page
      • 🟨Notifications
        • Alert
        • Confirm
      • 🟨Other
        • Copy To Clipboard
        • Run Custom
      • 🟨Text
        • Concat
        • Contains
        • Ends With
        • I Contains
        • I Ends With
        • I Starts With
        • LTrim
        • RTrim
        • Text To Upper
        • Text To Lower
        • Trim
        • Starts With
      • 🟨Variables
        • Set Field
        • Remove Field
      • Validate
    • 🟦CSS Layouts
      • Holy Grail Layout
      • Side Bar Layout
      • Sticky Footer Layout
      • Sticky Header Layout
      • CSS Frameworks
    • 🔲CSS Frameworks
    • Component Libraries
    • Meta Attributes
  • Learn Backend
    • 🖥️Getting Started
      • Intro to Data
      • Intro to APIs
      • Build your first custom API
      • Actions & Events
    • 🟫Back-End Actions
    • 🟩Custom Models
      • 🟩Attributes
        • Array
        • Checkbox
        • Custom Model
        • Collection (Custom Model)
        • Collection (System Custom Model)
        • Date
        • Datetime
        • File
        • Number
        • Number With Decimal
        • Json
        • Text
    • ◼️Data Models
      • Database Fundamentals
        • Database relationships
      • ◼️Columns
        • Attachment
        • Auto Increment
        • Checkbox
        • Date
        • Datetime
        • Duration
        • Email
        • Long text
        • Link to another record field
        • Number
        • Number with decimal
        • Password
        • Phone number
        • Percent
        • Price
        • Rating
        • Single select
        • Single line text
        • URL
    • 🟥Events
      • 🟦Arrays
        • Append
        • Array Pop
        • At
        • Length
        • Prepend
        • Range
      • 🟦Conversions
        • Base64 Encode
        • Base64 Decode
        • Json Decode
        • Json Encode
        • Path Escape
        • Path Unescape
        • Query Escape
        • Query String Parse
        • Query Unescape
        • To Bool
        • To Float
        • To Int
        • To Str
        • Yaml Encode
        • Yaml Decode
      • 🟦Database Requests
        • Create
        • Create Or Update
        • Delete
        • Has Record
        • Read Event
        • Update
      • 🟦External
        • HTTP Request
        • SFTP Download
        • SFTP Upload
      • 🟦Files
        • CSV Reader Event
        • CSV Writer Event
        • File Read
        • File Save
        • File Size
        • File Type
        • JSON Reader
        • PDF Generator
      • 🟦Flow Control
        • Condition
        • Loop
        • Return
        • Run Action
        • Sleep
      • 🟦Notifications
        • Send Mail
        • Connect Websocket
        • Message Websocket
      • 🟦Maths
        • Add
        • Acos
        • Acosh
        • Asin
        • Asinh
        • Atan
        • Atanh
        • Cbrt
        • Ceil
        • Divide
        • Modulus
        • Multiply
        • Subtract
      • 🟦Security
        • Compare Password
        • JWT Decode
        • JWT Encode
        • Login
        • Logout Web User
        • Password Generator
        • Random Number
      • 🟦Time
        • Add Time
        • Day
        • Format Time
        • Minute
        • Nanosecond
        • Parse Time
        • Second
        • Time In
        • Year
        • Year Day
      • 🟦Text
        • Concat
        • Contains
        • Ends With
        • Execute Template
        • I Contains
        • I Ends With
        • I Starts With
        • Starts With
        • LTrim
        • RTrim
        • Replace
        • Regex Find All String
        • Regex Find String
        • Regex Match String
        • Trim
        • Text To Lower
        • Text To Upper
        • Text To Title
        • Url Getarg
      • 🟦Other
        • Assign
        • Run Custom
        • Redirect Web Page Event
        • Render Web Template Event
    • 🟧Variables
      • Array
      • Checkbox
      • Collection
      • Collection (Custom Model)
      • Collection (System Custom Model)
      • Custom Model
      • Date
      • Datetime
      • File
      • Json
      • Number
      • Number with decimal
      • Model
      • System Custom Model
      • Text
      • UUID
    • 🟪Webservices
  • Templates
    • Two sided marketplace template
      • Introduction
        • Part I: Installing and setting up the marketplace template
        • Part II: Understanding what features are included in the marketplace template
        • Part III: Customizing your marketplace frontend without coding
        • Exploring Dittofi's marketplace API
      • Listings
        • How to create a marketplace listing
    • How to build a CRUD app
  • Third Party APIs
    • OAuth 2.0 APIs
      • Introduction To OAuth 2.0
      • Google OAuth 2.0 - PART I
      • Google OAuth 2.0 - PART II
  • Dittofi Admin
    • Your Account
      • Workspace Admin
      • Manage Billing
      • Connect a custom domain
      • How to use a discount code to Dittofi Pro
  • FAQ
    • API Generator
    • How to write custom SQL?
    • Why does my image not display?
    • How can I set up Google Analytics?
    • Exporting and deploying my frontend code
    • What is a partial and what is a prefab?
    • How to set up a basic mulit tenant app on Dittofi
    • How to write a custom endpoint in Dittofi
    • How to integrate Font Awesome into Dittofi
    • How can I set up Dittofi to run on my own private server?
    • How to add roles such as Super Admin, Admin, Manager & User
    • How to prevent duplicate records being added to the database
    • What to do when relation cannot be created because their is violating data in the referenced table
  • Change Logs
    • Dittofi Change Logs
Powered by GitBook
On this page
  • Step 1. Create your data models
  • Step 2. Generate your authentication endpoints
  • Step 3. Build more endpoints and actions
  • Step 4. Build your code and test

Was this helpful?

  1. FAQ

How to set up a basic mulit tenant app on Dittofi

PreviousWhat is a partial and what is a prefab?NextHow to write a custom endpoint in Dittofi

Last updated 2 years ago

Was this helpful?

In this article we are going to look at how you can set up a basic multi tenant app on Dittofi. We can do this in four quick and easy steps.

  • Step 1: Create your data models

  • Step 2: Generate your authentication endpoints

  • Step 3: Build more endpoints and actions

  • Step 4: Build your code and test

NOTE In order to completely understand this article, we recommend that you either read and first, and / or have a detailed understanding of what are Data Models, Variables, Endpoints, Actions and Events. Furthermore, if you're not familar with what Multi Tenancy is, you can read our blog .

Step 1. Create your data models

For the purpose of this example we are going to create two data models, one called Users and one called Books. The Users data model will be used to hold all of the users that sign up to our app and will have two fields, Username (of type single line text) and Password (of type password). You can see an example of this below.

The Books data model will store a list of books that each user has created. These will have a title field (of type single line text) and also a creator_id field (of type number). You can see this table below.

NOTE We have prepopulated our tables with data. You can do this also, however you MUST MAKE SURE that your creator_id column matches the Id's of your users in your Users table. This is because in the next step we are going to associate each book in your Books Table with a user in your Users Table and we are going to create this association by matching the data in the creator_id column in your Books Table with the id in your Users table.

Next, we are going to join the two tables together so that every user can have one set of books associated with them The association will be between the Id column in the Users table and the creator_id column in the Books table.

To do this, first go into the Table Relations tab within Dittofi's data models as shown below.

Next, click on the Books data model and add a new relation.

Fill in the form as below, this will join the Books table with the Users table where the users table will be the parent table.

Next, save and the table relation and return to your Table Relations view. You will see the following relationship has been created between the tables.

Step 2. Generate your authentication endpoints

Next up, you need to create authentication endpoints. To do this, go to the Authentication Profiles tab that is located under the wrench icon in the bottom left of the Dittofi Design Studio.

This will give you the option to generate Sign Up, Login, Log Out and Rest Password endpoints, actions and workflows. You can see the endpoints generated by this click on the Endpoints tab inside the Dittofi Design Studio as shown below.

You can test the authentication endpoints by generating your code and running the endpoints from inside the Endpoints tab.

Step 3. Build more endpoints and actions

Next, we are going to create an endpoint for our app that will get the lists of books for ONLY the users that are authenticated to see them. To do this, create a new endpoint in the Endpoints tab as below. Make sure to set the authentication profile option in the endpoint to match the authentication profile that we created in Step 2.

Next, head over to the actions tab and create the endpoints corresponding Get Books action, as shown below.

** IMPORTANT ** Remember to link your action to your endpoint

Next, we need to configure our Get Books action to get data out of our Books model, ONLY for an authenticated user. To do this, we need to first add a variable of type Collection to our action.

Next, we our action to ONLY get the data for the user that is authenticated to see the data. This will be WHERE the creator_id from your Books Table is equal to the ID of the user, taken from your Users table.

To set up this WHERE clause, you can make use of a special Global Variable that was automatically created when we generated the code for our Authentication Profile in Step 2 and follow the steps in the GIF below.

Finally, press save and close the variable browser. The final step to set up your action is to make sure that the response variable from your action is set equal to the variable that we just created and which is going to store the list of books for a particular user. You can see this below.

Step 4. Build your code and test

Next let's run our Get Books endpoint without first running our login endpoint. You can do this from within the endpoints tab, as shown below.

You will get a 401 error at this point with the error message being:

{
  "data": null,
  "message": "login required",
  "error": "e_unauthorized"
}

This is because you have no logged in users.

If you run the Login endpoint and login one of the users inside your users table, what you will see is that if you then re-run your Get Books endpoint, this will only return data for that particular user.

Notice that the data that is returned all has the creator_id equal to 1 which is the same as the id for the logged in user james.virgo. You can see a print out of this below.

{
  "data": [
    {
      "Id": 1,
      "title": "100 years of solitude",
      "creator_id": 1
    },
    {
      "Id": 2,
      "title": "The Sirens of Titan",
      "creator_id": 1
    },
    {
      "Id": 4,
      "title": "The Autumn of the Patriarch",
      "creator_id": 1
    },
    {
      "Id": 5,
      "title": "Journey to the center of the earth",
      "creator_id": 1
    }
  ],
  "message": "success",
  "error": ""
}

Similarly, if we logout this user and login our other user, we get only the books returned where the creator_id is equal to 2.

Notice, the data that is returned (as above) all has the creator_id equal to 2 which is the same as the id for the logged in user samuel.virgo. You can see a print out of this below.

{
  "data": [
    {
      "Id": 6,
      "title": "King Lear",
      "creator_id": 2
    },
    {
      "Id": 7,
      "title": "Frank",
      "creator_id": 2
    },
    {
      "Id": 8,
      "title": "The Kingdom Of This World",
      "creator_id": 2
    }
  ],
  "message": "success",
  "error": ""
}

Since neither user of our app can access the others, this is an example of a straightforward mulit tenant architecture that you can use. Of course you can also export the code from Dittofi and set it up any other way that you might like to.

Click on in the top right hand side of the Authentication Profiles tab to start building a new authentication profile. Fill in the form that pops up as below. This will create a new Authentication Profile with the name Simple auth.

Once you've saved your authentication profile, and before exiting your authentication profile click on .

The final step is to build the code and test. Build your code in the usual way, clicking.

To learn about how to set up a single tenant app with a code export, you can read .

Exporting and deploying my frontend code
Dittofi Quick Start
Dittofi Essentials
setting up single vs multi tenant apps on Dittofi