A little secret to rock your YouTube subscribers
Get Free YouTube Subscribers, Views and Likes

Authentication in Node.js - #4 Registration

Follow
Code Realm

In this video, we're going to tackle the first core feature of user authentication, that is registration. When signing up with our app, a user would need to submit some personal data, which we're going to validate with Joi. @hapi/joi is a library for serverside validation with a very expressive and elegant API. We'll also have a bit of custom logic to check for the existence of a user account by its email address.

An important thing to note is that email addresses are effectively caseinsensitive, since most email providers will cast the mailbox name to lowercase. As such, the email will most likely get delivered regardless of the casing. In our application, data integrity is a must, so we need to avoid mixed casing as it enables users to sign up repeatedly with the same email address. We'll also come back to password validation and hashing in just a bit. As a reminder, never store plaintext passwords in your database!

One last touch we'll add is middleware. Borrowing Laravel's vernacular, we'll attach guest (and soon auth) middleware to our register endpoint. At this point, we'd be able to initiate user sessions and issue session cookies upon successful registration. You'll notice that repeated sign up requests cause the server to hang up. That's because we need to wrap async code with trycatch and forward any errors to Express using the next function. We'll address this issue with error handling in the very next video.

As mentioned before, please be sure to review Session Authentication in Express    • Session Authentication in Express   before watching this, as I'll be jumping over expresssession assuming you've already seen how to use it from the older tutorials on the channel. It goes without saying, but please be sure to also follow along with the previous videos in the series, as they build up on each other (see the playlist).

@hapi/joi https://www.npmjs.com/package/@hapi/joi (used to be joi up until v15)

mongoose https://www.npmjs.com/package/mongoose

Project repo https://github.com/alex996/nodeauth

posted by CydayCitambumyy