2 years ago
#50324
Zilton
Registration with License Key using Devise
I want to make a user registration using a generated key, with only one registration per key. Using Devise. In my DB I will have a generated key that is not assigned to any user, so when a user is registering and using the key this key will be assigned to the user.
I have created the RegistrationKey model with the following migration:
class CreateRegistrationKeys < ActiveRecord::Migration[6.1]
def change
create_table :registration_keys do |t|
t.uuid :key
t.references :user, null: true, foreign_key: true
t.timestamps
end
end
end
# registration_key.rb
class RegistrationKey < ApplicationRecord
before_create :generate_key
belongs_to :user, optional: true
private
def generate_key
self.key = SecureRandom.uuid
end
end
Updated my User model:
# user.rb
class User < ApplicationRecord
has_one :registration_key
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates_presence_of :registration_key
end
Permitted the registration_key param in registration controller:
# registration_controller.rb
class RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params
...
def create
registration_key = params[:user][:registration_key]
existing_key = RegistrationKey.find_by(key: registration_key)
params[:user][:registration_key] = existing_key
super
end
...
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:registration_key])
end
end
Added registration_key input in my new registration view:
# devise/registrations/new.haml.html
%h2 Sign up!!
= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
...
= f.input :registration_key
...
I have generated a registration key in my DB, which I use to signup but I still get the error, I cant figure out what is wrong and how to fix it:
Log:
Started POST "/users" for 127.0.0.1 at 2022-01-16 14:05:28 +0200
Processing by RegistrationsController#create as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"test@test.com", "registration_key"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
"registration_key"
"7a573aef-2d60-4561-bc29-0965647449a4"
RegistrationKey Load (1.0ms) SELECT "registration_keys".* FROM "registration_keys" WHERE "registration_keys"."key" = $1 LIMIT $2 [["key", "7a573aef-2d60-4561-bc29-0965647449a4"], ["LIMIT", 1]]
↳ app/controllers/registrations_controller.rb:20:in `create'
Unpermitted parameter: :registration_key
TRANSACTION (0.9ms) BEGIN
↳ app/controllers/registrations_controller.rb:26:in `create'
User Exists? (1.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "test@test.com"], ["LIMIT", 1]]
↳ app/controllers/registrations_controller.rb:26:in `create'
TRANSACTION (0.7ms) ROLLBACK
↳ app/controllers/registrations_controller.rb:26:in `create'
Rendering layout layouts/application.html.erb
Rendering devise/registrations/new.html.haml within layouts/application
Rendered devise/shared/_links.html.erb (Duration: 0.3ms | Allocations: 114)
Rendered devise/registrations/new.html.haml within layouts/application (Duration: 16.3ms | Allocations: 10451)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 22.0ms | Allocations: 13977)
Completed 200 OK in 314ms (Views: 22.6ms | ActiveRecord: 15.8ms | Allocations: 26287)
ruby-on-rails
devise
0 Answers
Your Answer