commit
3371a238b7
@ -0,0 +1,27 @@
|
||||
class SessionsController < ApplicationController
|
||||
before_filter :load_omniauth_auth, :only => :create
|
||||
|
||||
def create
|
||||
user = User.find_by_provider_and_uid(@auth["provider"], @auth["uid"]) ||
|
||||
User.create_with_omniauth(@auth)
|
||||
|
||||
self.current_user = user
|
||||
redirect_to root_url, :notice => "Signed in!"
|
||||
end
|
||||
|
||||
def destroy
|
||||
self.current_user = nil
|
||||
redirect_to root_url, :notice => "Signed out!"
|
||||
end
|
||||
|
||||
def failure
|
||||
redirect_to root_url, :alert => params[:message]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_omniauth_auth
|
||||
@auth = request.env["omniauth.auth"]
|
||||
end
|
||||
|
||||
end
|
@ -1,2 +1,9 @@
|
||||
module ApplicationHelper
|
||||
def twitter_auth_path
|
||||
"/auth/twitter"
|
||||
end
|
||||
|
||||
def github_auth_path
|
||||
"/auth/github"
|
||||
end
|
||||
end
|
||||
|
@ -0,0 +1,17 @@
|
||||
class User < ActiveRecord::Base
|
||||
|
||||
validate :provider, :presence => true
|
||||
validate :uid, :presence => true
|
||||
validate :nickname, :presence => true
|
||||
|
||||
def self.create_with_omniauth(auth)
|
||||
create! do |user|
|
||||
user.provider = auth["provider"]
|
||||
user.uid = auth["uid"]
|
||||
user.nickname = auth["info"]["nickname"]
|
||||
user.name = auth["info"]["name"]
|
||||
user.avatar_url = OauthHelper.get_avatar_url(auth)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -1,6 +1,17 @@
|
||||
defaults: &defaults
|
||||
carrierwave_storage: :fog
|
||||
oauth:
|
||||
twitter:
|
||||
consumer_key: "qAFaJs7lyIm3xjtdtFaQ"
|
||||
consumer_secret: "Ytqj4IfXmQ7gn0yWf2GgFUpFDAWfqYpVkX3P1ng0TU"
|
||||
github:
|
||||
consumer_key: "455d7cde838004d7d46a"
|
||||
consumer_secret: "5e076dbe06e79f6a7e329a4c7b306c0a0c6055c1"
|
||||
|
||||
development:
|
||||
<<: *defaults
|
||||
carrierwave_storage: :file
|
||||
|
||||
test:
|
||||
<<: *defaults
|
||||
carrierwave_storage: :file
|
||||
|
@ -0,0 +1,4 @@
|
||||
Rails.application.config.middleware.use OmniAuth::Builder do
|
||||
provider :twitter, CFG.oauth.twitter.consumer_key, CFG.oauth.twitter.consumer_secret
|
||||
provider :github, CFG.oauth.github.consumer_key, CFG.oauth.github.consumer_secret
|
||||
end
|
@ -0,0 +1,15 @@
|
||||
class CreateUsers < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :users do |t|
|
||||
t.string :provider, :null => false
|
||||
t.string :uid, :null => false
|
||||
t.string :email
|
||||
t.string :name
|
||||
t.string :avatar_url
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :users, [ :provider, :uid ], :unique => true
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
class AddNicknameColumnToUsersTable < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :users, :nickname, :string, :null => false
|
||||
end
|
||||
end
|
@ -0,0 +1,11 @@
|
||||
class OauthHelper
|
||||
|
||||
def self.get_avatar_url(auth)
|
||||
if auth["provider"] == "twitter"
|
||||
auth["info"]["image"]
|
||||
elsif auth["provider"] == "github"
|
||||
auth["extra_info"]["avatar_url"]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,87 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe SessionsController do
|
||||
|
||||
describe "#create" do
|
||||
let(:provider) { "twitter" }
|
||||
let(:uid) { 1234 }
|
||||
|
||||
before do
|
||||
OmniAuth.config.mock_auth[:twitter] = {
|
||||
"provider" => provider,
|
||||
"uid" => uid
|
||||
}
|
||||
|
||||
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
|
||||
end
|
||||
|
||||
context "user exists" do
|
||||
before do
|
||||
Factory(:user, :provider => provider, :uid => uid)
|
||||
post :create
|
||||
end
|
||||
|
||||
it "should create session" do
|
||||
session[:user_id].should_not be_nil
|
||||
@controller.current_user.should_not be_nil
|
||||
end
|
||||
|
||||
it "should redirects user to root url" do
|
||||
flash[:notice].should == "Signed in!"
|
||||
should redirect_to(root_url)
|
||||
end
|
||||
end
|
||||
|
||||
context "user doesn't exist" do
|
||||
let(:auth) { request.env["omniauth.auth"] }
|
||||
let(:user) { stub("user", :id => 1) }
|
||||
|
||||
it "should call create_with_omniauth" do
|
||||
User.should_receive(:create_with_omniauth).
|
||||
with(auth).
|
||||
and_return(user)
|
||||
|
||||
post :create
|
||||
end
|
||||
|
||||
it "should login user" do
|
||||
User.stub(:create_with_omniauth).and_return(user)
|
||||
|
||||
post :create
|
||||
|
||||
session[:user_id].should_not be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#destroy" do
|
||||
before do
|
||||
session[:user_id] = "123"
|
||||
get :destroy
|
||||
end
|
||||
|
||||
it "should destroy session" do
|
||||
session[:user_id].should be_nil
|
||||
@controller.current_user.should be_nil
|
||||
end
|
||||
|
||||
it "should redirects to root_url" do
|
||||
flash[:notice].should == "Signed out!"
|
||||
should redirect_to(root_url)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#failure" do
|
||||
let(:message) { "something went wrong" }
|
||||
|
||||
before do
|
||||
get :failure, :message => message
|
||||
end
|
||||
|
||||
it "should redirects to root_url and set error message" do
|
||||
flash[:alert].should == message
|
||||
should redirect_to(root_url)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,12 @@
|
||||
# Read about factories at http://github.com/thoughtbot/factory_girl
|
||||
|
||||
FactoryGirl.define do
|
||||
factory :user do
|
||||
provider "twitter"
|
||||
uid "1234"
|
||||
nickname "mrFoo"
|
||||
email nil
|
||||
name nil
|
||||
avatar_url nil
|
||||
end
|
||||
end
|
@ -0,0 +1,42 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OauthHelper do
|
||||
describe ".avatar_url" do
|
||||
let(:avatar_url) { "http://foo.bar/foo.png" }
|
||||
|
||||
context "when github auth" do
|
||||
let(:auth) do
|
||||
{
|
||||
"provider" => "github",
|
||||
"extra_info" => {
|
||||
"avatar_url" => avatar_url
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
it { OauthHelper.get_avatar_url(auth).should == avatar_url}
|
||||
|
||||
end
|
||||
|
||||
context "when twitter auth" do
|
||||
let(:auth) do
|
||||
{
|
||||
"provider" => "twitter",
|
||||
"info" => {
|
||||
"image" => avatar_url
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
it { OauthHelper.get_avatar_url(auth).should == avatar_url}
|
||||
end
|
||||
|
||||
context "when other provider" do
|
||||
let(:auth) do
|
||||
{ "provider" => "other" }
|
||||
end
|
||||
|
||||
it { OauthHelper.get_avatar_url(auth).should be_nil }
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,50 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe User do
|
||||
|
||||
let(:user) { Factory.build(:user) }
|
||||
|
||||
it "has valid factory" do
|
||||
Factory.build(:user).should be_valid
|
||||
end
|
||||
|
||||
describe ".create_with_omniauth" do
|
||||
let(:uid) { "123" }
|
||||
let(:provider) { "twitter" }
|
||||
let(:nickname) { "foo" }
|
||||
let(:name) { "Foo Bar" }
|
||||
|
||||
let(:auth) do
|
||||
{
|
||||
"provider" => provider,
|
||||
"uid" => uid,
|
||||
"info" => {
|
||||
"name" => name,
|
||||
"nickname" => nickname }
|
||||
}
|
||||
end
|
||||
|
||||
it "creates user with valid attributes" do
|
||||
user = User.create_with_omniauth(auth)
|
||||
user.provider.should == provider
|
||||
user.uid.should == uid
|
||||
user.nickname.should == nickname
|
||||
user.name.should == name
|
||||
user.avatar_url.should be_nil
|
||||
end
|
||||
|
||||
context "when avatar available" do
|
||||
let(:avatar_url) { "http://foo.bar/avatar.jpg"}
|
||||
|
||||
before do
|
||||
OauthHelper.stub(:get_avatar_url).and_return(avatar_url)
|
||||
end
|
||||
|
||||
it "assigns avatar_url" do
|
||||
user = User.create_with_omniauth(auth)
|
||||
user.avatar_url.should == avatar_url
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue