How to Build User Authentication with Laravel
Before we can start doing anything we need to first install laravel on our system, you can check out the laravel documentation to see the steps. When you are done installing laravel on your system, you can now go to the .env file and create a database on MySQL SQLite or any other one you are using. When you are done with creating your database you then go into your .env file and input your database name the database user name and password if you have one it should look like this:
DB_DATABASE=testing_swapi
DB_USERNAME=root
DB_PASSWORD=secret
Then go to the user model in the app directory and input the following options(the model would contain the logic to contain the user information) so you should see a file called User.php if you don’t see this you can make use of the artisan command in your command line as follows (php artisan make:model User), when you open the file you would then create the logic for inputting the user details as follows :
protected $fillable = [
'firstName', 'lastName', 'gender', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
protected $table = 'users';
protected $primaryKey = 'id';
The variable called fillable is for the visible user input in the authentication form, the hidden variable is for the inputs that are not visible, while the cast variable simply means changing a value from one value type to another, the table variable means to store the data on the users table, and the primary key means the id of the user registering.
Then we proceed to create a database migration table for the user by using the command line to input: php artisan make:migration create_users_table, then laravel should create a migration file in the migrations folder that would serve as the table for the users in the database then we input those attributes for the fillable in our model into the users table so that it stores in an appropriate fashion in the database.
So when we open the create_users_table in our migrations folder we then input the following options in it:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('firstName');
$table->string('lastName');
$table->string('gender');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Then this is for the storage area for the firstName of the user, the lastName of the user, gender of the user and email of the user. These are how the user information would be stored on your DB that you created earlier.
After you are done with this you then create a controller for the user also using the artisan command in the command line write php artisan make:controller UserController
Then head over to your controller file which is located in the Http directory, open the UserController file. The controller is the component that checks for validation, which means that it checks if the user input matches the required criteria then if it does it proceeds to save the data in the migration table in your database but if it doesn’t match it should throw an exception(error). When you open the controller file then you should write the following validation code
class UserController extends Controller
{
public function signUp(Request $request)
{
$this->validate($request, [
'firstName' => 'required',
'lastName' => 'required',
'email' => 'required|email|unique:users',
'gender' => 'required',
'password' => 'required'
]);
$user = new User([
'firstName' => $request->input('firstName'),
'lastName' => $request->input('lastName'),
'email' => $request->input('email'),
'gender' => $request->input('gender'),
'password' => bcrypt($request->input('password')),
]);
$user->save();
return response()->json([
'message' => 'user successfully created!'
], 201);
}
}
Then after you are done with this you then start up your server again using the artisan command, so in the command line, you should write php artisan serve.
Then you head over to postman put in your URL provided by your server put the fields in your model in the body in JSON format and then make a post request,
Then it should save successfully in your database, to confirm you can just head over to your database on PHPMyAdmin and see it works.