ruby-on-rails - 如何检查 current_user 是否通过 omniauth 登录?

标签 ruby-on-rails ruby rubygems

如果我有 omniauth 允许用户使用 facebook/twitter 等提供商登录,我如何检查 current_user 是否使用 omniauth 登录?

我想让代码仅在当前用户通过 omniauth 登录时运行。

这是我的模式的样子

create_table "users", :force => true do |t|
    t.string    "name"
    t.string    "email"
    t.timestamp "created_at",                         :null => false
    t.timestamp "updated_at",                         :null => false
    t.string    "password_digest"
    t.string    "remember_token"

  end

 create_table "authentications", :force => true do |t|
    t.integer  "user_id"
    t.string   "provider"
    t.string   "uid"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.string   "secret"
    t.string   "token"
  end

Controller

    class PostsController < ApplicationController
    def create
      @post = current_user.posts.build(params[:post])

      if @post.save
        if @post.check
          UserMailer.check_email(@user).deliver
        else
          flash[:success] = "Posted"
          Twitter::Client.new.update(@post.content) if logged_using_omniauth? request != nil
        end
        redirect_to root_path
      else
        @feed_items = []
        render 'static_pages/home'
      end
    end

最佳答案

我建议维护一个 session 来检查它是否通过 omniauth 登录。

在 OmniauthCallbacksController 中

def method_name
 .......
 #if logged in set the session
 session[:logged_in_using_omniauth] = true
 .......
end

在应用程序 Controller 中

def logged_in_using_omniauth?
  session[:logged_in_using_omniauth].present?
end

并通过以下方式将该方法公开为辅助方法:

helper_method :logged_in_using_omniauth?

现在在 Controller 或 View 中的任何地方使用 logged_in_using_omniauth?

关于ruby-on-rails - 如何检查 current_user 是否通过 omniauth 登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15955139/

相关文章:

ruby-on-rails - 如何在我的 ubuntu 服务器上启动 Rails 控制台

ruby-on-rails - Rails 缓存清扫器

Ruby Rescue 显示完整的回溯

ruby - Ruby 数组是真正的数组吗?

ruby-on-rails - 添加两个变量,使它们成为一个字符串,在 Ruby 中有一个空格

ruby - 具有身份验证的私有(private) Ruby Gem 服务器

mysql - rails 服务器错误,mysql

ruby-on-rails - Ruby 中的日期减法

mysql - 考虑到性能,如何设计允许每个用户一票的评级系统?

ruby-on-rails - 有没有一种简单的方法可以从 ruby​​ 中的日期字符串中获取年份?