首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用户应使用instagram登录,应创建记录

用户应使用instagram登录,应创建记录
EN

Stack Overflow用户
提问于 2019-03-15 00:01:25
回答 1查看 206关注 0票数 0

我只是想使用devise + oauth,让用户用他们的instagram账户登录我的应用。我尝试了几个教程,devcamp和tried视频,但就是不能让它工作。有没有一个working step by step指南,关于如何使用devise在instagram上实现omniauth?

每次我想要阅读文档时,我都会感到困惑,因为instagram只是在一个区块中粘贴了大量代码,而没有解释这些代码属于哪里。此外,他们从不使用纯镶嵌的红宝石,而是使用haml之类的东西。

现在,我得到了这样的结果:

omniauth_callbacks_controller:

代码语言:javascript
复制
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def instagram
  end
end

routes.rb

代码语言:javascript
复制
  devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}

initializers/devise.rb

代码语言:javascript
复制
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内容的代码,但它也不起作用。

EN

回答 1

Stack Overflow用户

发布于 2019-03-15 06:27:41

代码示例

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55167053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档