如果我有 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/