cartalyst / sentinel

A framework agnostic authentication & authorization system.
BSD 3-Clause "New" or "Revised" License
1.51k stars 238 forks source link

AppserviceProvider Dynamic Set Model #452

Closed Newdelay closed 5 years ago

Newdelay commented 5 years ago

Hi, I got a problem. how I make dynamic setmodel in AppserviceProvider for Runtime.

I have two authentication in my project, and two login dealers and customers. that's why I need dynamic model.

I tried this.

DEALERS LOGİN CONTROLLER

`try{

  $credentials = [
    'username' => $request->username,
    'password' => $request->password,
  ];

  Sentinel::setModel('App\Models\Smes');

  if(Sentinel::forceAuthenticate($credentials)){

    $lang = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
    Session::put('locale', $lang);
    // login type = 1 -> user / login type = 2 dealers.
    Session::put('login_type','2');

    return redirect('/panel');
  }
  else{
    return Toastr::error('Kullanıcı adı veya şifre yanlış.', 'Hata', ['timeOut' => 10000]);
  }

}
catch(\Exception $e){
  return Toastr::error('Hata', 'Hata', ['timeOut' => 10000]);
}`

CUSTOMERS LOGİN CONTROLLER

`$credentials = [ "username" => $request->username, "password" => $request->password, ];

    if (Sentinel::forceAuthenticate($credentials)) {

        $lang = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
        Session::put('locale', $lang);
        // login type = 1 -> user / login type = 2 dealers.
        Session::put('login_type','1');

        return redirect('/panel');
    } else {
        return redirect('giris')->with('error', 'Kullanıcı adı veya şifre yanlış.');

    }`

I created a session login_type, if login type == 1 then user customer. else dealers.

AppserviceProivider

`public function boot() {

  if(Session::get('login_type') == 2){
  Sentinel::getUserRepository()->setModel('App\Models\Smes');
  Sentinel::getPersistenceRepository()->setUsersModel('App\Models\Smes');
  }
  else{
    Sentinel::getUserRepository()->setModel('App\Models\Users');
    Sentinel::getPersistenceRepository()->setUsersModel('App\Models\Users');
  }

}

`

But Session Always Null.

talal424 commented 5 years ago

i would recommend using single authentication system and use roles to determine user identity if you are lazy ( like me ) and dont want to use roles you can alter the table users and add new column and add it to the model then you can simple check by $user->newColumn === 'customer'

brunogaspar commented 5 years ago

As @talal424 suggested, use a single system, which seems better, but if it that doesn't work for you, try to call the setters within a middleware perhaps? That way you can possibly detect which one to apply using session or something else.