This article will guide you through the process of displaying only the records that are connected to the logged-in user.
One of Knack's most powerful features is the ability for users to login and view their own private records and not the records of other users.
For example, you may want to have your customers login and see their invoices, or have your employees login to see their jobs for the day.
This article walks through how to enable logged-in users to only see their connected records using the Custom CRM sample app.
In order to set up your app to show records connected to the logged in user, you will need to have Users enabled in your app.
Tip: You can learn more on users and how to enable them here.
1. Defining Your User Roles
After enabling Users in your app, you have the ability to define the various user roles that will be able to interact with your Live App. Knack allows you to control access and permissions by specifying which user roles can access specific pages in your app and which views are displayed on each page.
Some examples of user roles include Employees, Managers, Editors, and Admins. You have the flexibility to create multiple roles based on your specific requirements. For instance, if you're developing an app for a school, you might have roles for students and teachers.
To add a user role, click the "+" button next to the User Roles list in the Data section of your Builder.
This will prompt you to name your user role, then a new user role will be added to your app. You can add fields to that user role just like any other table.
The main Accounts table will contain the user records for all of your roles. This is essentially a master list of all your user records.
If you add a new user to a role, that user will be present in both that user role table and in the main Accounts table. This is useful if you need to work with every user regardless of their role.
After you add your user role, you can add a record to start with in the "Records" section of the Builder. Any user records you add will be able to log in to your app on the pages you have allowed.
Tip: You can learn more on adding and managing user records here.
2. Connecting Users With Records
After setting up user roles, you have the ability to link your records to your users. For instance, you can connect Projects to Employees, Invoices to Clients, or Orders to Sales Reps.
Tip: If you don't already have an table with records you want to connect to your user, you can read more on adding tables here.
To connect records to your users, you will need to add a new connection field. In this example, we will connect the Contacts table to a Sales Rep user role.
While viewing the table in the Data section of the Builder, click the "+" button in the Connections right side bar.
This will then open a modal popup where you can select which table or user role to connect with. In this case, we are connecting the Contacts table to the Sales Rep user role.
Once you have selected which table to connect with, you will be prompted to define how those tables will connect. In this example, we want each Contact to connect with one Sales Lead and each Sales Lead to connect with many Contacts.
This will allow Sales Leads to be assigned multiple Contacts where each Contact will only have a single Sales Lead assigned to them.
Tip: You can learn more about different types of connections here.
When you are done defining the connection type, click "Add Connection" and this will create a new connection field in your table. Here, you can see the Contacts table now has a "Sales Rep" connection field.
This connection field will appear as a lookup where you can choose which record to connect with. For example, when you edit any of the Contact records, you can now use the connection field to select a Sales Rep:
3. Adding Login Pages to Your Live App
Now that the tables, user roles, and connections are setup, we need to add a page for your users to login and view their records.
A new login page can be added by clicking the "+" button on the Pages section of the Builder and then selecting "Login Page" from the options.
This will prompt you to choose who can login to the page. In this case, we want to select "Limit permissions to a specific user role."
For this example, we are limiting permission to Sales Reps.
Note: Learn more about security settings available with user logins to help ensure the security of your Live App pages.
Once you have created your new login page, you will be prompted to add a view. When adding a view, you will select the view type, the table that the view will display, and what type of records from that table will display.
For this example, we selected a grid view type which will display Contacts connected to the logged-in Sales Rep:
Next, you will be asked if you want to add any additional links to your view. This is not necessary for this example, but you can read more about action links here.
Once you have finished setting up your view, click the "Confirm" button to add the grid to the login page. Now, you have a view showing records connected to the logged in user.
Tip: You can always check the "Source" section of the view settings to confirm which records are being shown in a view.
4. Login to the Live App & Test
Now that you have set up your login page and view to show records connected to the logged in user, you can confirm it is working correctly by viewing the Live App. To view the Live App, click the "Go to Live App" button or the "Preview" button.
You can log in with a user record from the user role you used in this example. You should then see the view you added working with the records connected to your user.
How do I add a login to an existing page?
If you already have a page you want to use to show records connected to the logged in user, you can add a login by selecting the drop down next to the page name. There, you can select "Require Login", and you will be prompted to add a login.
Tip: You can learn more about logins here.
How do I add a view to an existing page?
If you already have login page in your app, you can add a view by selecting the "Add View" button while viewing that page. This will start the view setup process where you can create view that shows records connected to the logged in user.
Tip: You can learn more about adding and managing views here.
How do I show records connected to the logged in user's group?
It's also possible to connect your users to a company or organization and show records connected to that company. For example, a Teacher may want to login and only see students connected to their Department.