jam-cloud/app/controllers/sessions_controller.rb

103 lines
3.7 KiB
Ruby
Raw Normal View History

2012-09-03 22:03:16 +00:00
# this is not a jam session - this is an 'auth session'
class SessionsController < ApplicationController
def new
@login_error = false
2013-06-02 19:27:46 +00:00
render :layout => "landing"
end
def create
2012-11-14 05:57:10 +00:00
user = User.authenticate(params[:session][:email], params[:session][:password])
if user.nil?
@login_error = true
render 'new', :layout => "landing"
2012-11-14 05:57:10 +00:00
else
@session_only_cookie = !jkclient_agent? && !params[:user].nil? && 0 == params[:user][:remember_me].to_i
2012-11-15 08:47:19 +00:00
complete_sign_in user
end
end
2012-11-15 09:30:30 +00:00
# OAuth docs
# http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/
2012-11-12 20:12:32 +00:00
def create_oauth
auth_hash = request.env['omniauth.auth']
2012-11-15 06:18:37 +00:00
authorization = UserAuthorization.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
if authorization
# Sign in for a user who has already registered.
complete_sign_in authorization.user
else
# Sign up for a completely new user.
# First/last name: auth_hash["info"]["first_name"] and auth_hash["info"]["last_name"]
# token: auth_hash["credentials"]["token"] -- "expires_at"
#
# For debugging - to see what all is there:
# render :text => auth_hash.to_yaml
2012-11-15 08:47:19 +00:00
#FbGraph.debug!
#app = FbGraph::Application.new '468555793186398', :secret => '546a5b253972f3e2e8b36d9a3dd5a06e'
token = auth_hash[:credentials][:token]
# FIXME:
# This should probably be in a transaction somehow, meaning the user
# create and the authorization create. Concern is UserManager.new.signup sends
# an email and whatnot.
#
# Also, should we grab their photo from facebook?
user = UserManager.new.signup(request.remote_ip,
auth_hash[:info][:first_name],
2012-11-15 08:47:19 +00:00
auth_hash[:info][:last_name],
auth_hash[:info][:email],
nil,
nil,
nil, # instruments
nil, # photo_url
2012-11-15 08:47:19 +00:00
nil)
2012-11-20 09:48:48 +00:00
# Users who sign up using oauth are presumed to have valid email adddresses.
user.confirm_email!
2012-11-22 07:52:13 +00:00
auth = user.user_authorizations.build :provider => auth_hash[:provider],
:uid => auth_hash[:uid],
:token => auth_hash[:credentials][:token],
:token_expiration => Time.at(auth_hash[:credentials][:expires_at])
2012-11-15 06:18:37 +00:00
user.save
complete_sign_in user
end
end
def complete_sign_in(user)
sign_in user
2013-07-01 01:06:22 +00:00
if !params[:sso].nil? && params[:sso] == "desk"
2013-07-04 21:11:20 +00:00
# generate multipass token and sign it
multipass = DeskMultipass.new(user)
callback_url = SampleApp::Application.config.multipass_callback_url
2013-07-04 21:21:35 +00:00
redirect_to "#{callback_url}?multipass=#{multipass.token}&signature=#{multipass.signature}"
2013-07-01 01:06:22 +00:00
else
redirect_back_or client_url
end
2012-11-12 20:12:32 +00:00
end
def destroy
# before signing out, clean up connection and music session (if necessary)
conn_mgr = ConnectionManager.new
conn_mgr.delete_connection(cookies[:client_id])
2013-07-04 21:11:20 +00:00
cookies.delete(:client_id)
sign_out
redirect_to client_url
end
2012-11-12 20:12:32 +00:00
def failure
end
2013-02-28 18:44:33 +00:00
def connection_state
if (defined?(TEST_CONNECT_STATES) && TEST_CONNECT_STATES) || 'development'==Rails.env
@prefix = defined?(TEST_CONNECT_STATE_JS_LOG_PREFIX) ? TEST_CONNECT_STATE_JS_LOG_PREFIX : '*** '
render('connection_state', :layout => 'client') && return
end
render :nothing => true, :status => 404
2013-02-28 18:44:33 +00:00
end
2012-11-12 20:12:32 +00:00
end