Let's start implementing features into our Application:
- Setting up API URL
- Setting up custom middleware to check users' Auth status
- Registration via API
- Login via API
By the end, we should have a working Login/Registration system.
Setting Up API URL
First thing we need to do is add our API URL to our .env file:
.env.
# ... API_URL=http://nativephp-events-app-api.test/apiOf course, we should never load the .env file in our code, so let's set up a config file for this:
config/services.php
// ... 'slack' => [ 'notifications' => [ 'bot_user_oauth_token' => env('SLACK_BOT_USER_OAUTH_TOKEN'), 'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'), ], ], 'api' => [ 'url' => env('API_URL'), ], ];Now, we will be able to get our API URL by calling:
config('services.api.url')Creating Custom Middleware
Now we have to think about our sessions. The typical auth middleware won't work here, as we are authenticated via token. So let's create a new one:
php artisan make:middleware ApiTokenExistsAnd inside it, we'll add:
- A check to see if we have
tokenset in our session - An API call to load the user Profile