Redirect user after login based on email

Is it possible to use kirby’s user authentication and then redirect specific users to specific urls?
I tried the following using the user.login:after hook, but I get an error on the login page saying “Invalid login”.

# /site/config/config.php

...
'hooks' => [
  'user.login:after' => function ($user, $session) {

    $usersMap = [
      "client-one@gmail.com" => "http://mysite.com/section-client-one",
      "client-two@gmail.com" => "http://mysite.com/client-two-page",
    ];
    if($user->role()->name() == "client"){
      if(isset($url = $usersMap[ $user->email() ])){
        go($url);
      } else {
        // show some error message
      }
    }

];
...

If this is possible, how would I:

  • display an error message if there’s no url corresponding to the user
  • protect those pages from non logged or wrong users

Are those users Panel users who log in via the Panel login view?

Also, your syntax is not correct in this line:

if(isset($url = $usersMap[ $user->email() ])){

==>

if ($url = $usersMap[$user->email()] ?? false) {
  go($url);
}

Yes they are panel users, and I already defined a role called “client”.
As first step I was going to implement it using the panel login view, maybe later I’ll add a custom login form.

I forgot: the users with the client role shouldn’t have any access to the panel at all but only be redirected.

I think you can’t do this from a hook, because a hook doesn’t return anything. If you implement a frontend login, you will be able to achieve what you want.

A custom login form would the way to go then?

Yes.