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: .. code-block:: bash 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: .. literalinclude:: ../references/auth.php :language: php :lines: 67-77 :emphasize-lines: 3 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: .. literalinclude:: ../references/User.php :language: php :lines: 1-10 :emphasize-lines: 5 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 :ref:`Quick Usage Example ` you can add the `defaultUseMasterKey` property to a model: .. literalinclude:: ../references/User.php :language: php :emphasize-lines: 20 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: .. image:: ../images/register-form-filled.png .. image:: ../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`): .. literalinclude:: ../references/RegisterController.php :language: php :lines: 48-55 :emphasize-lines: 5 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: .. literalinclude:: ../references/AppServiceProvider.php :language: php :lines: 5-21 Back to the `RegisterController` you can use the validation rule like so: .. literalinclude:: ../references/FinalRegisterController.php :language: php :lines: 48-55 :emphasize-lines: 5 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. .. image:: ../images/parse-user-peter.png .. image:: ../images/register-form-filled.png .. image:: ../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: .. literalinclude:: ../references/en.php :language: php :lines: 83 .. image:: ../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. .. image:: ../images/parse-user-petra.png .. image:: ../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`): .. literalinclude:: ../references/FinalRegisterController.php :language: php :lines: 63-71 :emphasize-lines: 6-7 .. 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: .. image:: ../images/parse-user-petra-logged-in.png