Studio: Spa User Signup
For this studio you will add functionality to allow users to sign up for your SpaDay app.
The starter code has been modified from where you left off last class. Grab the refactored code on the user-signup-starter branch .
You’ll notice in this branch that the name has been removed from the service selection form. Once we
implement user-signup functionality, we can use a given user’s name to identify the spa client. We’ve also
moved data into a Client model and out of the SpaController class.
In this studio, we’ll ask you to write another model, User. User and Client may at first
appear redundant, but in the future as you develop your spa application, you may find a scenario where
a user is logging in and is not also Client.
Getting Ready
Within SpaDay, create the following files.
- Create a
UserControllerinControllers. - Create a new folder,
User, withinViews. - Create
Index.cshtmlandAdd.cshtmltemplates withinViews/User/. - Create a
Userclass withinModels.
Creating the Model
Your User class should have a few properties: Username, Email, and Password.
Rendering the Add User Form
In the
UserController, create an action methodAdd()to render the form. This action method should correspond to the path/user/add, and for now, it can just return theAdd.cshtmlview.TipDon’t forget to add
/user/addto your path when you test your new features.Within the
Add.cshtmltemplate, create a form that accepts inputs for each of theUserclass properties. Include an additional password input field to verify the password input. The form should be set up toPOSTto/user.Be sure to set
type="password"for the password and verify inputs, to ensure the passwords are not visible when being typed into the form. You can also settype="email"on the email input, which will enable some basic client-side validation. We’ll tackle validation in more detail in the next lesson.
Processing Form Submission
Within the
UserController, create an action method with this signature:public IActionResult SubmitAddUserForm(User newUser, string verify) { // add form submission handling code here }This will use model binding to create a new user object,
newUser, and pass it into your action method.NoteYou don’t need to store the
Userobject anywhere for this studio. We’re focusing on form handling and validation in this exercise. If you want to keep track of users using the method we employed in the models lesson video, check out the Bonus Missions below.Check that the
verifyparameter matches the password within thenewUserobject.- If it does, store the user’s name in a
ViewBagproperty and render theUser/Index.cshtmlview by returningView("Index"). - If the passwords don’t match, render the form again.
- If it does, store the user’s name in a
Refining Form Submission
Once registered, we want the user to access the form selecting their spa services and see a personalized welcome message!
- In
User/Index.cshtml, add anh1element with a welcome message. Use theViewBagproperty containing the user’s name to personalize the message! - Also in
User/Index.cshtml, add anaelement to take the user back to the path,/spa, where theSpa/Index.cshtmltemplate will be rendered.
- In
If the form is re-rendered when a password is not verified, we should let the user know that their form was not properly submitted. Add an
errorproperty toViewBagletting the user know that their passwords should match. This property will need to correspond to an element in the template that will only render the error text when the passwords do not match.If we send a user back to re-populate the form, it would be nice to not clear their previous submission. We won’t need to save the password entries in this fashion.
In the form submission action method, add the
usernameandemailfields of the submitted user asViewBagproperties.Back in the form, add a value attribute to these form fields and make them equal to the
ViewBagproperties.
Bonus Missions
- Add a
Dateproperty inUser, and initialize it to the time the user joined (i.e. when theUserobject was created). - At the bottom of
User/Index.cshtml, add adiv. Inside that element, add account details such as the user’s email and the date they joined.