Authentication

Laravel-Parse authentication driver works perfectly in combination with the Laravel authentication service. So please ensure that you already enabled Laravel’s authentication service before you continue on:

php artisan make:auth

This command should be used on fresh applications and will install a layout view, registration and login views, as well as routes for all authentication end-points. A HomeController will also be generated to handle post-login requests to your application’s dashboard.

Parse Authentication Setup

Before you can use Laravel-Parse authentication driver, you will need to make some little tweaks to settle everything up. Please follow the instructions given in this guide in order to learn how to setup the authentication.

Step-01: Change Authentication Provider

First you will need to replace the authentication provider and change it with the parse value. This can be done within the authentication (config/auth.php) configuration file, within the providers.users array key:

    'providers' => [
        'users' => [
            'driver' => 'parse', //'eloquent',
            'model' => App\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

Step-02: Change User Authentication

Next you will need to change the binding between Laravel’s authentication and the user model with that one from Laravel-Parse. Open the User model (App\User) in order to make the changes:

<?php namespace App;

use Illuminate\Notifications\Notifiable;
//use Illuminate\Foundation\Auth\User as Authenticatable;
use Sirthxalot\Parse\Auth\UserModel as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

Step-03: Enable Master Key For User

In order to avoid permission problems it’s a good practice to provide the User with the master key usage. As you may learned in the Quick Usage Example you can add the defaultUseMasterKey property to a model:

<?php namespace App;

use Illuminate\Notifications\Notifiable;
//use Illuminate\Foundation\Auth\User as Authenticatable;
use Sirthxalot\Parse\Auth\UserModel as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Default Master Key
     *
     * Defines the default value of `$useMasterKey` throughout all class methods,
     * such as `query`, `create`, `all`, `__construct`, and `__callStatic`.
     *
     * @var mixed $defaultUseMasterKey
     * A mixed that determine the default master key.
     */
    protected static $defaultUseMasterKey = true;
}

Unique Users Validation

The authentication has been setup correctly but still if you are going to use Laravel’s authentication you run into some errors. Lets see why some errors occur:

Open your registration form in order to register a new user by hitting the /register route in your browser, fill out the form and hit submit - you should receive a confusing exception:

../_images/register-form-filled.png ../_images/exception-sql-register.png

About The Exception

This exception is a little bit tricky so lets see what is going on exactly. First of all you can see that this is not an exception from Laravel-Parse it is a Laravel exception. But this still doesn’t give you the answer.

The exception will be thrown within the RegisterController due the validation. It tries to run desperately some queries to find a user in a database? Hmm ... But why does the validator tries to fire queries to a database?

The answer can be found within the validation rules to register a new user (app/Http/Controller/Auth/RegisterController.php):

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

The error occurs with the unique:users rule which will check if a user already exists, using a database query. By removing the rule you should fix this error, but this will bring another problem. We need a replacement for this validation or the validator keep throwing exceptions when a username will be reused for register.

Custom Validation Rule

The truth is that Laravel-Parse doesn’t have a validation rule straight out of box. But I can show you how to add your custom validation rule like a bed of roses.

Open the AppService provider (app/Providers/AppServiceProvider.php) in order to extend the boot method with the custom validation rule:

use Illuminate\Support\Facades\Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('unique_parse_user', function ($attribute, $value) {
            $query = new \Sirthxalot\Parse\Query('_User', 'App\User', true);
            $result = $query->where('username', $value)->first();
            return ! (bool) $result;
        });
    }

Back to the RegisterController you can use the validation rule like so:

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique_parse_user',
            'password' => 'required|min:6|confirmed',
        ]);
    }

If you now try to register a user which already exists than you will see the affects from the validation rule as expected:

Hint

If you are wondering why I have a user stored in Parse. I did that manually behind the scenes using the Parse Dashboard. So I added a new user within Parse Dashboard using the same credentials as I use to register. Just if you followed this guide and getting confused.

../_images/parse-user-peter.png ../_images/register-form-filled.png ../_images/custom-validation-rule.png

Ta-da! The validation rules work out as expected - good job! But the validation message is a little bit disturbing, so lets fix that as well. You can do that by adding the unique_parse_user key to the resources/lang/en/validation.php file:

    'unique_parse_user'    => 'The :attribute has already been taken.',
../_images/custom-validation-rule-fixed.png

Cannot sign up user with an empty name

But even when you will register a new user, which doesn’t exist in Parse at this point, you still will run into an exception. The difference between this exception and that one you will receive if you try to register an existing user, is that this exception will be thrown by Laravel-Parse.

../_images/parse-user-petra.png ../_images/exception-cannot-sign-up-user-with-an-empty-name.png

I need to rewrite this exception message since you may getting confused with the name term. The problem is not the name it is the username which is missing. All Parse Users will need to have a username in order to sign up - its a Parse thing.

Workaround: Use E-Mail As Username

There are multiple ways to solve this problem, but I would like to show you only a quick workaround. I assume that you can add your custom fields by your own, so to keep everything simple here’s the workaround (app/Http/Controllers/Auth/RegisterController.php):

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'username' => $data['email'],
            'password' => $data['password'],
        ]);
    }

Warning

You should always remove the bcrypt function from your password since this is not necessary when using Parse.

Register a new user and everything should work as expected. Even the name will be used:

../_images/parse-user-petra-logged-in.png