lonnieezell / myth-auth

One-stop Auth package for CodeIgniter 4
MIT License
637 stars 208 forks source link

login multiuser by their role #557

Closed AbidMuhammadTaufiq closed 2 years ago

AbidMuhammadTaufiq commented 2 years ago

I want to ask about the login filter by role, how do I make each role can have different direct view?. thank you

manageruz commented 2 years ago

Hi @L200180059 . Your question is a little bit confusing. Is your question about Myth:Auth's filters usage or it's about after successful authentication process ability to redirect to different views according by users roles?

manageruz commented 2 years ago

And the recommended place to ask questions about how to use the lib is to open new discussion.

AbidMuhammadTaufiq commented 2 years ago

Hi @L200180059 .

Your question is a little bit confusing. Is your question about Myth:Auth's filters usage or it's about after successful authentication process ability to redirect to different views according by users roles?

My point is about after succesful authentication process ability to redirect to different views according by users roles (point 2)

manageruz commented 2 years ago

Ok. In that case the quick way to do it is modifying AuthController's attemptLogin() method. Example code:

public function attemptLogin()
{
      // default code goes here ...

     // Is the user being forced to reset their password?
     if ($this->auth->user()->force_pass_reset === true) {
            return redirect()->to(route_to('reset-password') . '?token=' . $this->auth->user()->reset_hash)->withCookies();
     }

        // add logic to manage different views
       $authorization = service('authorization');
       if ($authorization->inGroup('admin', $this->auth->id())) {
            $whereToGo = route_to('admin_dashboard');
        } elseif ($authorization->inGroup('reguler_users', $this->auth->id())) {
            $whereToGo = route_to('user_dashbord');
        } else {
            $whereToGo = route_to('default_home');
        }

        $redirectURL = session('redirect_url') ?? $whereToGo;;
        unset($_SESSION['redirect_url']);
        // end of logic to manage different views code

        return redirect()->to($redirectURL)->withCookies()->with('message', lang('Auth.loginSuccess'));
}

The best way to modify AuthController is to extend it to your app/controllers folder. And don't forget to change login routes to reflect changes.

MGatner commented 2 years ago

It looks like this became a Discussion; can this Issue close?

manageruz commented 2 years ago

Yes, moved to discussions.

AbidMuhammadTaufiq commented 2 years ago

Yes sure