Permalink
Browse files

devise+omniauth

  • Loading branch information...
twata701 committed Feb 17, 2014
1 parent d972834 commit 8c8503452426cafb9ad3dbb7d0a28bc32e290df4
View
@@ -13,6 +13,8 @@ capybara-*.html
rerun.txt
pickle-email-*.html
config/initializers/secret_token.rb
config/initializers/devise.rb
config/settings.yml
config/secrets.yml
## Environment normalisation:
View
@@ -48,3 +48,8 @@ end
# Pagenation
gem 'kaminari'
# User Authentication
gem 'devise'
gem 'omniauth-twitter'
View
@@ -27,6 +27,7 @@ GEM
tzinfo (~> 0.3.37)
arel (4.0.2)
atomic (1.1.14)
bcrypt-ruby (3.1.2)
builder (3.1.4)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
@@ -36,8 +37,15 @@ GEM
execjs
coffee-script-source (1.7.0)
commonjs (0.2.7)
devise (3.2.2)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
thread_safe (~> 0.1)
warden (~> 1.2.3)
erubis (2.7.0)
execjs (2.0.2)
hashie (2.0.5)
hike (1.2.3)
i18n (0.6.9)
jbuilder (1.5.3)
@@ -62,6 +70,17 @@ GEM
mime-types (1.25.1)
minitest (4.7.5)
multi_json (1.8.4)
oauth (0.4.7)
omniauth (1.2.1)
hashie (>= 1.2, < 3)
rack (~> 1.0)
omniauth-oauth (1.0.1)
oauth
omniauth (~> 1.0)
omniauth-twitter (1.0.1)
multi_json (~> 1.3)
omniauth-oauth (~> 1.0)
orm_adapter (0.5.0)
polyglot (0.3.3)
rack (1.5.2)
rack-test (0.6.2)
@@ -122,16 +141,20 @@ GEM
uglifier (2.4.0)
execjs (>= 0.3.0)
json (>= 1.8.0)
warden (1.2.3)
rack (>= 1.0)
PLATFORMS
ruby
DEPENDENCIES
coffee-rails (~> 4.0.0)
devise
jbuilder (~> 1.2)
jquery-rails
kaminari
less-rails
omniauth-twitter
rails (= 4.0.0)
sass-rails (~> 4.0.0)
sdoc
@@ -0,0 +1,14 @@
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def twitter
# You need to implement the method below in your model
@user = User.find_for_twitter_oauth(request.env["omniauth.auth"])
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Twitter"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.twitter_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
View
@@ -0,0 +1,17 @@
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :trackable, :omniauthable
def self.find_for_twitter_oauth(auth)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
unless user
user = User.create(name:auth.info.nickname,
provider:auth.provider,
uid:auth.uid,
password:Devise.friendly_token[0,20]
)
end
user
end
end
@@ -52,6 +52,11 @@
<li><%= link_to "Link1", "/path1" %></li>
<li><%= link_to "Link2", "/path2" %></li>
<li><%= link_to "Link3", "/path3" %></li>
<% if user_signed_in? %>
<li><%= link_to "ログアウト", (destroy_user_session_path) %></li>
<% else %>
<li><%= link_to "ログイン", (user_omniauth_authorize_path :twitter) %></li>
<% end %>
</ul>
</div><!--/.nav-collapse -->
</div>
@@ -0,0 +1,59 @@
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
en:
devise:
confirmations:
confirmed: "Your account was successfully confirmed."
send_instructions: "You will receive an email with instructions about how to confirm your account in a few minutes."
send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes."
failure:
already_authenticated: "You are already signed in."
inactive: "Your account is not activated yet."
invalid: "Invalid email or password."
locked: "Your account is locked."
last_attempt: "You have one more attempt before your account will be locked."
not_found_in_database: "Invalid email or password."
timeout: "Your session expired. Please sign in again to continue."
unauthenticated: "You need to sign in or sign up before continuing."
unconfirmed: "You have to confirm your account before continuing."
mailer:
confirmation_instructions:
subject: "Confirmation instructions"
reset_password_instructions:
subject: "Reset password instructions"
unlock_instructions:
subject: "Unlock Instructions"
omniauth_callbacks:
failure: "Could not authenticate you from %{kind} because \"%{reason}\"."
success: "Successfully authenticated from %{kind} account."
passwords:
no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided."
send_instructions: "You will receive an email with instructions about how to reset your password in a few minutes."
send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
updated: "Your password was changed successfully. You are now signed in."
updated_not_active: "Your password was changed successfully."
registrations:
destroyed: "Bye! Your account was successfully cancelled. We hope to see you again soon."
signed_up: "Welcome! You have signed up successfully."
signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated."
signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked."
signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please open the link to activate your account."
update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and click on the confirm link to finalize confirming your new email address."
updated: "You updated your account successfully."
sessions:
signed_in: "Signed in successfully."
signed_out: "Signed out successfully."
unlocks:
send_instructions: "You will receive an email with instructions about how to unlock your account in a few minutes."
send_paranoid_instructions: "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
unlocked: "Your account has been unlocked successfully. Please sign in to continue."
errors:
messages:
already_confirmed: "was already confirmed, please try signing in"
confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one"
expired: "has expired, please request a new one"
not_found: "not found"
not_locked: "was not locked"
not_saved:
one: "1 error prohibited this %{resource} from being saved:"
other: "%{count} errors prohibited this %{resource} from being saved:"
View
@@ -5,6 +5,14 @@
post 'import_csv'
end
end
devise_for :users, :controllers => { :omniauth_callbacks => 'omniauth_callbacks' }
root :to => 'tweets#index'
devise_scope :user do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
@@ -0,0 +1,23 @@
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Trackable
t.integer :sign_in_count, :default => 0, :null => false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Omniauthable
t.integer :uid, :limit => 8 #bigintにする
t.string :name
t.string :provider
t.string :password
t.timestamps
end
add_index :users, :uid, :unique => true
end
end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140215151545) do
ActiveRecord::Schema.define(version: 20140217151640) do
create_table "tweets", force: true do |t|
t.string "tweet_id"
@@ -28,4 +28,20 @@
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.integer "uid", limit: 8
t.string "name"
t.string "provider"
t.string "password"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["uid"], name: "index_users_on_uid", unique: true
end
View
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
View
@@ -0,0 +1,7 @@
require 'test_helper'
class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 8c85034

Please sign in to comment.