Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5.2 Insert login session data to database on custom auth

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');
like image 405
Hassaan Avatar asked Jan 07 '18 18:01

Hassaan


1 Answers

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.

like image 114
lintabá Avatar answered Oct 13 '22 21:10

lintabá