美好的一天,我是铁杆上的红宝石新手。我正在开发一个旅游应用程序,我想提供登录Facebook的选项。实际上我的应用程序在使用facebook进行注册时工作正常,但是当我关闭会话时以及当我再次尝试使用facebook登录时出现错误,它显示'电子邮件已被采取'有人可以帮忙吗?我将非常感谢你的帮助。
user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, omniauth_providers: [:facebook]
has_many :destinations
has_many :deals
has_many :payments
has_many :transactions
has_many :questions
has_many :comments
def self.find_or_create_by_omniauth(auth)
user = User.find_by(provider: 'provider', uid: ':uid')
unless user
user = User.create(
name: auth[:name],
last_name: auth[:last_name],
nickname: auth[:nickname],
email: auth[:email],
uid: auth[:uid],
provider: auth[:provider],
password: Devise.friendly_token[0,20]
)
end
user
end
end
的routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
end
omniauth_callbacks_controller.rb
class OmniauthCallbacksController < ApplicationController
def facebook
auth = request.env["omniauth.auth"]
data = {
name: auth.info.first_name,
last_name: auth.info.last_name,
nickname: auth.info.nickname,
email: auth.info.email,
provider: auth.provider,
uid: auth.uid
}
@user = User.find_or_create_by_omniauth(data)
if @user.persisted?
sign_in_and_redirect @user, event: :authentication
else
session[:omniauth_errors] = @user.errors.full_messages.to_sentence unless @user.save
session[:omniauth_data] = data
redirect_to new_user_registration_url
end
end
end
缺少什么?
答案 0 :(得分:0)
更改以下行
user = User.find_by(provider: auth[:provider], uid: auth[:uid])
到
def self.find_or_create_by_omniauth(auth) # I recommend using a better name for this method instead of find_or_create_by as it looks like query
where(provider: auth[:provider], uid: auth[:uid]).first_or_create do |user|
name: auth[:name],
last_name: auth[:last_name],
nickname: auth[:nickname],
email: auth[:email],
uid: auth[:uid],
provider: auth[:provider],
password: Devise.friendly_token[0,20]
end
end
或进一步简化方法,请使用以下
const aws = require('aws-sdk');
var config = require('../../server/config.json');
aws.config = config.aws;
const s3 = new aws.S3();
const s3Params = {
Bucket: config.aws.bucket,
Key: instance.filename,
Expires: 60,
ACL: 'public-read'
};
s3.getSignedUrl('putObject', s3Params, (err, signedUrl) => {
if(err){
return next(err);
}
instance.uploadUrl = signedUrl;
next();
});