-
-
Save patrickcurl/4551281 to your computer and use it in GitHub Desktop.
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 facebook | |
redirect_to '/auth/facebook' | |
end | |
def twitter | |
redirect_to '/auth/twitter' | |
end | |
def create | |
auth = request.env['omniauth.auth'] | |
# Find an identity or create an identity | |
@user = User.find_with_omniauth(auth) | |
#@identity = Identity.find_with_omniauth(auth) | |
if @user.nil? | |
# If no identity was found, create a brand new one here | |
@user = User.link_account(auth) | |
end | |
if user_signed_in? | |
if @user == current_user | |
# User is signed in so they are trying to link an identity with their | |
# account. But we found the identity and the user associated with it | |
# is the current user. So the identity is already associated with | |
# this user. So let's display an error message. | |
redirect_to root_path, notice: "You have already linked this account" | |
else | |
# The identity is not associated with the current_user so lets | |
# associate the identity | |
@user = current_user | |
@user.save | |
redirect_to root_path, notice: "Account successfully authenticated" | |
end | |
else # no user is signed_in | |
if @user.present? | |
# The identity we found had a user associated with it so let's | |
# just log them in here | |
self.current_user = @user | |
redirect_to root_path, notice: "Signed in!" | |
else | |
# The identity has no user assigned and there is no user signed in | |
# Our decision here is to create a new account for the user | |
# But your app may do something different (eg. ask the user | |
# if he already signed up with some other service) | |
if auth['provider'] == 'identity' | |
u = User.find(uid) | |
# If the provider is identity, then it means we already created a user | |
# So we just load it up | |
else | |
# otherwise we have to create a user with the auth hash | |
u = User.create_with_omniauth(auth) | |
# NOTE: we will handle the different types of data we get back | |
# from providers at the model level in create_with_omniauth | |
end | |
# We can now link the identity with the user and log him in | |
#u.identities << @identity | |
self.current_user = u | |
redirect_to root_path, notice: "Welcome to The app!" | |
end | |
end | |
end | |
def destroy | |
#reset_session | |
self.current_user = nil | |
redirect_to root_url, :notice => 'Signed out!' | |
end | |
def failure | |
redirect_to root_url, :alert => "Auth error: #{params[:message].humanize}" | |
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
# == Schema Information | |
# Schema version: 20130116180743 | |
# | |
# Table name: users | |
# | |
# id :integer not null, primary key | |
# name :string(255) | |
# email :string(255) | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# | |
class User < ActiveRecord::Base | |
rolify | |
attr_accessible :role_ids, :as => :admin | |
attr_accessible :name, :email, :twitter_id, :facebook_id | |
# has_many :identities | |
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | |
validates :email, :presence => true, | |
:format => { :with => email_regex }, | |
:uniqueness => { :case_sensitive => false } | |
def link_account(auth) | |
if auth['provider'] == "twitter" | |
update_attributes(:twitter_id, auth['uid']) | |
end | |
if auth['provider'] == "facebook" | |
update_attributes(:facebook_id, auth['uid']) | |
end | |
end | |
def self.find_with_omniauth(auth) | |
if auth['provider'] == "twitter" | |
where(:twitter_id => auth['uid']).first | |
elsif auth['provider'] == "facebook" | |
where(:facebook_id => auth['uid']).first | |
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 UsersController < ApplicationController | |
before_filter :authenticate_user!, :except => [:new, :create] | |
before_filter :correct_user?, :except => [:index, :new, :create] | |
def index | |
@users = User.all | |
end | |
def edit | |
@user = User.find(params[:id]) | |
end | |
def update | |
@user = User.find(params[:id]) | |
if @user.update_attributes(params[:user]) | |
redirect_to @user | |
else | |
render :edit | |
end | |
end | |
def new | |
@user = env['omniauth.identity'] ||= User.new | |
respond_to do |format| | |
format.html # new.html.erb | |
format.json { render json: @user } | |
end | |
end | |
def create | |
#auth = request.env["omniauth.auth"] | |
@user = User.create_with_omniauth(:params) | |
respond_to do |format| | |
if @user.save | |
# current_user = @user | |
# redirect_to signin_url notice: "test" | |
format.html { redirect_to signin_url, notice: 'User was successfully created.' } | |
format.json { render json: @user, status: :created, location: @user } | |
else | |
format.html { render "new" } | |
format.json { render json: @user.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def show | |
@user = User.find(params[:id]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment