0
votes

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
If you just need to store a Session value you may use Session::put. - Vagabond
yes i was not getting the corrent way how should i put the variable in session and redirect to /desk. - Ajit Hogade
Right. Can you show me your logout function too? - Vagabond
I have done it somthing like this way $stream = Session::put('stream','myValue'); and i have just passed it like return Redirect::to('desk')->with($stream); and its there till user logs out,its done - Ajit Hogade
I wanted to have a look at your logout function. Anyways, I have tried to answer your problem. Please have a look at it and let me know. - Vagabond

1 Answers

0
votes

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.