User foundation setup

This commit is contained in:
2014-03-26 13:17:34 +01:00
parent 6c2427e082
commit 4e75c72097
87 changed files with 861 additions and 120 deletions
@@ -0,0 +1,52 @@
module Users
class ApplicationController < ::ApplicationController
before_action :user_authentication, :unless => ->(c){ %w(obtain_token).include?(c.action_name) || c.request.format.symbol == :html } # , except: [:obtain_token, :index]
private
def user_authentication
if params[:auth_token].present?
user = User.find_by_authentication_token(params[:auth_token])
sign_in user if user
sign_out current_user if current_user && !user # Other token attempt of logged in user
else
authenticate_user!
end
unless current_user.present?
respond_to do |format|
format.html {redirect_to new_user_session_path}
format.json {render json: json_response(ok: false, status: 401), status: :unauthorized}
end
end
end
def obtain_token
redirect_to user_omniauth_authorize_path('facebook') and return unless current_user.present?
respond_to do |format|
format.html
format.json do
render json: json_response(ok: false, status: 401) and return unless params[:user].present? && params[:user][:email].present? && params[:user][:password].present?
user = User.find_by_email(params[:user][:email])
render json: json_response(ok: false, status: 401) and return unless user
render json: json_response(ok: false, status: 401) and return unless user.valid_password?(params[:user][:password])
user.ensure_authentication_token
sign_in user
render json: json_response(ok: true, auth_token: user.authentication_token, user_id: user.id)
end
end
end
def handle_message_params
flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) if params[:list_closed].present?
flash.now[:notice] = t("messages.#{params[:message]}", list: List.model_name.human, supplier: Supplier.model_name.human) if params[:message].present? && params[:message] =~ /^\w+$/
end
# General handler of json responses. Will be able to set some additional communication data.
# By default a response is ok.
def json_response(obj = {})
obj[:ok] = true unless obj.has_key?(:ok)
obj
end
end
end