Laravel Multi-Aiuth with multiple table session set till user logout

久未见 提交于 2019-12-13 06:34:40

问题


I have implemented multi-auth in laravel-4.2 with multiple tables So accordingly my login view posts 3 values,1st is username,2nd is password and 3rd is a value from dropdown which is mathches the same value as Auth::<3rd Value>()->attempt,Which helps me to log in with requested table,And suppose if 3rd value is null then Auth::user()->attempt come in use. for multi auth I am using 'ollieread' pakage Auth Implementation code is

auth.php:

    <?php
return array(
    'multi' => array(
        'BSc' => array(
            'driver' => 'database',
             'table' => 'bsc',
            'model' => 'BSc'
        ),
        'user' => array(
            'driver' => 'eloquent',
            'model' => 'User',
            'table' => 'users'
        )
    ),
    'reminder' => array(
        'email' => 'emails.auth.reminder',
        'table' => 'password_reminders',
        'expire' => 60,
    ),
);

Edits in filter:

    Route::filter('students', function()
{
    if (Auth::BSc()->guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        }
        else
        {
return Redirect::guest('/');    }
    }
});

Route::filter('auth.basic', function()
{
    if (Auth::user()->guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        }
        else
        {
return Redirect::guest('/');    }
    }
});

UserController@postLogin:

 public function postLogin()
 {
  $username = Input::get('username');
  $model_name = Input::get('course');
  $password = Input::get('password'),

  $credentials = array(
    'username'    => $username,
    'password' => $password,
  );

   //class exists(Model exist?)
   if(!(is_null($model_name))){
    if(class_exists($model_name)){
     if(Auth::$model_name()->attempt($credentials))
     {
       return Redirect::to('desk')->with('stream',$model_name);
     }
    else
     {
       echo "Invalid UserName";   
     }   
   }else{
     echo "No model is created for ".$model_name;
   }
   }else{
     if(Auth::user()->attempt($credentials)){
      return Redirect::intended('desktop')->with('stream',"Test");
     }
     else{
      echo "Username is invalid";     
      }
   }

In postLogin function,at the redirecting to route "desk" I am passing variable $model_name to store in session which will help me know the user is from which table,

For first time on redirecting to /desk after login the $model_name shows the correct passed value,I retrieving the value in desk view by,

<?php
$stream = Session::get('stream');
var_dump($stream);   
?>

Now it dumps the correct value passed with redirect But as i refresh the page the value turns to null.

If I could able set this value till the user logs out,It would help to log out with that $stream value.

also can be used for:

Auth::$stream()->username

So How can set the session::get('stream') value constant till the user logs out? Hope I have explained correctly.


回答1:


In order to store the session value you may use the Session facade of laravel 4 as follows:

//class exists(Model exist?)

   if(!(is_null($model_name))){
    if(class_exists($model_name)){
     if(Auth::$model_name()->attempt($credentials))
     {
       Session::put('stream', $model_name);
       return Redirect::to('desk');
     }
    else
     {
       echo "Invalid UserName";   
     }   
   }else{
     echo "No model is created for ".$model_name;
   }
   }else{
     if(Auth::user()->attempt($credentials)){
      return Redirect::intended('desktop')->with('stream',"Test");
     }
     else{
      echo "Username is invalid";     
      }
   }

The value remains there in the session with the name (key) 'stream' until you you use Session::forget('key') or Session::flush() (which could be used in your logout function). You may also like to go through this page.

I hope this solves your problem. Please let me know if it didn't.



来源:https://stackoverflow.com/questions/36239771/laravel-multi-aiuth-with-multiple-table-session-set-till-user-logout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!