2016-10-21 1 views
0

Wenn ich ein neues Konto mit Google-Authentifizierung erstellen, erstellt es ein Konto und auch die Anmeldung automatisch.Rails devise - Authentifizierung Google - kann erstellen, aber nicht anmelden

Wenn ich logout und später Login erneut auf, mit dem Google-Konto, kann ich mich nicht anmelden ...

bekomme ich keine Fehlermeldung :(

class OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def facebook 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     sign_in_and_redirect @user, event: :authentication 
     set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format? 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 

    def google_oauth2 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     sign_in_and_redirect @user, event: :authentication 
     set_flash_message(:notice, :success, kind: "Google") if is_navigational_format? 
    else 
     session["devise.google_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 
end 

Weitere Informationen.

Hier ist das Protokoll, wenn die autentication/Kontoerstellung funktioniert:

Started GET "/auth/google_oauth2" for 177.207.232.141 at 2016-10-23 23:47:31 +0000 


Started GET "/auth/google_oauth2/callback?state=1f3b3124e0264c1dabb3f40059d9a15a9da524553e0ec5dc&code=4/ZjqBwsoXJJXncItmMDuwPssw0ooT1RwnNjkRIfilQYc" for 177.207.232.141 at 2016-10-23 23:47:33 +0000 
Processing by OmniauthCallbacksController#google_oauth2 as HTML 
    Parameters: {"state"=>"1f3b3124e0264c1dabb3f40059d9a15a9da524553e0ec5dc", "code"=>"4/ZjqBwsoXJJXncItmMDuwPssw0ooT1RwnNjkRIfilQYc"} 
    [1m[35mSubdomain Load (27.7ms)[0m SELECT "subdomains".* FROM "subdomains" WHERE "subdomains"."address" = $1 LIMIT 1 [["address", "yoga"]] 
    [1m[36mDomain Load (27.5ms)[0m [1mSELECT "domains".* FROM "domains" WHERE "domains"."address" = $1 LIMIT 1[0m [["address", "smartmarket.io"]] 
    [1m[35mUser Load (28.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["provider", "google_oauth2"], ["uid", "110174173081848856467"]] 
    [1m[36m (27.7ms)[0m [1mBEGIN[0m 
    [1m[35mUser Exists (27.8ms)[0m SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
    [1m[36mUser Exists (27.8ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1[0m 
    [1m[35mSQL (27.9ms)[0m INSERT INTO "users" ("provider", "uid", "name", "email", "image", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["provider", "google_oauth2"], ["uid", "really?"], ["name", "Diogo Wernik"], ["email", "[email protected]"], ["image", "https://lh6.googleusercontent.com/-vXjHRteu8BY/AAAAAAAAAAI/AAAAAAAAB3A/_oEJPVq8h8o/photo.jpg"], ["encrypted_password", "$2a$10$6CIsOntiZxoLQfyUTfskiuys3P4u6.M0O4h/4S5zoW7EpN2KgX4Re"], ["created_at", "2016-10-23 23:47:34.023034"], ["updated_at", "2016-10-23 23:47:34.023034"]] 
    [1m[36mSQL (28.1ms)[0m [1mINSERT INTO "profiles" ("name", "kind", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"[0m [["name", "Diogo Wernik"], ["kind", "blog"], ["user_id", 5], ["created_at", "2016-10-23 23:47:34.082199"], ["updated_at", "2016-10-23 23:47:34.082199"]] 
    [1m[35mSQL (28.4ms)[0m INSERT INTO "user_main_profiles" ("profile_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["profile_id", 5], ["user_id", 5], ["created_at", "2016-10-23 23:47:34.140877"], ["updated_at", "2016-10-23 23:47:34.140877"]] 
    [1m[36mUserMainProfile Load (27.7ms)[0m [1mSELECT "user_main_profiles".* FROM "user_main_profiles" WHERE "user_main_profiles"."user_id" = $1 LIMIT 1[0m [["user_id", 5]] 
    [1m[35m (28.1ms)[0m COMMIT 
    [1m[36mUser Load (27.9ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 1]] 
    [1m[35m (27.5ms)[0m BEGIN 
    [1m[36mSQL (27.9ms)[0m [1mUPDATE "users" SET "last_sign_in_at" = $1, "current_sign_in_at" = $2, "last_sign_in_ip" = $3, "current_sign_in_ip" = $4, "sign_in_count" = $5, "updated_at" = $6 WHERE "users"."id" = $7[0m [["last_sign_in_at", "2016-10-23 23:47:34.301029"], ["current_sign_in_at", "2016-10-23 23:47:34.301029"], ["last_sign_in_ip", "177.207.232.141/32"], ["current_sign_in_ip", "177.207.232.141/32"], ["sign_in_count", 1], ["updated_at", "2016-10-23 23:47:34.329926"], ["id", 5]] 
    [1m[35m (28.0ms)[0m COMMIT 
Redirected to http://yoga.smartmarket.io/ 
Completed 302 Found in 636ms (ActiveRecord: 418.1ms) 
    [1m[36m (27.5ms)[0m [1mBEGIN[0m 
    [1m[35mSQL (27.7ms)[0m DELETE FROM "sessions" WHERE "sessions"."id" = $1 [["id", 11]] 
    [1m[36m (27.9ms)[0m [1mCOMMIT[0m 

Hier wird wenn ich abmelden und versuchen Sie es erneut

Started GET "/auth/google_oauth2" for 177.207.232.141 at 2016-10-23 23:51:13 +0000 


Started GET "/auth/google_oauth2/callback?state=ca339595a33e9e192c274a78450400a02e7edfd7f7ebb937&code=4/bQ59Fm5Fcxv_vWtBlFpMAwMoY6iig8J6bZGWFKboZms" for 177.207.232.141 at 2016-10-23 23:51:14 +0000 
Processing by OmniauthCallbacksController#google_oauth2 as HTML 
    Parameters: {"state"=>"ca339595a33e9e192c274a78450400a02e7edfd7f7ebb937", "code"=>"4/bQ59Fm5Fcxv_vWtBlFpMAwMoY6iig8J6bZGWFKboZms"} 
    [1m[35mSubdomain Load (27.6ms)[0m SELECT "subdomains".* FROM "subdomains" WHERE "subdomains"."address" = $1 LIMIT 1 [["address", "yoga"]] 
    [1m[36mDomain Load (27.5ms)[0m [1mSELECT "domains".* FROM "domains" WHERE "domains"."address" = $1 LIMIT 1[0m [["address", "smartmarket.io"]] 
    [1m[35mUser Load (27.9ms)[0m SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["provider", "google_oauth2"], ["uid", "110174173081848856467"]] 
    [1m[36m (27.7ms)[0m [1mBEGIN[0m 
    [1m[35mUser Exists (28.5ms)[0m SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
    [1m[36mUser Exists (28.0ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'di[email protected]' LIMIT 1[0m 
    [1m[35m (27.8ms)[0m ROLLBACK 
Redirected to http://yoga.smartmarket.io/sign_up 
Completed 302 Found in 271ms (ActiveRecord: 195.0ms) 


Started GET "/sign_up" for 177.207.232.141 at 2016-10-23 23:51:15 +0000 
Processing by Devise::RegistrationsController#new as HTML 
    [1m[36mSubdomain Load (27.5ms)[0m [1mSELECT "subdomains".* FROM "subdomains" WHERE "subdomains"."address" = $1 LIMIT 1[0m [["address", "yoga"]] 
    [1m[35mDomain Load (27.7ms)[0m SELECT "domains".* FROM "domains" WHERE "domains"."address" = $1 LIMIT 1 [["address", "smartmarket.io"]] 
    Rendered devise/registrations/_social.erb (0.5ms) 
    Rendered devise/registrations/new.html.erb within layouts/application (2.0ms) 
    Rendered layouts/partials/_topnavbar.html.erb (0.4ms) 
    Rendered layouts/partials/_sidebar.html.erb (0.1ms) 
    Rendered layouts/partials/_footer.html.erb (0.0ms) 
    Rendered layouts/partials/_alerts.erb (0.1ms) 
Completed 200 OK in 251ms (Views: 165.2ms | ActiveRecord: 83.0ms) 


Started GET "/api/i18n/site-pt.json" for 177.207.232.141 at 2016-10-23 23:51:16 +0000 
Processing by ApiController#i18n as JSON 
    Parameters: {"locale"=>"site-pt"} 
    Rendered app/assets/i18n/site-pt.json (0.1ms) 
Completed 200 OK in 35ms (Views: 5.8ms | ActiveRecord: 27.9ms) 


Started GET "/assets/fontawesome/fonts/fontawesome-webfont.woff2?v=4.5.0" for 177.207.232.141 at 2016-10-23 23:51:16 +0000 


Started GET "/sign_up" for 177.207.232.141 at 2016-10-23 23:51:16 +0000 
Processing by Devise::RegistrationsController#new as HTML 
    [1m[36mSubdomain Load (27.6ms)[0m [1mSELECT "subdomains".* FROM "subdomains" WHERE "subdomains"."address" = $1 LIMIT 1[0m [["address", "yoga"]] 
    [1m[35mDomain Load (27.5ms)[0m SELECT "domains".* FROM "domains" WHERE "domains"."address" = $1 LIMIT 1 [["address", "smartmarket.io"]] 
    Rendered devise/registrations/_social.erb (0.5ms) 
    Rendered devise/registrations/new.html.erb within layouts/application (2.2ms) 
    Rendered layouts/partials/_topnavbar.html.erb (0.4ms) 
    Rendered layouts/partials/_sidebar.html.erb (0.1ms) 
    Rendered layouts/partials/_footer.html.erb (0.0ms) 
    Rendered layouts/partials/_alerts.erb (0.1ms) 
Completed 200 OK in 231ms (Views: 144.7ms | ActiveRecord: 83.0ms) 

hier mit dem gleichen Konto einzuloggen ist die user.rb

class User < ActiveRecord::Base 
    include Userable 

    # include DeviseTokenAuth::Concerns::User 


    # Include default devise modules. Others available are: 
    # :lockable, :timeoutable 

    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, 
     :validatable, :omniauthable # , :confirmable 


    validates :email, uniqueness: true 

    enum permission: [:guest, :admin] 

    after_create :create_profile!, :send_welcome_email! 
    before_validation :set_uid! 

    def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.name = auth.info.name 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.email = auth.info.email 
     user.image = auth.info.image 
     user.password = Devise.friendly_token[0, 20] 
    end 
    end 


    def main_profile 
    user_main_profile.try :profile 
    end 

    private 

    def set_uid! 
    self.uid = 'really?' 
    end 

    def create_profile! 
    profile = profiles.create(
     name: name, 
     kind: 'blog' 
    ) 

    create_user_main_profile(profile_id: profile.id) 
    end 

    def send_welcome_email! 
    UserMailer.delay.welcome(self.id) 
    end 

end 

Antwort

1

Was kann ich sagen, ist, dass Ihre INSERT:

[1m[35mSQL (27.9ms)[0m INSERT INTO "users" ("provider", "uid", "name", "email", "image", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["provider", "google_oauth2"], ["uid", "really?"], ["name", "Diogo Wernik"], ["email", "[email protected]"], ["image", "https://lh6.googleusercontent.com/-vXjHRteu8BY/AAAAAAAAAAI/AAAAAAAAB3A/_oEJPVq8h8o/photo.jpg"], ["encrypted_password", "$2a$10$6CIsOntiZxoLQfyUTfskiuys3P4u6.M0O4h/4S5zoW7EpN2KgX4Re"], ["created_at", "2016-10-23 23:47:34.023034"], ["updated_at", "2016-10-23 23:47:34.023034"]] 

ist vorbei Feld ["uid", "really?"] wenn es vorbei sein sollte ["uid", "110174173081848856467"]

Also wenn th e Benutzer versucht, über Google einzuloggen, sucht:

SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["provider", "google_oauth2"], ["uid", "110174173081848856467"]] 

Ihr user.rb Überprüfen Sie, wie omniauth die Variablen bekommt oder Ihren Beitrag mit dem Inhalt bearbeiten.

+0

Ja, Sie haben das Problem gefunden, aber ich konnte nicht finden, wie zu beheben :) ... gut, könnten Sie mir helfen zu finden, ich konnte nicht herausfinden, wie zu beheben .. wie man es die richtige UID speichern ? Ich habe nur die user.rb Datei eingefügt ... –

+0

Die Lösung gefunden !!! Vielen Dank, ich habe nur die set_uid gelöscht, dass ich nicht weiß, warum es dort war und arbeitete !!! –

Verwandte Themen