Questions about rodauth and sequel

86 views
Skip to first unread message

kirqe

unread,
Aug 30, 2021, 2:01:26 PM8/30/21
to Rodauth
Hi,
I have a few questions regarding rodauth and sequel:

1. 
How do you get the current_user when using jwt in rodauth
Is there a current_user helper or something similar?

should I just do the following:

Account.find(id: rodauth.account_from_session[:id])

2. 
When saving a new record in sequel and there’re validators in model, it raises an error instead of returning a validation message.
eg:

Sequel::Model.plugin :validation_helpers

def validate
    super
    validates_min_length 5, :body
end

it’s not possible to do:

if post.save
    {success: “”}.to_json
else
    {error: post.errors...}.to_json
end

but something like this will work

post.safe
    {success: “”}.to_json
rescue
    {error: post.errors…}.to_json

How should such things be done?

3.
After setting custom routes for rodauth I had complains that login didn’t match and then there’s no password confirmation messages.
(Not sure why it asked for login confirmation). I added the last 2 lines from the code below and it solved the problem. 
Interesting, it returned validation messages to the console(without raising an error)
  Is this the expected behaviour?

prefix “auth”
login_route “signin”

require_login_confirmation? false 
require_password_confirmation? false


4.  Is it possible to replace {login: “”, password: “”} to {email: “”, password: “”} when making a post request to signin/signup route?


Thank you!

Jeremy Evans

unread,
Aug 30, 2021, 2:23:27 PM8/30/21
to rod...@googlegroups.com
On Mon, Aug 30, 2021 at 11:01 AM kirqe <belet...@gmail.com> wrote:
Hi,
I have a few questions regarding rodauth and sequel:

1. 
How do you get the current_user when using jwt in rodauth
Is there a current_user helper or something similar?

should I just do the following:

Account.find(id: rodauth.account_from_session[:id])

Rodauth doesn't have a current_user concept, in the sense that it returns a model object (since Rodauth doesn't use models).  You can call session_value to get the id of the account for the currently logged in user.  So if you were using Account.find to look an account up based on that id:

  Account.find(id: rodauth.session_value)
 
2. 
When saving a new record in sequel and there’re validators in model, it raises an error instead of returning a validation message.
eg:

Sequel::Model.plugin :validation_helpers

def validate
    super
    validates_min_length 5, :body
end

it’s not possible to do:

if post.save
    {success: “”}.to_json
else
    {error: post.errors...}.to_json
end

but something like this will work

post.safe
    {success: “”}.to_json
rescue
    {error: post.errors…}.to_json

How should such things be done?

You can set post.raise_on_save_failure = false before saving if you want a failed save to return nil/false instead of raising an exception (you can also use the raise_on_save_failure class method to set a default for the class, or the :raise_on_failure option when calling save).  Sequel defaults to raising an exception for save failures so you don't have a silent failure that you miss.
 
3.
After setting custom routes for rodauth I had complains that login didn’t match and then there’s no password confirmation messages.
(Not sure why it asked for login confirmation). I added the last 2 lines from the code below and it solved the problem. 
Interesting, it returned validation messages to the console(without raising an error)
  Is this the expected behaviour?

prefix “auth”
login_route “signin”

require_login_confirmation? false 
require_password_confirmation? false

Rodauth defaults to requiring login and password confirmation, so if you don't want to require confirmation, you should turn it off, as you did.  Rodauth route handling doesn't raise exceptions for invalid requests, they return an HTTP response with an appropriate error code.  This is expected behavior.
 
4.  Is it possible to replace {login: “”, password: “”} to {email: “”, password: “”} when making a post request to signin/signup route?

You would use the login_param configuration method for that:

  login_param 'email'

Thanks,
Jeremy

kirqe

unread,
Aug 30, 2021, 2:30:50 PM8/30/21
to Rodauth
Awesome :) Thank you!
Reply all
Reply to author
Forward
0 new messages