我正在使用rails,Rspotify和Angular制作应用程序。用户使用Spotify进行身份验证,他们搜索音乐,将音乐添加到播放列表(制作播放列表),然后将该信息存储在PostgreSQL中。我在所有这些数据上创建一个API,并使用Angular显示它。Omniauth用户值返回null
我已经能够获得音乐+音乐搜索功能正常工作。我正在努力建立一个用户和用户模型,但是当谈到会话和验证用户时,我没有做正确的事情。我得到重定向到正确的路径,并没有看到任何错误,但我的代码返回
{
"country": null,
"display_name": null,
"email": null,
"images": null,
"product": null,
"external_urls": null,
"href": null,
"id": null,
"type": null,
"uri": null
}
这是我的代码;
的routes.rb
root to: "home#index"
#auth
get '/auth/:spotify/callback', to: 'sessions#create', via: [:get, :post]
resources :users
get '/home', to: 'users#home'
users_controller.rb
class UsersController < ApplicationController
# user's home, where current null data is
def home
@spotify_user = RSpotify::User.new
render :json => @spotify_user
end
def music
@music = RSpotify::Track.search(params[:search])
render :json => @music
end
end
sessions_controller.rb
class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
@spotify_user = RSpotify::User.new(:provider => auth['provider'],
:uid => auth['uid'].to_s) || User.create_with_omniauth(auth)
reset_session
redirect_to "/home", :notice => "Authenticated!"
end
#sign out
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "Signed out!"
end
end
users.rb的
class User < ActiveRecord::Base
def self.from_omniauth(auth)
where(auth.slice("provider", "uid")).first || create_from_omniauth(auth)
end
def self.create_from_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["nickname"]
end
end
end
我也注意到,我没有收到来自Spotify的一个uid
。当我跑 render :text => "<pre>" + env["omniauth.auth"].to_yaml and return
在我的sessions#create
,它返回的信息,但uid
是空白。作为参考,我已经能够使用的唯一指南是RailsCasts 241 Simple Omniauth。我一直有这个问题,我知道我是sooooo接近找出它,因为我终于有零错误,并可以看到数据。如果任何人之前做过这样的事情,并且可以指引我朝着正确的方向发展,那么我将不胜感激。感谢任何和所有的帮助。
谢谢。那么会话控制器和用户模型与Rspotify没有互动?它只是在数据库中保存rspotify用户信息?如果我放弃rspotify并使它成为'@spotify_user = User.new',然后保留我的代码的其余部分?对不起,我觉得我太过于复杂了...... – user3749994 2014-08-30 22:20:09