Challenge: Make the app a multi-user application
Anvil provides a built-in user authentication service that takes care of sign-up, sign-in and user permissions for you. Right now, anyone who opens up our news aggregator app can add, edit and delete all articles.
In this challenge, we’ll turn our app into a multi-user application so that users have to sign-in before they can access the app.
Add the Users Service
In the Sidebar Menu, click the blue plus button and add the Users Service. You’ll see a screen with different configuration options and an empty table of Users at the bottom.
Configure the login process to your preference. Should new users be able to use their accounts right away? Should users be able to login with Google?
Present a login form
Next, modify the
__init__ of your Homepage code to present users with a login form:
For more information and help with login forms, see the documentation: Anvil Docs | Presenting a Login Form.
Link news articles to users
Now, add a column to your Articles table that links to a row of the Users table (if you’ve forgotton how to do this, revisit step 4 of this tutorial) and update your
add_article server function to store the currently logged in user when adding a row in the Articles table.
(Hint: You can get the currently logged-in user with
anvil.users.get_user(). Read more in the documentation: Anvil Docs | User Permissions)
Display the user’s articles
Next, modify the
get_articles() server function to only return rows from the Article Data Table where the currently logged-in user matches the user in the Users column.
Allow users to log out
Finally, add a Link or a Button to the app bar in your app and create a click event on it that logs out the current user with
anvil.users.logout(). Also in that click event function, reload the app so that the login form is displayed.
(Hint: You can do this with
open_form. Learn more about navigation in the documentation: Anvil Docs | Navigation)
Try it out
Test out your app by creating multiple accounts and adding news articles. Stuck? Why not ask a question on the Anvil Community Forum?
For a walkthrough of this challenge, check out the full tutorial on extending the news aggregator app to be a multi-user application: