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 server-side 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 case-insensitive, 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 try-catch 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 express-session 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/node-auth
Смотрите видео Authentication in Node.js - #4 Registration онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Code Realm 25 Декабрь 2019, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 12,812 раз и оно понравилось 191 людям.