Best practice for overriding routes (specifically, create-account)?

105 views
Skip to first unread message

csk

unread,
Aug 4, 2021, 10:31:54 AM8/4/21
to Rodauth
Hello all,

I'm playing with Rodauth, trying to get comfortable with it, and I'm wondering if there's some guidance for overriding entire routes.

The usecase: I want to have custom views and fields for creating accounts, and it's starting to feel it'll be easier to override the entire default thing than to try and customize it via methods and hooks. For instance, I would like to run all validations (or at least the ones I explicitly define) and present all the errors to the user, similar to what Rails does. I found a github issue discussing something similar and Jeremy's final suggestion is, indeed, to override the route: https://github.com/jeremyevans/rodauth/issues/50

So: let's say I create my own form and my own "post" route: can I get access to Rodauth's context to do my thing? For instance, I would like to use "password_meets_requirements?"; can I get it from my own route?

I hope I managed to explain my question clearly (English is not my first language)...

Any help is appreciated! And thanks Jeremy for all the amazing work you do. I already love Roda and Sequel, and now I'm getting to know Rodauth too.

Regards,

Jeremy Evans

unread,
Aug 4, 2021, 10:41:17 AM8/4/21
to rod...@googlegroups.com
On Wed, Aug 4, 2021 at 7:31 AM csk <ckosh...@gmail.com> wrote:
I'm playing with Rodauth, trying to get comfortable with it, and I'm wondering if there's some guidance for overriding entire routes.

There is: http://rodauth.jeremyevans.net/rdoc/files/README_rdoc.html#label-Overriding+Route-Level+Behavior  .  This uses an example of overriding the login action, but the same approach works for create-account.
 
So: let's say I create my own form and my own "post" route: can I get access to Rodauth's context to do my thing? For instance, I would like to use "password_meets_requirements?"; can I get it from my own route?

Yes. An approach like this should work:

route do |r|
  r.post 'create-account' do
    password = r.params['password']
    if rodauth.password_meets_requirements?(password)
      # ...
    else
      # ...
    end
  end

  r.rodauth
end

Be advised that not all Rodauth methods will work like that.  Some depend on being called at a certain point during Rodauth's processing.

Any help is appreciated! And thanks Jeremy for all the amazing work you do. I already love Roda and Sequel, and now I'm getting to know Rodauth too.

That is great to hear!

Thanks,
Jeremy

csk

unread,
Aug 5, 2021, 8:58:38 AM8/5/21
to Rodauth
Ah, nice! For some reason I overlooked that available "rodauth" instance. Calling "instance_exec" on it allowed me to rewrite things easily.

Thanks, Jeremy!
Reply all
Reply to author
Forward
0 new messages