Call a function after login fails or success laravel 5.3 - laravel

I want to call a custom function when the user success in login and another when fails but i can't find the solution, i use laravel 5.3 and make:auth method.
I have the LoginController but is almost emtpy.
The route for login post:
Route::post('login', ['as' => 'login.post', 'uses' => 'Auth\LoginController#login']);
I have a helper function called flash('The messaje') that sends an jquery alert on the view, i use in controllers and works fine, but i don't know where put this code to show an alert if the auth login fails and success.
Thanks!

As by default LoginController uses AuthenticatesUsers trait, all you need to is to write custom methods sendLoginResponse and sendFailedLoginResponse. You can of reuse trait methods and to add only custom mode you want to execute.

Related

How to change logout method to GET in laravel 6?

I'm using laravel 6 and I want to change the method of logout auth. How I can do that?
I run :
php artisan route:list
and I get default laravel logout method is POST :
Route::post('/logout', 'LoginController#logout');
and I want change to GET :
Route::get('/logout', 'LoginController#logout');
How I can change the route method ? And where I can custom the logout controller ?
In your routes.php, add Route::get('/logout', 'LoginController#logout'); to the top of Auth::routes() so it overrides the default logout route.
In your LoginController#logout() method: simply do auth()->logout() and redirect as appropriate

how to remove register and login form routes and leave everything else?

I am using Laravel 5.8 and I need to remove the routes that lead to login and registeration form but I want leave everything as it is (verify email, reset password) would you tell me how to do this kindly?
If you have ever run this command to generate the authentication files
php artisan make:auth
and you no longer need the files for the reason that you are doing login and registration using ajax, then you may have to remove the views associated with the files and remove some routes in the associated controllers that you do not need.
For instance, if you do not need the login and registration forms and the associated routes, you can follow the outlined steps below to remove them.
If you are in doubt of the routes that you need, first run this artisan command:
PHP artisan route:list
to see the details of the available routes and note the ones you need.
Thereafter, delete the auth route definition in routes/web.php
Auth:routes();
and declare the routes you need explicitly. In your case you should need post routes for registration/login and also the email verification routes:
Route::post('login', 'Auth\AuthController#login');
Route::post('logout', 'Auth\AuthController#logout');
Route::post('register', 'Auth\AuthController#login');
Route::get('email/resend', 'Auth\VerificationController#resend')->name('verification.resend');
Route::get('email/verify', 'Auth\VerificationController#show')->name('verification.notice');
Route::post('email/verify/{id}', 'Auth\VerificationController#verfy')->name('verification.verify');
Next, remove the views contained in the following default locations
resources/views/auth/login.blade.php
and
resources/views/auth/register.blade.php
These are the views for the respective forms.
Then remove showLoginForm() and showRegistrationForm() from
app/Http/Controllers/Auth/LoginContoller.php
and
app/Http/Controllers/Auth/RegisterController.php`
respectively.
I am using Laravel 8 and this is how you can achieve it
Auth::routes([
'register' => false, // Register Routes...
'login' => false, // Login Routes...
]);

Restricting access to a page if user isn't logged in with Laravel 5.4

I've created a Dashboard page and i want it to be accessed only if an user is logged in. The login and register were made with the php artisan make:auth command. Any tips or ideas how to accomplish this ?
Just add the Auth middleware to your route
Route::get('dashboard', function () {
//whatever
})->middleware('auth');
Make sure you have
use Auth; in your routes
In Your web.php route, you can add
Route::get('complaintSuggestion', function() {
return view('patials.complaintSuggestion');
})->middleware('auth');

Laravel 5.2 middleware auth not working as I expect

I've created the basic application used make:auth
I then renamed HomeController to MembersController
When assessing the controller it redirect to the home page but it only does this when
//$this->middleware('auth');
isn't commented. It works fine when uncommented, with the exception theres no authorization.
Any ideas on why its behaving this way
Go to app/Http/Middleware/Authenticate.php then change the redirect URL,where you want to redirect after login

Laravel 5.1 flash message after register

I'm using Laravel's out of the box authentication, which came with laravel 5.
Basically, my AuthController is empty, and all the logic happens within the AuthenticatesAndRegistersUsers trait.
Now I want to flash a message only after a user is registered.
I can change the trait, but it's not recommended, and might not work on future versions of Laravel.
There is a way to run my custom code from the Controller after user registers?
Just copy the trait into your controller, Laravel will use the method in your controller instead of the trait because it's kind of inheritance, that way you won't touch the trait and you will be sure that the method of the flash message will work on any version of Laravel

Resources