"Forgot Password" feature for my customers

I need to implement a “Forgot Password” feature for my customers on the front-end of my site but I’m not sure how to best go about that yet. What is the currently recommended way to do this? I saw in some other threads there wasn’t really a great solution at those times. Is there one now?

1 Like

There is no plugin that I know of, but @samnabi has implemented a password reset solution in Shopkit. See the register.php controller in /site/plugins/shopkit.

3 Likes

The resetPassword() function is in shopkit/site/plugins/shopkit/shopkit.php line 348 :wink:

… and check out the line 111 in shopkit/site/plugins/shopkit/registry/routes.php

2 Likes

Thanks @texnixe and @gillesvauvarin for pointing to the work @samnabi already has done in this area. I’ve got both bits added to my site local test site. It generates the link with the token perfectly. I’m noticing that in the route it allows customers into the panel to manage their account and reset their password. Does ShopKit limit the user to only see their own user account in the panel some how? If so, how is that done?


Update: I think I found it, though I’m not really sure how it works.

$kirby->set('role', 'customer', [
  'name' => 'Customer',
  'default' => true,
  'permissions' => [
    '*' => false,
    'panel.access' => true,
    'panel.access.users' => function() { return strstr(kirby()->path(), 'users/'.$this->user()->username().'/') ? true : false; },
    'panel.user.delete' => function() { return $this->user()->is($this->target()->user()) ? true : false; },
    'panel.user.read' => function() { return $this->user()->is($this->target()->user()) ? true : false; },
    'panel.user.update' => function() { return $this->user()->is($this->target()->user()) ? true : false; },
  ]
]);

You could use permissions: https://getkirby.com/docs/panel/permissions#available-permissions (panel.user.read, panel.user.update)

Edit: oh, great, you have already found it yourself. You can just use it as is (for the role you need), check out the docs for details.

That worked! I keep discovering new things Kirby can do. It’s so great!

A few related questions:

  1. When I saved a new password I was redirected to the panel login screen. Is there a way to redirect the user upon saving their password to the front-end login page?
  2. On the front-end I have a few requirements for passwords when creating an account but those aren’t enforced on in the panel. Is it possible to do so?
  3. I noticed when testing what the user could see in the panel that I got an error when clicking /users/ in the bread crumbs at the top.

Fatal error: Uncaught Error: You are not allowed to do this in … Stack Trace… Thrown in… /panel/app/src/panel/event.php on line 95

You would have to modify the core password method to enforce your rules.

Edit: As regards the breadcrumb: @samnabi hides the topbar completely in Shopkit, using a customer specific stylesheet.

1 Like

I see. Since my code isn’t in a plugin, where do I put that additional code to tweak the customer’s panel experience?

// Restrict panel access for customers
if ($user = site()->user() and $user->hasRole('customer')) {
  // Additional CSS for customers
  $kirby->set('option', 'panel.stylesheet', [
    '/assets/plugins/shopkit/css/panel.css',
    '/assets/plugins/shopkit/css/customer.css'
  ]);
  // Ensure customers don't stray onto the wrong page
  if (strpos($kirby->request()->url(),'/panel') and strstr($kirby->path(),'users/'.$user->username()) === false) {
    go(site()->url());
  }
} else {
  // Standard Shopkit CSS for customers
  $kirby->set('option', 'panel.stylesheet', '/assets/plugins/shopkit/css/panel.css');
}

Note: When I get around to remaking this site I think I’ll go with ShopKit. @samnabi has all of this stuff figured out. :wink:

1 Like

You can put this into the config.php file.

@texnixe Ok I added to my local config file. I was getting errors when using it straight from the plugin. I modified it to be this:

// Restrict panel access for customers
if ($user = site()->user() and $user->hasRole('customer')) {
    // Additional CSS for customers
    kirby()->set('option', 'panel.stylesheet', [
        'assets/css/panel.css',
        'assets/css/customer.css'
    ]);
    // Ensure customers don't stray onto the wrong page
    if (strpos(kirby()->request()->url(),'/panel') and strstr(kirby()->path(),'users/'.$user->username()) === false) {
        go(site()->url());
    }
} else {
    // Standard Shopkit CSS for customers
    kirby()->set('option', 'panel.stylesheet', '/assets/css/panel.css');
}

I copied the stylesheets from ShopKit to get to get this working and put them in assets/css/. While the stylesheets don’t seem to be working but the part that forces them to the homepage if they try to stray works perfectly. Any idea as to what I may be doing wrong?

1 Like

Can’t find anything wrong with the code, should work fine.

Removed and tried…

c::set('panel.stylesheet', 'assets/css/customer.css');

That doesn’t work either. Not sure why.

Does it work without the condition, if you just set the panel stylesheet?

Edit: Do you get an error message in your dev tools?

There are quite a few CSS errors but the source is all from files withing the /panel folder.

But no 404 file not found, it seems. Does the file get loaded, i.e. visible in the resources tab?

No 404 and no the file isn’t loaded at all.

Weird. So this line in config.php all on its own

c::set('panel.stylesheet', 'assets/css/customer.css');

is completely ignored? If the path was wrong, you’d get an error message. Could you please try this with a fresh starterkit?

(I’m not a bot, I need some sleep now :sleeping:)

Yeah it really is weird. Yes, that line is all on it’s own and yeah it seems to be completely ignored. I’ll try with a fresh 2.4.1 install like you suggested.

I’m sorry! Thanks for all the help, now get some rest!

Ok, tried with a fresh install and it worked using that single line in the config file. So that made me suspect a plugin. I then installed the Kirby Panel Brand plugin and it stopped working. Not sure how to fix it yet but that seems to be the culprit.

Edit: @jenstornell any idea why this might be happening?

Yes, I know why. The Kirby Panel Brand plugin is using registry set to change the stylesheet. If I remember correctly there can be only one custom panel stylesheet. I would like Kirby to handle panel styles more like routes, where you can just hang on more stuff to it (I wish the same for components).

A workaround for you could be to copy everything in the stylesheet and copy it to your stylesheet. In that case you also need to replace [text], [background] and [color] to set these yourself. I don’t think there is another way. To have different brands on different enviroments you could use different config files for different domains that points to different stylesheets.