· 7 years ago · Jan 21, 2018, 10:52 AM
1OmniAuth.config.logger = Rails.logger
2Rails.application.config.middleware.use OmniAuth::Builder do
3provider :google_oauth2,"google_clent_id", "google_secret_id",{client_options: {ssl: {ca_file:Rails.root.join("cacert.pem").to_s}}}
4end
5
6config.omniauth :google_oauth2, "google_client_id", "google_secret_id", { name: 'google' }
7
8devise_for :users, :controllers => { sessions: "sessions", registrations: 'registrations',:omniauth_callbacks => "users/omniauth_callbacks" }
9 get 'auth/:provider/callback', to: 'sessions#create'
10 get 'auth/failure', to: redirect('/')
11 get 'signout', to: 'sessions#destroy', as: 'signout'
12
13class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
14skip_before_action :verify_authenticity_token
15 def google_oauth2
16 @user = User.from_omniauth(request.env["omniauth.auth"])
17 if @user.persisted?
18 flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
19 sign_in_and_redirect @user, :event => :authentication
20 else
21 session["devise.google_data"] = request.env["omniauth.auth"]
22 redirect_to new_user_registration_url
23 end
24 end
25end
26
27devise :database_authenticatable, :registerable,
28 :recoverable, :rememberable, :trackable, :omniauthable,
29 :omniauth_providers => [:google_oauth2]
30 def self.from_omniauth(auth)
31 where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
32 user.provider = auth.provider
33 user.uid = auth.uid
34 full_name = auth.info.name.split(" ")
35 user.first_name = full_name[0]
36 user.last_name = full_name[1]
37 user.email = auth.info.email
38 user.oauth_token = auth.credentials.token
39 user.oauth_expires_at = Time.at(auth.credentials.expires_at)
40 user.save!
41 end
42 end
43
44def create
45 user = User.from_omniauth(env["omniauth.auth"])
46 session[:user_id] = user.id
47 redirect_to root_path
48 end
49
50 def destroy
51 session[:user_id] = nil
52 redirect_to root_path
53 end
54
55<% if current_user %>
56 Signed in as <strong><%= current_user.name %></strong>!
57 <%= link_to "Sign out", signout_path, id: "sign_out" %>
58 <% else %>
59 <%= link_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path, id: "sign_in" %>
60 <% end %>
61
62Started GET "/users/auth/google_oauth2" for 127.0.0.1 at 2018-01-21 15:49:11 +0530
63 (0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
64(google_oauth2) Request phase initiated.
65Started GET "/users/auth/google_oauth2/callback?state=*****&code=4/****" for 127.0.0.1 at 2018-01-21 15:49:16 +0530
66(google_oauth2) Callback phase initiated.
67Processing by Users::OmniauthCallbacksController#google_oauth2 as HTML
68 Parameters: {"state"=>"****", "code"=>"4/****"}
69 User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."provider" = ? AND "users"."uid" = ? ORDER BY "users"."id" ASC LIMIT ? [["provider", "google_oauth2"], ["uid", "*****"], ["LIMIT", 1]]
70 (0.1ms) begin transaction
71 SQL (3.5ms) UPDATE "users" SET "oauth_token" = ?, "oauth_expires_at" = ?, "updated_at" = ? WHERE "users"."id" = ? [["oauth_token", "*********"], ["oauth_expires_at", "2018-01-21 11:19:16"], ["updated_at", "2018-01-21 10:19:17.882842"], ["id", 2]]
72 (105.7ms) commit transaction
73 User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
74Redirected to http://localhost:3000/
75Completed 302 Found in 220ms (ActiveRecord: 111.1ms)
76
77
78Started GET "/" for 127.0.0.1 at 2018-01-21 15:49:18 +0530
79Processing by HomeController#index as HTML
80 Rendering home/index.html.erb within layouts/application
81 Rendered layouts/sliders/_slider_home.html.erb (0.4ms)
82 Rendered customers/_index.html.erb (1.6ms)
83 Rendered home/index.html.erb within layouts/application (3.4ms)
84 Rendered layouts/header/_style_files.html.erb (0.3ms)
85 Rendered layouts/_meta_files.html.erb (1.5ms)
86 Rendered layouts/logos/_logo_main.html.erb (1.4ms)
87 Rendered layouts/header/_header_search.html.erb (2.5ms)
88 Rendered layouts/header/_header_navigation.html.erb (0.6ms)
89 Rendered layouts/logos/_logo_secondary.html.erb (1.1ms)
90 Rendered layouts/footer/_footer.html.erb (2.3ms)
91 Rendered layouts/static/_hover_top.html.erb (0.4ms)
92 Rendered layouts/footer/_script_files.html.erb (1.1ms)
93Completed 200 OK in 727ms (Views: 724.5ms | ActiveRecord: 0.0ms)