Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Rails Omniauth Google
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
=link_to "Login", '/auth/google', class: "btn btn-large btn-primary"
Rails.application.config.middleware.use OmniAuth::Builder do
provider :open_id, :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id'
end
match 'auth/google/callback', to: 'sessions#create'
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"]["info"])
if user.save
sign_in user
redirect_to user, flash: { success: "You have successfully signed in."}
else
redirect_to root_path, flash: { error: "You failed to sign in" }
end
end
def destroy
sign_out
redirect_to root_path
end
end
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"]["info"])
if user.save
sign_in user
redirect_to user, flash: { success: "You have successfully signed in."}
else
redirect_to root_path, flash: { error: "You must sign in with your Maluuba account." }
end
end
def destroy
sign_out
redirect_to root_path
end
end
class User < ActiveRecord::Base
before_save :create_remember_token
validates :name, presence: true
validates :email, presence:true,
uniqueness: { case_sensitive: false }
def self.from_omniauth(auth_info)
where(auth_info.slice("email", "name")).first ||
create_from_omniauth(auth_info)
end
def self.create_from_omniauth(auth_info)
new do |user|
user.email = auth_info[:email]
user.name = auth_info[:name]
end
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.