当我没有session /:id(。:format)的id时,如何在Rails API中销毁Session?

时间:2021-03-15 21:04:44

I don't have a model Session. There are no id's, rows, data, etc.

我没有模型会话。没有id,行,数据等。

I have...

module Api
    module V1
        class SessionsController < ApplicationController
            include Api::V1::SessionsHelper
            respond_to :json
            skip_before_filter :verify_authenticity_token

            def create
                @user = User.find_by(email: params[:session][:email].downcase)
                if @user && @user.authenticate(params[:session][:password])
                    token = User.new_remember_token
                    sign_in @user, token
                    render json: {status: :success, token: token, user_id: @user.id }
                else
                    render json: {status: :failure, error: "Authentication Failed"}
                end
            end

            def destroy
                token = User.digest([:session][:token])
                @user = User.find_by(remember_token: [:session][:token])
                if @user
                    sign_out
                    render json: {status: :success}
                else
                    render json: {status: :failure, error: "Could not find user to sign out"}
                end

            end
        end
    end
end

Now in PostMan/iOS, I can create a new session by sending a POST to api/v1/sessions with json that looks like

现在在PostMan / iOS中,我可以通过向json发送一个POST到api / v1 / sessions来创建一个新会话

{
    "session": 
        {
            "email": "v@v.com",
            "password": "vvvvvv"
        }
}

This will send back a token to the caller to be stored on the device, and the hashed token will be stored along with the User model.

这将向调用者发送一个令牌以存储在设备上,并且散列令牌将与用户模型一起存储。

However, if I want to destroy the session and send in...

但是,如果我想破坏会话并发送...

{
    "session": 
        {
            "token": "34Y6hFDA8LyVNexlKHxwwQ"
        }   
}

I can't do this because the rake routes has...

我不能这样做,因为耙路线有......

api_v1_sessions POST   /api/v1/sessions(.:format)                        api/v1/sessions#create {:format=>"json"}
api_v1_session DELETE /api/v1/sessions/:id(.:format)                    api/v1/sessions#destroy {:format=>"json"}

/api/v1/sessions/:id(.:format). I don't have an id to pass in. I just want to be able to DELETE at /api/v1/sessions, pass in the unhashed token, and then my controller can hash it, search for the token in the DB, and remove it.

/api/v1/sessions/:id(.:format)。我没有要传入的ID。我只是希望能够在/ api / v1 / sessions中删除,传入未散列的令牌,然后我的控制器可以对其进行哈希处理,在数据库中搜索令牌,以及去掉它。

How can I do this?

我怎样才能做到这一点?

2 个解决方案

#1


0  

Since you do not use the id parameter, you can just set it to anything in the URL, like:

由于您不使用id参数,因此您可以将其设置为URL中的任何内容,例如:

/api/v1/sessions/dummy

A test with curl would look like this:

卷曲测试看起来像这样:

curl -X DELETE -d "session[token]=token-here" http://localhost:3000/api/v1/sessions/dummy

#2


-1  

you can to change in routes.rb resources to resource

您可以将routes.rb资源更改为资源

example: resource :sessions, only: %i[create destroy show]

示例:资源:会话,仅限:%i [创建销毁显示]

#1


0  

Since you do not use the id parameter, you can just set it to anything in the URL, like:

由于您不使用id参数,因此您可以将其设置为URL中的任何内容,例如:

/api/v1/sessions/dummy

A test with curl would look like this:

卷曲测试看起来像这样:

curl -X DELETE -d "session[token]=token-here" http://localhost:3000/api/v1/sessions/dummy

#2


-1  

you can to change in routes.rb resources to resource

您可以将routes.rb资源更改为资源

example: resource :sessions, only: %i[create destroy show]

示例:资源:会话,仅限:%i [创建销毁显示]