Logging in as a seeded user in an OmniAuth app

23 views
Skip to first unread message

Jason Hsu

unread,
Jan 7, 2019, 11:55:18 AM1/7/19
to Ruby on Rails: Talk
Here's an example:
I'm working on the CodeTriage app (https://github.com/codetriage/codetriage).  I have CodeTriage working and passing tests in my local development environment.

In this app, GitHub authentication is the sole authentication option. To avoid being automatically logged in in the localhost environment, I can log out of GitHub in my browser or open up the localhost site in private to get the option to log in. However, clicking on the "Log in" option then takes me to https://github.com/login, which means that I can only log in as myself and not one of the seeded users.


Given this, what's the procedure to log in as one of the seeded users?

Rob Jonson

unread,
Jan 8, 2019, 5:11:59 AM1/8/19
to Ruby on Rails: Talk
looks like that example is using devise, so you can bypass signin

(after checking authorisation) 

or see:

note - this is for practical use, not for powering tests

Jason Hsu

unread,
Jan 10, 2019, 5:15:51 PM1/10/19
to Ruby on Rails: Talk
OK, I'm trying out the bypass_sign_in feature.  I'm getting the following error:
/home/winner/.rbenv/gems/2.6.0/gems/devise-4.5.0/lib/devise/controllers/sign_in_out.rb:112:in `expire_data_after_sign_in!': undefined local variable or method `session' for main:Object (NameError)

I've put the script login.rb in the root directory of the project.  The code of login.rb is:
require File.expand_path('../config/environment', __FILE__)
require 'devise'
include
Devise::Controllers::SignInOut

users
= User.all;
num_users
= User.count;
n_first
= 0;
n_last
= num_users - 1;

puts
'---------------------------'
puts
'Logging in as a seeded user'
puts
''
puts
"Pick a user number from #{n_first} to #{n_last}:"
n_input
= gets.chomp().to_i;
@user = users[n_input];
puts
@user

bypass_sign_in
@user, scope: :user

Everything works as expected until the bypass_sign_in.  Is there another "require" or "include" statement that I need to add?

Rob Jonson

unread,
Jan 10, 2019, 5:43:59 PM1/10/19
to rubyonra...@googlegroups.com
bypass_sign_in is intended to be used from a controller where you'll have access to things like 'session'
I suggest you start by getting it working in the intended environment before you try to pull it out into a standalone script.

--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/d85MICj-BJQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/21df3d80-5f5b-4f60-a28a-0bd6d4e5a13d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--





Hobbyist Software is a trading name of Hobbyist Software Limited. Registered office 12 Fraley Rd, Bristol, BS93BS. Registered in England. Company no:7876492

Jason Hsu

unread,
Jan 10, 2019, 10:30:19 PM1/10/19
to Ruby on Rails: Talk
The test environment already has the ability to log in as a user other than myself.  I'm trying to get my localhost/development setup to do the same thing by running a Ruby script.

Rob Jonson

unread,
Jan 11, 2019, 5:45:49 AM1/11/19
to rubyonra...@googlegroups.com
sorry - I wasn't clear with my language. By environment - I meant 'just write this in a controller action'

e.g. this is commonly available from an admin controller so that administrators can log in as other users. 
Devise is assuming it has access to the controller plumbing and that things like session are meaningful.




For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages