Ruby on rails Omniauth用户值返回null

Ruby on rails Omniauth用户值返回null,ruby-on-rails,postgresql,session,omniauth,Ruby On Rails,Postgresql,Session,Omniauth,我正在用rails和Angular制作一个应用程序。用户通过Spotify进行身份验证,搜索音乐,将音乐添加到播放列表(制作播放列表),然后将该信息存储在PostgreSQL中。我对所有这些数据制作了一个API,并以角度显示 我已经能够让音乐+音乐搜索功能正常工作。我正在努力创建一个用户和用户模型,但在会话和验证用户时,我做得不对。我被重定向到正确的路径,没有看到任何错误,但我的代码返回 { "country": null, "display_name": null, "email": null

我正在用rails和Angular制作一个应用程序。用户通过Spotify进行身份验证,搜索音乐,将音乐添加到播放列表(制作播放列表),然后将该信息存储在PostgreSQL中。我对所有这些数据制作了一个API,并以角度显示

我已经能够让音乐+音乐搜索功能正常工作。我正在努力创建一个用户和用户模型,但在会话和验证用户时,我做得不对。我被重定向到正确的路径,没有看到任何错误,但我的代码返回

{
"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'
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
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
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
用户\u控制器.rb

root to: "home#index"

  #auth
  get '/auth/:spotify/callback', to: 'sessions#create', via: [:get, :post]

  resources :users

  get '/home', to: 'users#home'
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
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
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
class UsersController@spotify\u user
终止
def音乐
@music=RSpotify::Track.search(参数[:search])
render:json=>@music
终止
终止
会话\u controller.rb

root to: "home#index"

  #auth
  get '/auth/:spotify/callback', to: 'sessions#create', via: [:get, :post]

  resources :users

  get '/home', to: 'users#home'
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
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
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
class sessioncontrollerauth['provider'],
:uid=>auth['uid'].to_s)| | User。使用_omniauth(auth)创建_
重置会话
将_重定向到“/home”,注意=>“已验证!”
终止
#注销
def销毁
会话[:用户\u id]=无
重定向到root\u url,注意:“已注销!”
终止
终止
users.rb

root to: "home#index"

  #auth
  get '/auth/:spotify/callback', to: 'sessions#create', via: [:get, :post]

  resources :users

  get '/home', to: 'users#home'
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
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
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
class用户
我还注意到,我没有从Spotify收到
uid
。当我跑的时候
render:text=>“”+env[“omniauth.auth”]。发送到_yaml并返回

@spotify_user = RSpotify::User.new
render :json => @spotify_user

在我的
会话#create
中,它返回了信息,但
uid
为空。作为参考,我能使用的唯一指南是。我已经有一段时间遇到这个问题了,我知道我很快就能解决它了,因为我最终没有错误,并且可以看到数据。如果有人以前做过这样的事情,能给我指出正确的方向,我将不胜感激。谢谢你的帮助

您将应用程序的
用户
s与Spotify gem的
RSpotify::User
s混淆了

当用户注册OmniAuth时,必须将其保存到数据库中。然后,在UsersController中,您必须找到该用户并将其作为json发送

看看你在这里做什么:


您正在创建一个新用户(每个属性都为nil),然后将其呈现为json。

谢谢。会话控制器和用户模型与Rspotify没有交互?它只是在数据库中保存rspotify用户信息?如果我删除rspotify并将其设置为
@spotify\u user=user.new
,然后将其余代码保持原样?对不起,我想我把它复杂化了。。。