让用户使用Facebook登录或创建一个帐户 [英] Having users login using facebook or create an account

查看:172
本文介绍了让用户使用Facebook登录或创建一个帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一点上,用户可以使用他们的Facebook帐户登录到我的网站与omniauth-facebook宝石。我也想实现一种方式,让用户只需创建一个使用宝石像设计的帐户,这样他们在发布时可以保持一点点匿名。

At this point users are able to login to my website using their facebook account with omniauth-facebook gem. I also want to implement a way for users to just create an account using a gem like devise, so that they can remain a bit more anonymous when posting.

这是我的在我的sessions_controller中

Here's what I have in my sessions_controller

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

这是我可以通过添加在devise gem中解决的东西,还是需要在我的sessions_helper或用户模型中添加一些东西?

Is this something I can just solve by adding in the devise gem or will I need to add a few things in my sessions_helper or user model?

推荐答案

Devise具有内置的功能,能够使用所有的各种omniauth- * gems。你一定要看看。 https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

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

这篇关于让用户使用Facebook登录或创建一个帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆