Skip to content

Instantly share code, notes, and snippets.

@joeljackson
joeljackson / omniauth.rb
Created March 27, 2013 16:24
Omniauth Project Example
class SessionsController < ApplicationController
def create
@user = User.find_or_create_from_auth_hash(auth_hash)
self.current_user = @user
redirect_to '/'
end
protected
def auth_hash
@joeljackson
joeljackson / ominauth.rb
Created March 27, 2013 19:03
Omniauth, More Complicated
def create
omniauth = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
flash[:notice] = "Signed in successfully."
sign_in_and_redirect(:user, authentication.user)
elsif current_user
current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
flash[:notice] = "Authentication successful."
redirect_to authentications_url
@joeljackson
joeljackson / omniauth.rb
Created March 27, 2013 19:09
Bad Omniauth
def create
if signing_in
if service_1
elsif service_2
elsif ..
end
elsif associating_an_account
if service_1
elsif service_2
elsif ..
@joeljackson
joeljackson / sessions_controller.rb
Created March 27, 2013 19:16
Optimal Session New
def create
user = find_user || create_user
associate_account_if_necessary
login(user)
redirect_to_appropriate_location
end
@joeljackson
joeljackson / user_sessions_controller.rb
Created March 27, 2013 19:24
Completed Sessions#create method
def create
runner = StrategyRunner.new
begin
user = runner.run(:Authentication, request) || runner.run(:UserCreation, request)
auto_login(user)
runner.run(:ExternalServiceAssociation, request, user) if user
rescue ExternalServiceAssociationStrategyFinderError => e
#Swallow this. Sometimes there is no external service to associate with
rescue PasswordAuthenticationError => e
flash[:login_alert] = 'bad_username'
@joeljackson
joeljackson / strategy_runner.rb
Created March 27, 2013 19:27
Strategy Runner
class StrategyRunner
def run(strategy_name, *params)
finder_klass = Kernel.const_get(strategy_name.to_s + "StrategyFinder")
finder = finder_klass.new(*params)
strategy_klass = finder.strategy
result = if strategy_klass.nil?
nil
else
strategy = strategy_klass.new()
strategy.run(*finder.params)
@joeljackson
joeljackson / authentication_strategy_finder.rb
Created March 27, 2013 19:35
Authentication Strategy Finder
class AuthenticationStrategyFinder
def initialize(request)
@request = request
end
def strategy
@authentication_class ||=
if @request.session[:user_id].present?
SessionAuthenticationStrategy
elsif @request.params[:user_session].present?
@joeljackson
joeljackson / password_authentication_strategy.rb
Created March 27, 2013 19:41
An example of a strategy class
class PasswordAuthenticationStrategy < AuthenticationStrategy
def run(username, password)
user = DbUser.find_by_username(username)
return user if user.present? && password.xfire_bcrypt == user.password
#If the user tried to auth via password and failed raise an exception
raise PasswordAuthenticationError.new("Incorrect Username or Password")
end
end
@joeljackson
joeljackson / video.html
Created April 4, 2013 20:52
Video Embed
<div class="media">
<div id="mediaspace_wrapper" style="position: relative; width: 500px; height: 298px;"><object type="application/x-shockwave-flash" data="/jwplayer/jwplayer.flash.swf" width="100%" height="100%" bgcolor="#000000" id="mediaspace" name="mediaspace" tabindex="0"><param name="allowfullscreen" value="true"><param name="allowscriptaccess" value="always"><param name="seamlesstabbing" value="true"><param name="wmode" value="opaque"></object><div id="mediaspace_jwpsrv" style="position: absolute; top: 0px; z-index: 10;"></div></div>
<script type="text/javascript">
jwplayer.key="L9eJslnS4SIMCy1u1VeSZLNSyUJNi3shW1f35A=="
jwplayer('mediaspace').setup({
autostart: true,
file: 'http://video.xfire.com/5ede1f.mp4',
@joeljackson
joeljackson / videos.html.erb
Created April 4, 2013 20:54
Source for jwplayer videos page.
<div id='mediaspace'></div>
<script type='text/javascript'>
jwplayer.key="L9eJslnS4SIMCy1u1VeSZLNSyUJNi3shW1f35A=="
jwplayer('mediaspace').setup({
autostart: true,
file: '<%= url %>',
width: 500,
height: 298,
primary: 'flash',