zefer/devise-twitter-mongoid

Forked from MSch/devise-twitter

Easy twitter integration for devise

License: MIT

Language: Ruby


devise-twitter-mongoid

A modified devise-twitter for Mongoid.

Devise-twitter adds Sign in via Twitter and Connect your account to Twitter functionality to your devise app.

It requires at least Devise 1.1 and ONLY works with Rails 3.

Current status

Devise-twitter currently supports Sign in via Twitter and Connect your account to Twitter, but no proper API for Connect your account to Twitter exists so far.

This plugin is in use in an upcoming product and continues to be improved.

Installation

Simply add devise-twitter to your Gemfile and bundle it up:

gem 'devise-twitter-mongoid', :git => 'git://github.com/zefer/devise-twitter-mongoid.git'

Run the generator, supplying the name of the model (e.g. User)

$ rails generate devise:twitter user

Add your OAuth credentials to config/initializers/devise_twitter.rb

Devise::Twitter::Mongoid.setup do |config|
  config.consumer_key = <YOUR CONSUMER KEY HERE>
  config.consumer_secret = <YOUR CONSUMER SECRET HERE>
  config.scope = :user
end

Modify your user model like so

class User
  # To use devise-twitter-mongoid don't forget to include the :twitter_oauth module:
  # e.g. devise :database_authenticatable, ... , :twitterable_oauth

  # IMPORTANT: If you want to support sign in via twitter you MUST remove the
  #            :validatable module, otherwise the user will never be saved
  #            since it's email and password is blank.
  #            :validatable checks only email and password so it's safe to remove

  include Mongoid::Document

  field :twitter_handle
  field :twitter_oauth_token
  field :twitter_oauth_secret

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :twitterable_oauth

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

end

Modify the generated routes (in config/routes.rb) to your liking

Application.routes.draw do
  devise_for :user do
    match '/user/sign_in/twitter' => Devise::Twitter::Mongoid::Rack::Signin
    match '/user/connect/twitter' => Devise::Twitter::Mongoid::Rack::Connect
  end
  ...

Signing in via Twitter

When signing in via Twitter, after authorizing access on www.twitter.com, devise-twitter will sign in an existing user or create a new one, if no user with these oauth credentials exists.

Connect your account to Twitter

Devise-twitter supports adding Twitter credentials to an existing user account (e.g. one that registered via email/password) but currently the API to expose this feature is far from perfect:

After navigating to /user/connect/twitter and authorizing access on www.twitter.com, devise-twitter checks if there is another user with the same twitter handle. If not devise-twitter adds twitter handle and oauth credentials to the current user and saves.

If another user with the same twitter handle is found devise-twitter sets the session variable warden.user.twitter.connected_user.key to the id of this user. Your application can check if this variable is set and display an option to merge the two users.

if connected_user = session['warden.user.twitter.connected_user.key'].present?
  connected_user = User.find(connected_user)

  # Ask user if she/he wants to merge her/his accounts
  # (or just go ahead and merge them)
end

If you have any idea how to improve it, please message me.

Acknowledgements

Thanks to

Meta

Project Statistics

Repository Size 647 KB
Stars 1
Forks 0
Watchers 1
Open issues disabled
Dependencies 0
Contributors 3
Tags 2
Created
Last updated
Last pushed

Top Contributors See all

Martin Schürrer Joe Roberts jtoy

Recent Tags See all

v0.1.1 September 13, 2010
v0.1.0 September 13, 2010

Something wrong with this page? Make a suggestion

Login to resync this repository