If I want to build a chatroom with rails (ca开发者_开发百科nonical case) with a choice of anonymous ('pick a nickname') and authorized (u/n & pw), how would I build this with Devise?
I've successfully got Devise working in the latter case, it's the anonymous part (create & maintain a session) I'm struggling with.
There is actually a Devise Wiki page for that, only they call it Guest user:
How To: Create a guest user
use extra before_filter to setup anonymous user, for instance,
def anonymous_sign_in
return if user_signed_in?
u = User.new(:type => 'anonymous')
u.save(:validate => false)
sign_in :user, u
end
Another option is not to sign in a guest user, but have current_user return a guest user in the absence of a signed in user.
In the below if the user is not signed in then current_user
will return a guest user. So any controller which can be access without signing in do not need the authenticate_user!
before filter.
def current_user
super || guest_user
end
def guest_user
User.find(session[:guest_user_id].nil? ? session[:guest_user_id] = create_guest_user.id : session[:guest_user_id])
end
def create_guest_user
token = SecureRandom.base64(15)
user = User.new(:first_name => "anonymous", :last_name => 'user', :password => token, :email => "#{token@example.com}")
user.save(:validate => false)
user
end
#user.rb
# Creates an anonymous user. An anonymous user is basically an auto-generated
# +User+ account that is created for the customer behind the scenes and its
# completely transparently to the customer.
def anonymous!(nickname)
temp_token = SecureRandom.base64(15).tr('+/=', 'xyz')
usr = ::User.new(email: "#{temp_token}@example.net", password: temp_token, password_confirmation: temp_token, nickname: nickname)
usr.save!(validate: false)
usr
end
You can then delete the record when it suits.
This is how I did it in Rails 6:
class ApplicationController < ActionController::Base
before_action :anonymous_sign_in
def anonymous_sign_in
return if user_signed_in?
u = User.new(:name => 'anonymous')
u.save(:validate => false)
sign_in :user, u
end
end
From the default devise generated model, I've removed the email default and null constraint, and added the name:
class DeviseCreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :email
t.string :name
## Database authenticatable
# t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
# ...
精彩评论