Register
This is a walk through of registering a user
Overview:
This following code will collect user information by capturing it through html input tags in the register.component.html. We will store the inputs in a form object inside the register.component.ts using two way data binding, the form object will hold multiple data points to hold the following values firstName, lastName, email, password.
Once the data collected through the html file is bound to the typescript file, a user will click the sign up button. Once pressed, the button's (click) event will fire, which will invoke the register.component.ts's signUp function.
The register.component.ts's signUp function will then invoke the userService's registerUser function which will make a HTTP POST to the loopback server's /appUser api route, passing in the user credentials, aka the 'form' object passed by the register.component.ts.
Inside of the register.component.ts's signUp function we will subscribe to the result of the invocation of the userService's registerUser function. The subscribe function will accept a callback function as a argument. This callback function will run once the data has been sent back from loopback's server to the client. This callback function will accept a parameter, typically this parameter is called res, symbolizing the response from the HTTP request. In the callback function's scope we will store some of the response's attributes, such as the user's id and a token, to the client's local storage.
We will then use this token value to hit other api routes in our loopback server to communicate database. This token is a security feature to ensure accessibility only to certain people. This will be discussed in more depth later in the course.
Prerequisites:
Install LoopbackCreate a MongoDBConnect Loopback to MongoDBSetup Token AuthenticationCreate User ModelsOpen your front end angular application in a code editor
Open a terminal and navigate to your
Create A Register Component
Create A User Service
serviceName should be called user
References
Intro to ServicesImportant Checks:
This service should be automatically imported into
app.module.ts, check to be sureMake sure that we have this service declared inside the
app.module.tsproviders array
Import User Service & Router Inside Register Component
Initialize User Service & Router Inside Register Component Constructor
Collect User Input & Store It In The Register Component
Create Input Fields On The
register.component.htmlFileCreate A
formObject Insideregister.component.tsThis
formobject will hold the following fields:firstName
lastName
email
password
Lines 13 through 18 on the
register.component.tsfile express how to create this form objectCreate a button to invoke the
register.component.ts'ssignUpfunctionCreate a
signUpfunction inside theregister.component.ts
Reminder we have done this before check the following website for help:
Login Page ChallengeImport HttpClient In user.service.ts
HttpClient In user.service.tsInitialize HttpClient Inside user.service.ts Constructor
HttpClient Inside user.service.ts ConstructorIn The User.service.ts File Create The following Attributes in the UserService Class
User.service.ts File Create The following Attributes in the UserService ClassCreate A registerUser Function In The User.service.ts File
registerUser Function In The User.service.ts FileThis function will accept a userCredentialsparameter.
This function will make a post request to the loopback server's localhost:3000/api/appUsers/route, which will then submit the userCredential data to the mongo database.
Alter the signUp function inside register.component.ts
signUp function inside register.component.tsThis function signUp will invoke the userService's registerUser function, passing in the user credentials, aka the register.component.ts's form object.
When we subscribe to the observable we will do the following
populate session storage with the responses token attribute
populate session storage with the responses userId attribute
redirect to home using the router
Session Storage Reference:
Try Creating A New User
Important Notes:
StackBlitz Example
Last updated