Git Product home page Git Product logo

google_authentication's Introduction

google_authentication with devise + omniauth

Build Status Gem Version

I found myself using Google authentication in a lot of projects (especially private projects which require google apps authentication). With Omniauth it's pretty simple to authenticate against Google and Devise has a lot of convenient helpers.

But if you need to use Devise with a model which is only :omniauthable you'll have to do a lot of configuration to make it working, as you can see here. Essentially you need to define custom routes, a callbacks controller for processing Omniauth responses and a sessions controller because you can't use Devise one if your model is only :omniauthable.

I'm studying Rails Engines, so this gem is my first attempt with them. I tried to build all the needed stuff in a gem to develop an out-of-the-box solution for Google authentication based on devise + omniauth. This gem is the result.

Hello world Google

This is the minimal configuration in order to use google as authenticator for your projects

rails new google_app
gem 'google_authentication' # in your Gemfile
bundle install
rails g google_authentication:install # this will also install devise in your app
rails g google_authentication user    # this will generate a model and it will enable devise routes for it
rake db:migrate

No other steps are needed, you have a working google application. You can use all the devise helpers and routes in your app, with no further configuration.

A deeper look

To use the gem the mandatory steps are pretty standard

gem 'google_authentication' # in your Gemfile

Then (after the bundle install command) you must install devise and google_authentication initializers in the app with the command

rails g google_authentication:install # this will also install devise in your app

As far as the model defined in the :model_name config parameter doesn't exist the gem (and devise itself) is invisible to your app.

You can use the given generator to create a Google authenticable model with

rails g google_authentication user    # this will generate a model and it will enable devise routes for it
rake db:migrate

These commands will (indirectly) activate the gem routes by defining the User class. The generated model contains an acts_as_google_user call which activates devise features for that model.

At this point you can start using devise methods, helpers and filters in your app, something like this piece of code in your views (or layout)

<% if current_user %>
    <p>Currently logged in as <strong><%= current_user.email %></strong></p>
<% else %>
    <p>You're not logged in, <%= link_to 'Login', user_omniauth_authorize_path(:google_apps) %></p>
<% end %>

If you want to add Google authentication to an existing model you'll have to add an acts_as_google_user call in it and you need to generate a migration which creates required fields with

rails g migration add_google_authentication_to_user email:string omniauth_uid:string

Which creates a migration like this one

class AddGoogleAuthenticationToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :email, :string
    add_column :users, :omniauth_uid, :string
  end

  def self.down
    remove_column :users, :omniauth_uid
    remove_column :users, :email
  end
end

You may still need to edit the generated migration to add other devise fields if you use other devise modules. See this wiki page and below

Configuration and Customization

At this stage my gem has only 2 configurable params: domain and model_name. You can configure them in the initializer created by the install generator.

  • domain: it tell the gem which google domain it should use to authenticate users
  • model_name: it tell the gem which model should be authenticated with Google

Model creation and retrieval

When returning from google auth page to your app gem's callback controller is invoked and it search for an existing user with the given omniauth_uid. If he can't be found it creates it and sign in him. If the user already exist it will update its fields with omniauth returned ones.

This (pretty simple) logic is implemented in the acts_as_google_user behavior and it can be customized as you wish.

To replace the default finder just define a class method in you model named find_or_create_by_omniauth and it will be called in the callback controller to retrieve or create the users. This method will be invoked with the full omniauth hash returned by omniauth gem. With the current omniauth implementation it will contain the following values

  • provider (required) - The provider with which the user authenticated (i.e. 'google_apps')
  • uid (required) - An identifier unique to the given provider. Should be used as unique identifier for your users, stored as a string.
  • user_info (required) - A hash containing information about the user
    • name (required) - The best display name known to the strategy. Usually a concatenation of first and last name.
    • email - The e-mail of the authenticating user.
    • first_name
    • last_name

Integration with other devise modules

Models used in this gem may use other devise modules in a transparent way, just pass devise modules to the acts_as_google_user call. However I've excluded some modules because they're useless in this context, so you can not pass any of them to the call. These are the devise modules you can use in your models:

class User < ActiveRecord::Base
  # :omniauthable is obviously included by default even if you don't list it
  acts_as_google_user :omniauthable, :token_authenticable, :trackable, :timeoutable, :rememberable
end

Contributing to google_authentication

I'm using the gitflow model to maintain this gem, so all the development stuff is in the develop branch, master will contain only stable code.

  • Check out the latest develop to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2011 Fabio Napoleoni. See LICENSE.txt for further details.

google_authentication's People

Contributors

fabn avatar

Stargazers

Javier Sanz avatar  avatar Jason Clark avatar Hieu Pham avatar Tiago Serra avatar Elvis Moreira avatar  avatar Alvin Lai avatar

Watchers

 avatar James Cloos avatar

Forkers

mrhieu

google_authentication's Issues

Tokens

I am uncertain if the devise helpers are working correctly. After following your readme steps, I can click "login" which directs the browser to the /auth/google_apps and I am logged in with the flash message saying, "login successful."

However,

even though i have <% if user_signed_in?%> <% link_to "sign-out", "signout_path" %> , user_signed_in is failing... and i still see a "login" link.

Error when generate install

rails generate google_authenticate:install
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/devise-2.1.0/lib/devise/omniauth.rb:9:in <top (required)>': uninitialized constant OmniAuth::VERSION (NameError) from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/devise-2.1.0/lib/devise.rb:375:inomniauth'
from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/google_authentication-0.1.0/lib/google_authentication/engine.rb:16:in block in <class:Engine>' from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:30:ininstance_exec'
from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:30:in run' from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:55:inblock in run_initializers'
from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:54:in each' from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:54:inrun_initializers'
from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/application.rb:136:in initialize!' from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/railtie/configurable.rb:30:inmethod_missing'
from /usr/local/nethome/rkass/projects/google_login/config/environment.rb:5:in <top (required)>' from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/application.rb:103:inrequire_environment!'
from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands.rb:25:in <top (required)>' from script/rails:6:inrequire'
from script/rails:6:in `

'

Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.1'

Bundle edge Rails instead:

gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

Gems used only for assets and not required

in production environments by default.

group :assets do
gem 'sass-rails', '> 3.2.3'
gem 'coffee-rails', '
> 3.2.1'

See https://github.com/sstephenson/execjs#readme for more supported runtimes

gem 'therubyracer'

#gem 'uglifier', '>= 1.0.3'
end
gem 'omniauth', '>= 0'
gem 'oa-openid'
gem 'jquery-rails'
gem 'google_authentication'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.