Skip to content

Commit 164ed2a

Browse files
authored
Configures Devise to Use UID Instead of Email (#1198)
* Configure devise to lookup by uid * Add key for guest users
1 parent 71076fd commit 164ed2a

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

app/controllers/application_controller.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ def landing
2121
def help_guide
2222
render "/help_guide"
2323
end
24+
25+
private
26+
27+
# Needed for guest user authentication. Devise expects the authentication key to be present, but we want to allow it to be nil for non-guest users.
28+
# This method ensures that only keys starting with "guest" are considered valid, and generates a unique guest UID if the key is nil or invalid.
29+
# rubocop:disable Lint/UselessAssignment
30+
def guest_uid_authentication_key(key)
31+
key &&= nil unless /^guest/.match?(key.to_s)
32+
key ||= "guest_" + Array.new(5) { SecureRandom.rand(0..9) }.join
33+
end
34+
# rubocop:enable Lint/UselessAssignment
2435
end

config/initializers/devise.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
# session. If you need permissions, you should implement that in a before filter.
4141
# You can also supply a hash where the value is a boolean determining whether
4242
# or not authentication should be aborted when the value is not present.
43-
# config.authentication_keys = [:email]
43+
config.authentication_keys = [:uid]
4444
# require "omniauth-cas"
4545

4646
config.omniauth :openid_connect, {
@@ -72,12 +72,12 @@
7272
# Configure which authentication keys should be case-insensitive.
7373
# These keys will be downcased upon creating or modifying a user and when used
7474
# to authenticate or find a user. Default is :email.
75-
config.case_insensitive_keys = [:email]
75+
config.case_insensitive_keys = [:uid]
7676

7777
# Configure which authentication keys should have whitespace stripped.
7878
# These keys will have whitespace before and after removed upon creating or
7979
# modifying a user and when used to authenticate or find a user. Default is :email.
80-
config.strip_whitespace_keys = [:email]
80+
config.strip_whitespace_keys = [:uid]
8181

8282
# Tell if authentication through request.params is enabled. True by default.
8383
# It can be set to an array that will enable params authentication only for the
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddIndexToUsersUid < ActiveRecord::Migration[7.0]
2+
def change
3+
add_index :users, :uid, unique: true, if_not_exists: true
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.0].define(version: 2024_01_15_173008) do
13+
ActiveRecord::Schema[7.0].define(version: 2026_02_27_200115) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "plpgsql"
1616

@@ -51,6 +51,7 @@
5151
t.index ["netid"], name: "index_users_on_netid", unique: true
5252
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
5353
t.index ["sub"], name: "index_users_on_sub", unique: true
54+
t.index ["uid"], name: "index_users_on_uid", unique: true
5455
end
5556

5657
end

0 commit comments

Comments
 (0)