Created
November 17, 2011 14:15
-
-
Save croaky/1373230 to your computer and use it in GitHub Desktop.
Twitter authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
helper_method :current_user, :signed_in?, :signed_out? | |
hide_action :current_user, :current_user=, | |
:signed_in?, :signed_out?, | |
:sign_in, :sign_out, | |
:authorize, :deny_access | |
def current_user | |
@_current_user ||= user_from_cookie | |
end | |
def current_user=(user) | |
@_current_user = user | |
end | |
def signed_in? | |
! current_user.nil? | |
end | |
def signed_out? | |
current_user.nil? | |
end | |
def sign_in(user) | |
if user | |
cookies[:remember_token] = { | |
:value => user.remember_token, | |
:expires => 1.year.from_now | |
} | |
self.current_user = user | |
end | |
end | |
def sign_out | |
current_user.reset_remember_token! if current_user | |
cookies.delete(:remember_token) | |
self.current_user = nil | |
end | |
def authorize | |
deny_access unless signed_in? | |
end | |
def deny_access | |
store_location | |
if signed_in? | |
redirect_to root_path | |
else | |
redirect_to '/auth/twitter' | |
end | |
end | |
protected | |
def user_from_cookie | |
if token = cookies[:remember_token] | |
User.find_by_remember_token(token) | |
end | |
end | |
def store_location | |
if request.get? | |
session[:return_to] = request.fullpath | |
end | |
end | |
def redirect_back_or(default) | |
redirect_to(return_to || default) | |
clear_return_to | |
end | |
def return_to | |
session[:return_to] || params[:return_to] | |
end | |
def clear_return_to | |
session[:return_to] = nil | |
end | |
def redirect_to_root | |
redirect_to('/') | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "omniauth-twitter" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.config.middleware.use OmniAuth::Builder do | |
provider :developer unless Rails.env.production? | |
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET'] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateDieselClearanceUsers < ActiveRecord::Migration | |
def self.up | |
create_table(:users) do |t| | |
t.string :provider, default: "", null: false | |
t.string :uid, default: "", null: false | |
t.string :name, default: "", null: false | |
t.string :twitter, default: "", null: false | |
t.string :location, default: "", null: false | |
t.string :remember_token, limit: 128 | |
t.timestamps | |
end | |
add_index :users, :remember_token | |
add_index :users, [:provider, :uid] | |
add_index :users, :twitter | |
end | |
def self.down | |
drop_table :users | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionsController < ApplicationController | |
def create | |
sign_in User.find_or_create_by_auth(request.env["omniauth.auth"]) | |
redirect_to root_path | |
end | |
def destroy | |
sign_out | |
redirect_to root_path | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
before_create :generate_remember_token | |
def self.find_or_create_by_auth(auth) | |
find_by_provider_and_uid(auth["provider"], auth["uid"]) || | |
create do |user| | |
user.provider = auth["provider"] | |
user.uid = auth["uid"] | |
user.name = auth["info"]["name"] | |
user.twitter = auth["info"]["nickname"] | |
user.location = auth["info"]["location"] | |
end | |
end | |
protected | |
def generate_remember_token | |
self.remember_token = SecureRandom.hex(20).encode('UTF-8') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment