Permalink
Browse files

Add devise and omniauth

  • Loading branch information...
1 parent d91fa19 commit f8bae950eb895c2e60776766e79c920420cd51bd @amesel amesel committed May 27, 2012
Showing with 749 additions and 178 deletions.
  1. +3 −2 Gemfile
  2. +28 −11 Gemfile.lock
  3. +0 −3 app/assets/javascripts/joins.js.coffee
  4. 0 app/assets/javascripts/members.js.coffee
  5. +1 −0 app/assets/javascripts/parties.js.coffee
  6. +0 −3 app/assets/javascripts/sessions.js.coffee
  7. +2 −2 app/controllers/activities_controller.rb
  8. +0 −16 app/controllers/application_controller.rb
  9. +1 −1 app/controllers/joins_controller.rb
  10. +41 −0 app/controllers/omniauth_callbacks_controller.rb
  11. +1 −1 app/controllers/parties_controller.rb
  12. +0 −13 app/controllers/sessions_controller.rb
  13. +16 −0 app/controllers/settings_controller.rb
  14. +1 −1 app/controllers/tasks_controller.rb
  15. +1 −74 app/controllers/users_controller.rb
  16. +2 −0 app/helpers/settings_helper.rb
  17. +5 −0 app/models/authentication.rb
  18. +5 −11 app/models/user.rb
  19. +12 −0 app/views/devise/confirmations/new.html.erb
  20. +5 −0 app/views/devise/mailer/confirmation_instructions.html.erb
  21. +8 −0 app/views/devise/mailer/reset_password_instructions.html.erb
  22. +7 −0 app/views/devise/mailer/unlock_instructions.html.erb
  23. +16 −0 app/views/devise/passwords/edit.html.erb
  24. +12 −0 app/views/devise/passwords/new.html.erb
  25. +25 −0 app/views/devise/registrations/edit.html.erb
  26. +21 −0 app/views/devise/registrations/new.html.erb
  27. +17 −0 app/views/devise/sessions/new.html.erb
  28. +25 −0 app/views/devise/shared/_links.erb
  29. +12 −0 app/views/devise/unlocks/new.html.erb
  30. +14 −12 app/views/layouts/application.html.haml
  31. +1 −1 app/views/parties/_form.html.haml
  32. +32 −0 app/views/settings/_form.html.haml
  33. +3 −0 app/views/settings/edit.html.haml
  34. +2 −1 app/views/shared/_global_nav.html.haml
  35. +1 −1 app/views/tasks/_task.html.haml
  36. +3 −0 config/environments/development.rb
  37. +212 −0 config/initializers/devise.rb
  38. +0 −3 config/initializers/omniauth.rb
  39. +58 −0 config/locales/devise.en.yml
  40. +65 −0 config/locales/devise.ja.yml
  41. +6 −7 config/routes.rb
  42. +7 −2 db/migrate/20120429020535_create_users.rb
  43. +18 −0 db/migrate/20120527040434_create_authentications.rb
  44. +28 −5 db/schema.rb
  45. +0 −8 db/seeds.rb
  46. +7 −0 test/fixtures/authentications.yml
  47. +14 −0 test/functional/settings_controller_test.rb
  48. +7 −0 test/unit/authentication_test.rb
  49. +4 −0 test/unit/helpers/settings_helper_test.rb
View
@@ -22,10 +22,11 @@ end
gem 'jquery-rails'
-gem 'omniauth-twitter'
-
gem 'best_in_place'
+gem 'devise'
+gem 'oa-oauth', :require => "omniauth/oauth"
+
group :development do
gem 'erb2haml'
end
View
@@ -28,7 +28,9 @@ GEM
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
+ addressable (2.2.8)
arel (3.0.2)
+ bcrypt-ruby (3.0.1)
best_in_place (1.0.6)
jquery-rails
rails (~> 3.1)
@@ -47,16 +49,23 @@ GEM
sass (~> 3.1)
compass-rails (1.0.1)
compass (~> 0.12.0)
+ devise (1.4.5)
+ bcrypt-ruby (~> 3.0)
+ orm_adapter (~> 0.0.3)
+ warden (~> 1.0.3)
erb2haml (0.1.2)
haml
hpricot
ruby_parser
erubis (2.7.0)
execjs (1.3.1)
multi_json (~> 1.0)
+ faraday (0.6.1)
+ addressable (~> 2.2.4)
+ multipart-post (~> 1.1.0)
+ rack (>= 1.1.0, < 2)
fssm (0.2.9)
haml (3.1.4)
- hashie (1.2.0)
hike (1.2.1)
hpricot (0.8.6)
i18n (0.6.0)
@@ -72,16 +81,21 @@ GEM
treetop (~> 1.4.8)
mime-types (1.18)
multi_json (1.3.4)
+ multipart-post (1.1.5)
+ nokogiri (1.4.7)
+ oa-core (0.2.4)
+ oa-oauth (0.2.4)
+ faraday (~> 0.6.1)
+ multi_json (>= 0.0.5)
+ nokogiri (~> 1.4.2)
+ oa-core (= 0.2.4)
+ oauth (~> 0.4.0)
+ oauth2 (~> 0.4.1)
oauth (0.4.6)
- omniauth (1.1.0)
- hashie (~> 1.2)
- rack
- omniauth-oauth (1.0.1)
- oauth
- omniauth (~> 1.0)
- omniauth-twitter (0.0.11)
- multi_json (~> 1.3)
- omniauth-oauth (~> 1.0)
+ oauth2 (0.4.1)
+ faraday (~> 0.6.1)
+ multi_json (>= 0.0.5)
+ orm_adapter (0.0.5)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
@@ -132,6 +146,8 @@ GEM
uglifier (1.2.4)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
+ warden (1.0.5)
+ rack (>= 1.0)
PLATFORMS
ruby
@@ -140,9 +156,10 @@ DEPENDENCIES
best_in_place
coffee-rails (~> 3.2.1)
compass-rails
+ devise
erb2haml
jquery-rails
- omniauth-twitter
+ oa-oauth
rails (= 3.2.3)
sass-rails (~> 3.2.3)
sqlite3
@@ -1,3 +0,0 @@
-# Place all the behaviors and hooks related to the matching controller here.
-# All this logic will automatically be available in application.js.
-# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
No changes.
@@ -2,6 +2,7 @@ $ ->
$('#party_member_tokens').tokenInput '/users.json',
hintText: 'メンバーを選択してください'
theme: 'facebook'
+ prePopulate: $('#party_member_tokens').data('load')
$('.best_in_place').best_in_place()
$("#global").css "height", $(window).height()
@@ -1,3 +0,0 @@
-# Place all the behaviors and hooks related to the matching controller here.
-# All this logic will automatically be available in application.js.
-# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -1,7 +1,7 @@
class ActivitiesController < ApplicationController
- before_filter :authenticate_user!, :except => [:index, :show]
+ before_filter :authenticate_user!
respond_to :html, :json, :js
-
+
# GET /activities
# GET /activities.json
def index
@@ -1,19 +1,3 @@
class ApplicationController < ActionController::Base
protect_from_forgery
-
- helper_method :current_user, :user_signed_in?
-
- private
-
- def current_user
- @current_user ||= User.find(session[:user_id]) if session[:user_id]
- end
-
- def user_signed_in?
- !current_user.nil?
- end
-
- def authenticate_user!
- redirect_to root_path, :notice => "Please sign in to access this page." unless user_signed_in?
- end
end
@@ -1,5 +1,5 @@
class JoinsController < ApplicationController
- before_filter :authenticate_user!, :except => [:index, :show]
+ before_filter :authenticate_user!
respond_to :html, :json
# def create
@@ -0,0 +1,41 @@
+class OmniauthCallbacksController < Devise::OmniauthCallbacksController
+
+ def twitter
+ omniauth = request.env['omniauth.auth']
+ @authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
+ if @authentication
+ logger.info("##### have authentication")
+ #sign_in_and_redirect @user, :event => :authentication
+ sign_in(:user, @authentication.user)
+ logger.info("##### abc")
+# set_token_to_session(current_user)
+ logger.info("##### ccc")
+ redirect_to root_url
+
+ #elsif current_user # 既にログインしてるけど、facebookとかの権限も追加するとき
+ # current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'],
+ # :oauth_token => omniauth['credentials']['token'],
+ # :oauth_token_secret => omniauth['credentials']['secret'])
+ # redirect_to authentications_url
+ else # 新規ユーザのとき
+ logger.info("##### don't have authentication")
+ data = omniauth['extra']['user_hash']
+ @user = User.new
+ @user.authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'],
+ :access_token => omniauth['credentials']['token'],
+ :access_secret => omniauth['credentials']['secret'],
+ :screen_name => data['screen_name'],
+ :bio => data['description'],
+ :image_url => data['profile_image_url'],
+ :web_url => data['url'],
+ :last_tid => nil) # data['id']で取れるけど初期値はnil
+ @user.name = data['screen_name']
+ @user.image = data['profile_image_url']
+ @user.save!(validate: false)
+
+ sign_in(:user, @user)
+ redirect_to root_url
+ end
+ end
+
+end
@@ -1,5 +1,5 @@
class PartiesController < ApplicationController
- before_filter :authenticate_user!, :except => [:index, :show]
+ before_filter :authenticate_user!
respond_to :html, :json
def index
@@ -1,13 +0,0 @@
-class SessionsController < ApplicationController
- def create
- auth = request.env["omniauth.auth"]
- user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
- session[:user_id] = user.id
- redirect_to root_url, :notice => "Signed in!"
- end
-
- def destroy
- session[:user_id] = nil
- redirect_to root_url, :notice => "Signed out!"
- end
-end
@@ -0,0 +1,16 @@
+class SettingsController < ApplicationController
+ def edit
+ @user = current_user
+ end
+
+ def update
+ @user = User.find(current_user.id)
+ if @user.update_attributes(params[:user])
+ # Sign in the user bypassing validation in case his password changed
+ sign_in @user, :bypass => true
+ redirect_to root_path
+ else
+ render "edit"
+ end
+ end
+end
@@ -1,5 +1,5 @@
class TasksController < ApplicationController
- before_filter :authenticate_user!, :except => [:index, :show]
+ before_filter :authenticate_user!
respond_to :html, :json, :js
# # GET /tasks
@@ -1,83 +1,10 @@
class UsersController < ApplicationController
- # # GET /users
- # # GET /users.json
def index
@users = User.where("name like ?", "%#{params[:q]}%")
-
+
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
- #
- # # GET /users/1
- # # GET /users/1.json
- # def show
- # @user = User.find(params[:id])
- #
- # respond_to do |format|
- # format.html # show.html.erb
- # format.json { render json: @user }
- # end
- # end
- #
- # # GET /users/new
- # # GET /users/new.json
- # def new
- # @user = User.new
- #
- # respond_to do |format|
- # format.html # new.html.erb
- # format.json { render json: @user }
- # end
- # end
- #
- # # GET /users/1/edit
- # def edit
- # @user = User.find(params[:id])
- # end
- #
- # # POST /users
- # # POST /users.json
- # def create
- # @user = User.new(params[:user])
- #
- # respond_to do |format|
- # if @user.save
- # format.html { redirect_to @user, notice: 'User was successfully created.' }
- # format.json { render json: @user, status: :created, location: @user }
- # else
- # format.html { render action: "new" }
- # format.json { render json: @user.errors, status: :unprocessable_entity }
- # end
- # end
- # end
- #
- # # PUT /users/1
- # # PUT /users/1.json
- # def update
- # @user = User.find(params[:id])
- #
- # respond_to do |format|
- # if @user.update_attributes(params[:user])
- # format.html { redirect_to @user, notice: 'User was successfully updated.' }
- # format.json { head :no_content }
- # else
- # format.html { render action: "edit" }
- # format.json { render json: @user.errors, status: :unprocessable_entity }
- # end
- # end
- # end
- #
- # # DELETE /users/1
- # # DELETE /users/1.json
- # def destroy
- # @user = User.find(params[:id])
- # @user.destroy
- #
- # respond_to do |format|
- # format.html { redirect_to users_url }
- # format.json { head :no_content }
- # end
- # end
end
@@ -0,0 +1,2 @@
+module SettingsHelper
+end
@@ -0,0 +1,5 @@
+class Authentication < ActiveRecord::Base
+ attr_accessible :user_id, :provider, :uid, :screen_name, :access_token, :access_secret, :bio, :image_url, :web_url, :last_tid
+
+ belongs_to :user
+end
View
@@ -1,22 +1,16 @@
class User < ActiveRecord::Base
- attr_accessible :image, :name, :provider, :uid
+ attr_accessible :image, :name, :email, :password, :remember_me, :password_confirmation
+ has_many :authentications
has_many :parties
has_many :joins
-# has_many :invited_parties, :through => :joins
has_many :invited_parties, :through => :joins, :source => :party
has_many :tasks
has_many :activities
validates :name, :presence => true
- validates :uid, :uniqueness => {:scope => :provider}
+ validates :email, :password, :presence => true, :on => :create
+# validates :uid, :uniqueness => {:scope => :provider}
- def self.create_with_omniauth(auth)
- create! do |user|
- user.provider = auth["provider"]
- user.uid = auth["uid"]
- user.name = auth["info"]["nickname"]
- user.image = auth["info"]["image"]
- end
- end
+ devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :omniauthable
end
@@ -0,0 +1,12 @@
+<h2>Resend confirmation instructions</h2>
+
+<%= form_for(resource, :as => resource_name, :url => confirmation_path(resource_name), :html => { :method => :post }) do |f| %>
+ <%= devise_error_messages! %>
+
+ <div><%= f.label :email %><br />
+ <%= f.email_field :email %></div>
+
+ <div><%= f.submit "Resend confirmation instructions" %></div>
+<% end %>
+
+<%= render :partial => "devise/shared/links" %>
@@ -0,0 +1,5 @@
+<p>Welcome <%= @resource.email %>!</p>
+
+<p>You can confirm your account through the link below:</p>
+
+<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p>
@@ -0,0 +1,8 @@
+<p>Hello <%= @resource.email %>!</p>
+
+<p>Someone has requested a link to change your password, and you can do this through the link below.</p>
+
+<p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %></p>
+
+<p>If you didn't request this, please ignore this email.</p>
+<p>Your password won't change until you access the link above and create a new one.</p>
Oops, something went wrong.

0 comments on commit f8bae95

Please sign in to comment.