P
P
pemi2017-07-13 09:28:23
Laravel
pemi, 2017-07-13 09:28:23

How to rewrite authentication in Laravel?

I need such authentication - there is a table with fields network, id_in_network. I need to add a method for the Laravel authentication module that takes $network, $idInNetwork and looks for a match in the table. If there is one - it logs in the user, does not find it - creates a record and also logs in. How to implement this beautifully? Without crutches, etc. ? The vendor folder is not under version control and editing it itself is not recommended. Therefore, how can I add such a method then? Where to create a file for adding this method?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2017-07-13
@xpert13

Where to create a file for adding this method?

In the controller that should be responsible for this action. The default is LoginController, but this is optional, you can use any controller.
To authorize a user, use the following method:
Where $user is the current user model. Checks for existence in the table of records and creation, if necessary, I think you will do it yourself.
If the "table with the fields network, id_in_network" should act as user models, then you still need to describe it in the "config/auth.php" file (be guided by how this is done for the standard user model)

I
Ivan Kolesnikov, 2017-07-13
@kolesnikov_i

If I correctly understood the task, then you do not need to rewrite the authorization.
There are events in Laravel that you can use before user authorization.
https://laravel.com/docs/5.4/authentication#events
You think you need to listen to the event Illuminate\Auth\Events\Attempting
When you create a Listener for this event, in it, you can check what you need and log in the user.
Or use the event Illuminate\Auth\Events\Login, because you will log in the user in both cases, and after login you will already make the necessary entries in the database if necessary.

A
Alex Kyznetsov, 2017-07-13
@360157

As far as I know, since version 5.3, controllers come in a box, and if you need to change the conditions, then just create an authController with the parameters you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question