我只是想使用devise
+ oauth
,让用户用他们的instagram账户登录我的应用。我尝试了几个教程,devcamp和tried视频,但就是不能让它工作。有没有一个working step by step指南,关于如何使用devise在instagram上实现omniauth?
每次我想要阅读文档时,我都会感到困惑,因为instagram只是在一个区块中粘贴了大量代码,而没有解释这些代码属于哪里。此外,他们从不使用纯镶嵌的红宝石,而是使用haml之类的东西。
现在,我得到了这样的结果:
omniauth_callbacks_controller:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def instagram
end
end
routes.rb
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
initializers/devise.rb
config.omniauth :instagram, ENV["INSTAGRAM_CLIENT_ID"], ENV["INSTAGRAM_CLIENT_SECRET"]
# i had plain text instead of the env tags, but it made no difference
在我的user.rb中,我放置了一些我不太理解的session内容的代码,但它也不起作用。
发布于 2019-03-15 06:27:41
代码示例
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def instagram
session["devise.instagram_data"] = request.env["omniauth.auth"]
if user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect user
else
redirect_to new_user_registration_path
end
end
def failure
redirect_to root_path
end
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, omniauth_providers: [:instagram]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).take
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.instagram_data"]
user.uid = data["uid"]
user.provider = data["provider"]
user.password = Devise.friendly_token
end
end
end
end
完整的例子在这里https://github.com/germanescobar/rails-devise-instagram/blob/master/app/models/user.rb
https://stackoverflow.com/questions/55167053
复制相似问题