Member Registration and Login
In this article you can learn about how to create Member registration and login functionality for the frontend of your application.
Last updated
In this article you can learn about how to create Member registration and login functionality for the frontend of your application.
Last updated
Using tools available on a fresh install of Umbraco CMS, you can create a frontend-based registration and login functions and restrict access to specific areas of your site based on this system.
By the end of this tutorial, you will know how to implement a basic register/login functionality on your website, hide pages from non-logged-in members, and assign newly registered members to specific member groups.
Make sure you have the latest dotnet templates installed.
Create an empty directory, open a terminal and run:
Complete the installer and login to the backoffice.
Navigate to the Settings section in the backoffice.
Locate the Partial Views entry under Templating in the left-hand section.
Click the "+" to create a new partial view.
Choose New partial view from snippet...: in the dialog that follows
Pick the Login snippet in the next dialog.
Name the partial view "Login" and save it:
Repeat the above steps using the Register Member and Login Status snippets. Save the partial views as "Register" and "LoginStatus", respectively.
The Partial Views list should now look like this:
To render these partial views, we need a new Document Type with a dedicated template (see also Defining Content):
Create a new Document Type with a template and name it "Login".
Setup the "Login" Document Type to be composed by the "Content Base" and "Navigation Base" Document Types.
Allow the "Login" Document Type as a child under the "Home" Document Type.
Locate the newly created "Login" template, and overwrite its content with this:
Halfway there!
Navigate to the Content section.
Create a new page based on the "Login" Document Type Under the Home node:
Save and publish the page.
The Register and Login functionality is rendered by the "Login" template:
You can now use the page to register new Members. Every registered person will show up in the Members section in the backoffice:
The "LoginStatus" partial view comes into play after registering as a new Member (or logging in as an existing Member). It will render a welcome message and a "log out" button:
In a real-life scenario, you probably don't want all this functionality on a single page. However, you can still use the partial views as a basis for your own implementation.
If you are on Umbraco Cloud you need to do the following steps to be able to restrict access for your users:
Go to the Users section in the Backoffice.
Select your User.
Once you have added the "Sensitive Data" group go to the Members section in the backoffice. In the Members section you need to select each member and approve them by toggling the Approved button.
Once the users have been approved, you can go ahead and continue the tutorial.
Now that we have the options to:
Register a member
Log in as a member
Check the current login status
Log out a member
We can take this a bit further and specify which parts of our website should be accessible to logged-in members. To do this:
Go to the Member section in the Backoffice.
Create a new Member Group.
Give the group a name.
Save the Member Group.
Navigate back to the created Member.
Assign the newly created Member Group.
Save the member:
Almost there!
Navigate to the Content section.
Create a new page that should only be visible to "Premium" members.
Click the menu icon (•••) to bring up the page options, and pick "Restrict Public Access" once created.
You will be able to restrict access to a specific member, or a specific group. Choose the latter option. In the dialog that follows you must define:
The group(s) that will have access to the page.
The page with the login form.
The page to display if the content page is inaccessible to the logged-in member.
It is recommended to have a dedicated page for the "No access" page - though you can use any page you have.
Congratulations! With all of that setup, the "Premium Content" page is only accessible to logged-in "Premium" Members. When not logged in, the website visitors will automatically be redirected to the "Register/Login" page.
However, with the above approach, members will not be assigned to any group automatically. For this to happen, we would need to write a bit of custom code.
We can leverage the built-in Notifications to handle the automatic Member Group assignment. Specifically the MemberSavedNotification
, which is triggered whenever a Member is saved.
This notification is triggered when any Member is saved. Make sure test its usage carefully.
The following code automatically assigns Members to the "Premium" Member Group.
Member Groups are also referred to as "Roles" in the service layers.
To enable the notification handler, we also need a composer:
Add the "Sensitive Data" Group.