Rails でトークンを扱う方法
- POST
環境 rails: rails 5.2.3 方法 トークンの認証 トークンの認証については authenticate_with_http_token を使用する
以下が対象
Authorization: Token XXX Authorization: Bearer XXX class ApplicationController < ActionController::API # authenticate_with_http_token を使用するために必要 include ActionController::HttpAuthentication::Token::ControllerMethods before_action :authenticate def authenticate authenticate_with_http_token do |token, options| # 認証処理 および レスポンスが必要な場合は記述 end end end トークンの取得 トークンの取得については token_and_options を使用する
以下が対象
Authorization: Token XXX Authorization: Bearer XXX class ApplicationController < ActionController::API # token_and_options を使用するために必要 include ActionController::HttpAuthentication::Token def access_token token_and_options(request)&.first end end