I'm trying to install omniauth-twitter and encountering some issues, mainly with the one outlined above. Initially I didn't have this issue but now I do so I'm scratching my head as to why this has cropped up. I've placed the consumer key and secret key in the secrets.yml file and also in my omniauth.rb file as copied below. I've read an awful lot of conflicting information as to whether this file is required and all this information goes into the devise.rb file. I haven't altered or placed any reference to omniauth in my devise.rb file. I have also ensured the appropriate url callback link ( http://127.0.0.1:3000/auth/twitter/callback ) has been placed in the twitter developers site.
Here's my code -
routes.rb
Rails.application.routes.draw do
get "/auth/:provider/callback" => "social_logins#create"
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users
resources :events do
resources :bookings
end
# get 'welcome/index'
authenticated :user do
root 'events#index', as: "authenticated_root"
end
root 'welcome#index'
end
social_logins.controller.rb
class SocialLoginsController < ApplicationController
def create
@details = request.env["omniauth.auth"].to_yaml
@provider = @details["provider"]
@provider_id = @details["uid"]
@user = User.where(provider: @provider, provider_id: @provider_id).first
if @user.present?
#sign them in
else
# make a new user
@user = User.new
@user.provider = @provider
@user.provider_id = @provider_id
# because of has_secure_password - will this work?
@user.password = "AAAAAA!!"
@user.password_confirmation = "AAAAAA!!"
# let's save the key and secret
@user.key = @details["credentials"]["token"]
@user.secret = @details["credentials"]["secret"]
# lets fill in their details
@user.name = @details["info"]["name"]
@user.email = @details["info"]["email"] || "aaaaa@email.com"
@user.save!
end
session[:uid] = @user.id
flash[:success] = "You've logged in"
redirect_to root_path
end
end
index.html.erb
<header>
<nav>
<div class="links">
<%= link_to 'Sign up', new_user_registration_path %>
<%= link_to 'Sign in', new_user_session_path %>
<%= link_to 'Go to Events', events_path %>
<%= link_to "Log in with Twitter", "/auth/twitter" %>
</div>
</nav>
</header>
omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, Rails.application.secrets.twitter_api_key, Rails.application.secrets.twitter_api_secret
end
你的用户模型中有这个吗?
devise omniauth_providers: [:twitter]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.