Wednesday, January 17, 2018

laravel 5.2 Insert login session data to database on custom auth

Leave a Comment

I have a custom auth login in laravel 5.2, my config for custom login is

'guards' => [         'web' => [             'driver' => 'session',             'provider' => 'users',         ],          'provider' => [             'driver' => 'session',             'provider' => 'providers',         ],     ], 

I have two auth controllers, One is laravel AuthController and other is ProviderAuthController. I have set SESSION_DRIVER=database in my env and also created a session table in my database I am getting sessions from web login but problem is that I am not able to get sessions on provider login. Is there any workaround to insert session on provider login.

There is nothing in my app service provider

<?php  namespace App\Providers;  use Illuminate\Support\ServiceProvider;  class AppServiceProvider extends ServiceProvider { /**  * Bootstrap any application services.  *  * @return void  */ public function boot() {     // }  /**  * Register any application services.  *  * @return void  */  public function register()  {     //  } } 

The route for my providerAuthcontroller are Route::group(['prefix' => 'provider'], function(){

Route::get('login', 'Auth\ProviderAuthController@showLoginForm'); Route::post('login', 'Auth\ProviderAuthController@login'); Route::get('logout', 'Auth\ProviderAuthController@logout');  // Registration Routes... Route::get('register', 'Auth\ProviderAuthController@showRegistrationForm'); Route::post('register', 'Auth\ProviderAuthController@register');  // Password Reset Routes... Route::get('password/reset/{token?}', 'Auth\ProviderPasswordController@showResetForm'); Route::post('password/email', 'Auth\ProviderPasswordController@sendResetLinkEmail'); Route::post('password/reset', 'Auth\ProviderPasswordController@reset'); 

1 Answers

Answers 1

Its supposed to happen in the middlewares, not in the providers.

Route::get('profile', function () {     // Only authenticated users may enter... })->middleware('auth'); 

and

class Auth {   public function handle(\Illuminate\Http\Request $request, Closure $next)   {     if(!$request->session()->get('authenticated'){       throw AuthException();     }     return $next($request);   } } 

Also check laravel lifecycle.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment