diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c1577c7..b008b3c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -21,6 +21,14 @@ class ApplicationController < ActionController::Base end end + def authenticate_user!(args = nil) + current_user.present? + end + + def current_user + super || User.where(soft_token: soft_token).first_or_initialize + end + private def flash_to_http_header @@ -30,4 +38,8 @@ class ApplicationController < ActionController::Base flash.discard # don't want the flash to appear when you reload page end + def soft_token + session[:user_token] ||= SecureRandom.hex(8) + end + end diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb new file mode 100644 index 0000000..a83b243 --- /dev/null +++ b/app/controllers/users/registrations_controller.rb @@ -0,0 +1,60 @@ +class Users::RegistrationsController < Devise::RegistrationsController + before_filter :configure_sign_up_params, only: [:create] +# before_filter :configure_account_update_params, only: [:update] + + # GET /resource/sign_up + # def new + # super + # end + + # POST /resource + # def create + # super + # end + + # GET /resource/edit + # def edit + # super + # end + + # PUT /resource + # def update + # super + # end + + # DELETE /resource + # def destroy + # super + # end + + # GET /resource/cancel + # Forces the session data which is usually expired after sign + # in to be expired now. This is useful if the user wants to + # cancel oauth signing in/up in the middle of the process, + # removing all OAuth session data. + # def cancel + # super + # end + + protected + + # If you have extra params to permit, append them to the sanitizer. + def configure_sign_up_params + devise_parameter_sanitizer.for(:sign_up) << :soft_token + end + + # If you have extra params to permit, append them to the sanitizer. + # def configure_account_update_params + # devise_parameter_sanitizer.for(:account_update) << :attribute + # end + + # The path used after sign up. + # def after_sign_up_path_for(resource) + # super(resource) + # end + + # The path used after sign up for inactive accounts. + # def after_inactive_sign_up_path_for(resource) + # super(resource) + # end +end diff --git a/app/models/game.rb b/app/models/game.rb index c534d1d..e45a2d7 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -65,7 +65,7 @@ class Game < ActiveRecord::Base def add_player_from_user(user) return false unless can_accomodate(user) - self.players.create(user: user) + new_player = self.players.create(user: user, soft_token: user.soft_token) end def can_accomodate(user) diff --git a/app/models/player.rb b/app/models/player.rb index 24ba76f..6b24556 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -6,6 +6,7 @@ class Player < ActiveRecord::Base has_many :player_cards has_many :plays + #TODO: soft_user will not have a user_id validates :user_id, presence: true validates :game_id, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index 2f13e49..2f865b6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,6 +6,14 @@ class User < ActiveRecord::Base self.role ||= :user end + def soft_user? + self.email.empty? + end + + def signed_in? + !soft_user? + end + # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, # :confirmable, diff --git a/app/views/games/show.json.jbuilder b/app/views/games/show.json.jbuilder index ef6d6bd..0c13d03 100644 --- a/app/views/games/show.json.jbuilder +++ b/app/views/games/show.json.jbuilder @@ -14,9 +14,9 @@ json.current_user_id current_user.try(:id) json.players @game.players do |player| json.id player.id - json.email player.user.email + json.email player.user.try(:email) json.inventory_count player.inventory.count - json.user_id player.user.id + json.user_id player.user.try(:id) if player.user == current_user json.inventory player.inventory diff --git a/app/views/layouts/_navigation_links.html.erb b/app/views/layouts/_navigation_links.html.erb index 0617046..825d30f 100644 --- a/app/views/layouts/_navigation_links.html.erb +++ b/app/views/layouts/_navigation_links.html.erb @@ -2,14 +2,14 @@