0

此时,用户可以使用带有omniauth-facebook gem 的Facebook 帐户登录我的网站。我还想为用户实现一种方法,只需使用像 devise 这样的 gem 创建一个帐户,这样他们在发帖时就可以保持匿名。

这是我的会话控制器中的内容

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to root_url
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url
  end
end

然后是我的用户模型

class User < ActiveRecord::Base
include SessionsHelper

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.name = auth.info.name
        user.oauth_token = auth.credentials.token
        user.oauth_expires_at = Time.at(auth.credentials.expires_at)
        user.save!
    end
  end

  has_many :stories
end

这是我可以通过添加设计 gem 来解决的问题,还是我需要在我的 session_helper 或用户模型中添加一些东西?

4

1 回答 1

0

Devise has functionality built in to be able to use all of the various omniauth-* gems. You should definitely look into it. https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

于 2013-10-10T03:21:04.740 回答