Creating a login system with Jetstream & Livewire

For this project I will be using Jetstream and Livewire to create a killer login system. This process couldn't be simpler, only requiring 3 lines of code.


composer require laravel/jetstream

php artisan jetstream:install livewire

npm install && npm run dev


and voila! a working authentication system! but let's make some personal tweaks..



Perfect...






When signing up to Reddit.com, you are asked for your name AND username, which is not available out the box with livewire, so we must adapt!

Firstly, I added a new field in the Livewire's register.blade.php form to take in 'username'. The issue now being that Reddit does not allow spaces in their usernames.

A quick bit of validation in Fortify's 'CreateNewUser' action using the argument 'alpha_dash' means that usernames may be alpha-numeric and also contain dashes and underscores.




And make sure to persist these changes below!

At this point, the form appeared to be displaying correctly, and validation was functioning, not allowing any spaced usernames, but the username was not persisting to the database.

Being very confused - I started browsing all the files Livewire provides for Authentication, until I had a peek in the User model. 

So, the User model provided has a technique which is new to me, in which all the 'fillable' fields are stated. With some research, this technique is that of taking privacy measures for data, so that the fields that are 'mass assignable' are stated. a counterpart to this is stating the 'guarded' fields - which means the fields which are not assignable (such as user_id, or user_type) so that no malicious user could 'make' themselves an admin or such.

Anyway, this had to be changed so that 'username' is fillable.

we can now store and access the users username with {{$user->username}}

Handy for the navbar:



A substantial start!


before we tackle authorisation and start uploading content to the site, we must go deeper in to the weddit-verse... 


by creating...a subweddit




Comments

Popular posts from this blog

Validation

Views and Policies