daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This controller handles the login/logout function of the site.  
  2. class SessionController < ApplicationController
  3.   include AuthenticatedSystem
  4.  
  5.   # render new.rhtml
  6.   def new
  7.   end
  8.  
  9.   def create
  10.     if using_open_id?
  11.       open_id_authentication(params[:openid_url])
  12.     else
  13.       password_authentication(params[:email], params[:password])
  14.     end
  15.   end
  16.  
  17.   def destroy
  18.     self.current_user.forget_me if logged_in?
  19.     cookies.delete :auth_token
  20.     reset_session
  21.     flash[:notice] = "You have been logged out."
  22.     redirect_back_or_default('/')
  23.   end
  24.  
  25.   protected
  26.  
  27.   def open_id_authentication(openid_url)
  28.     authenticate_with_open_id(openid_url, :required => [:email]) do |result, identity_url, registration|
  29.       if result.successful?
  30.         @user = User.find_or_initialize_by_identity_url(identity_url)
  31.         if @user.new_record?
  32.           @user.email = registration['email']
  33.           # validate
  34.           if @user.save
  35.             self.current_user = @user
  36.             successful_login
  37.           else
  38.             flash[:notice] = "Account creation failed. Are you already registered with that email address?"
  39.           end
  40.         end
  41.       else
  42.         failed_login result.message
  43.       end
  44.     end
  45.   end
  46.  
  47.   def password_authentication(email, password)
  48.     self.current_user = User.authenticate(email, password)
  49.     if logged_in?
  50.       successful_login
  51.     else
  52.       failed_login
  53.     end
  54.   end
  55.  
  56.   def failed_login(message = "Authentication failed.")
  57.     flash.now[:error] = message
  58.     render :action => 'new'
  59.   end
  60.  
  61.   def successful_login
  62.     if params[:remember_me] == "1"
  63.       self.current_user.remember_me
  64.       cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
  65.     end
  66.     redirect_back_or_default('/')
  67.     flash[:notice] = "Logged in successfully"
  68.   end
  69. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top