{"id":9464,"date":"2019-03-24T03:05:04","date_gmt":"2019-03-23T18:05:04","guid":{"rendered":"http:\/\/www.webcyou.com\/?p=9464"},"modified":"2019-03-24T03:17:45","modified_gmt":"2019-03-23T18:17:45","slug":"spa%e3%82%b5%e3%82%a4%e3%83%88%e3%81%a7%e3%81%ae%e8%aa%8d%e8%a8%bc%e8%aa%8d%e5%8f%af-jwt%e2%9c%97rails5%e2%9c%97nuxt-js","status":"publish","type":"post","link":"https:\/\/www.webcyou.com\/?p=9464","title":{"rendered":"SPA\u30b5\u30a4\u30c8\u3067\u306e\u8a8d\u8a3c\u8a8d\u53ef JWT\u2717Rails5\u2717Nuxt.js"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9465\" src=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/85c6dc8df2f148c963b5595dda299b90-768x307.png\" alt=\"\" width=\"768\" height=\"307\" srcset=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/85c6dc8df2f148c963b5595dda299b90-768x307.png 768w, https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/85c6dc8df2f148c963b5595dda299b90-768x307-300x120.png 300w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/p>\n<p>\u3069\u3082\u3067\u3059\u3002<\/p>\n<p>SPA\u3067\u30b5\u30fc\u30d3\u30b9\u3092\u30ac\u30f3\u30ac\u30f3\u4f5c\u6210\u3055\u308c\u3066\u3044\u308b\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>SPA\u306e\u8a8d\u8a3c\u8a8d\u53ef\u306e\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3068\u3057\u3066\u3001\u300c<strong>JWT\uff08JSON Web Tokens\uff09\u300d<\/strong>\u3092\u7528\u3044\u308b\u65b9\u3082\u591a\u3044\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u524d\u56de\u3001\u300c<a href=\"https:\/\/www.webcyou.com\/?p=9294\">Nuxt.js \u3068 auth-module \uff08@nuxtjs\/auth\uff09\u3067\u3001JWT\uff08JSON Web Tokens\uff09&amp; OAuth \u8a8d\u8a3c \u30ed\u30b0\u30a4\u30f3<\/a>\u300d\u306e\u8a18\u4e8b\u306b\u3066\u3001\u300c<a href=\"https:\/\/auth.nuxtjs.org\/\" target=\"_blank\" rel=\"noopener\">auth-module<\/a> \uff08<a href=\"https:\/\/github.com\/nuxt-community\/auth-module\" target=\"_blank\" rel=\"noopener\">@nuxtjs\/auth<\/a>\uff09\u300d\u3092\u7528\u3044\u305f\u300c<strong>JWT\uff08JSON Web Tokens\uff09&amp; OAuth \u8a8d\u8a3c \u30ed\u30b0\u30a4\u30f3<\/strong>\u300d\u306b\u3064\u3044\u3066\u8a18\u4e8b\u3092\u66f8\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u4eca\u56de\u30b5\u30fc\u30d0\u30fc\u3082\u4f5c\u6210\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u3068\u306b\u304b\u304f\u8a66\u3057\u305f\u3044\u65b9\u306f\u3053\u3061\u3089\u304b\u3089<\/p>\n<p><strong>Github<\/strong><\/p>\n<p>\u30fb\u30b5\u30fc\u30d0\u30fc\u5074\uff08Rails5\uff09<\/p>\n<p><a href=\"https:\/\/github.com\/webcyou-org\/jwt-server\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/webcyou-org\/jwt-server<\/a><\/p>\n<p>\u30fb\u30d5\u30ed\u30f3\u30c8\u5074\uff08Nuxt.js\uff09<\/p>\n<p><a href=\"https:\/\/github.com\/webcyou-org\/jwt-front\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/webcyou-org\/jwt-front<\/a><\/p>\n<p><strong>JWT\uff08JSON Web Tokens\uff09\u3092<\/strong>\u7528\u3044\u3066\u8a8d\u8a3c\u8a8d\u53ef\uff08\u30ed\u30b0\u30a4\u30f3\u5468\u308a\uff09\u306e\u5b9f\u88c5\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u30d5\u30ed\u30f3\u30c8\u5074\u306f\u3001<a href=\"https:\/\/www.webcyou.com\/?p=9294\" target=\"_blank\" rel=\"noopener\">\u524d\u56de<\/a>\u540c\u69d8\u300c<a href=\"https:\/\/auth.nuxtjs.org\/\" target=\"_blank\" rel=\"noopener\">auth-module<\/a> \uff08<a href=\"https:\/\/github.com\/nuxt-community\/auth-module\" target=\"_blank\" rel=\"noopener\">@nuxtjs\/auth<\/a>\uff09\u300d\u306e\u30b5\u30f3\u30d7\u30eb\u3092\u4f7f\u7528\u3059\u308b\u969b\u306b\u3001\u30d5\u30ed\u30f3\u30c8\u5074\u306e\u5b9f\u88c5\u3068\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u5b9f\u88c5\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>Rails\u306epublic\u30d5\u30a9\u30eb\u30c0\u306b\u3001Nuxt.js\u306eSPA\u30e2\u30fc\u30c9\u3067\u30ea\u30bd\u30fc\u30b9\u3092\u30d3\u30eb\u30c9\u3057\u305f\u3082\u306e\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-9466\" src=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/ac82fd9edf1610b6bce57dd86273974e.png\" alt=\"\" width=\"394\" height=\"124\" srcset=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/ac82fd9edf1610b6bce57dd86273974e.png 470w, https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/ac82fd9edf1610b6bce57dd86273974e-300x94.png 300w\" sizes=\"auto, (max-width: 394px) 100vw, 394px\" \/><\/p>\n<p>Rails\u5074\u3067\u3001\u30ed\u30b0\u30a4\u30f3\u3057\u305f\u308a\u3001\u30ed\u30b0\u30a4\u30f3\u5f8c\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u3092\u53c2\u7167\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-9467\" src=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/feb8baa3e79c6a28bf69c14c4e8f66b1.png\" alt=\"\" width=\"522\" height=\"282\" srcset=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/feb8baa3e79c6a28bf69c14c4e8f66b1.png 729w, https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/feb8baa3e79c6a28bf69c14c4e8f66b1-300x162.png 300w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/p>\n<p>\u524d\u56de\u3001\u30d5\u30ed\u30f3\u30c8\u5074\u306e\u5b9f\u88c5\u306b\u95a2\u3057\u3066\u3060\u3063\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u5b9f\u88c5\u306b\u95a2\u3057\u3066\u66f8\u3044\u3066\u3044\u3053\u3046\u304b\u306a\u3068\u3002<\/p>\n<p>\u30d5\u30ed\u30f3\u30c8\u5074\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u3001process.env.NODE_ENV\u306e\u300c<strong>development<\/strong>\u300d\uff08Rails API\u30b5\u30fc\u30d0\u30fc\u3092\u30dd\u30fc\u30c83000\u3067\u52d5\u304b\u3057\u3066\u3044\u308b\u5834\u5408\uff09\u306f\u3001config.proxy\u3092\u8a2d\u5b9a\u3057\u3001Nuxt.js\u306f\u30dd\u30fc\u30c83333\u3067\u7acb\u3061\u4e0a\u3052\u3001<a href=\"https:\/\/github.com\/webcyou-org\/jwt-front\/blob\/master\/nuxt.config.js#L36\" target=\"_blank\" rel=\"noopener\">\u300c\/api\u300d\u306f\u300chttp:\/\/localhost:3000\u300d\u306b\u5411\u3051\u3066\u3044\u308b<\/a>\u3068\u3053\u308d\u3067\u3059\u304b\u306d\u3002<\/p>\n<p>\u3042\u3068\u3001SPA\u306e\u8a8d\u8a3c\u8a8d\u53ef\u3067\u3001cookie\u3092\u7528\u3044\u3066\u3057\u307e\u3046\u3068\u8106\u5f31\u6027\u304c\u51fa\u3066\u3057\u307e\u3046\u306e\u3067\u3001<a href=\"https:\/\/github.com\/webcyou-org\/jwt-front\/blob\/master\/nuxt.config.js#L36\" target=\"_blank\" rel=\"noopener\">\u300cLocal Strage\u300d\u306b\u5909\u66f4<\/a>\u3057\u3066\u3044\u308b\u3068\u3053\u308d\u3068\u3001endpoints\u3092\u300c\/api\/v1\/\u300d\u306b\u5909\u66f4\u3057\u3001<a href=\"https:\/\/github.com\/webcyou-org\/jwt-front\/blob\/master\/nuxt.config.js#L42\" target=\"_blank\" rel=\"noopener\">\u300cpropertyName\u300d\u3092Rails\u30b5\u30fc\u30d0\u30fc\u5074\u304b\u3089\u53d7\u3051\u53d6\u308b\uff08knock\u30c7\u30d5\u30a9\u30eb\u30c8\uff09\u300cpropertyName\u300d\u306b\u5909\u66f4<\/a>\u3057\u305f\u3068\u3053\u308d\u3067\u3059\u304b\u306d\u3002<\/p>\n<p>\u3067\u306f\u3001\u30b5\u30fc\u30d0\u30fc\u5074\u3092Rails5 API\u30e2\u30fc\u30c9\u3067\u30b5\u30af\u30c3\u3068\u4f5c\u3063\u305f\u306e\u3067\u8efd\u304f\u8aac\u660e\u3067\u3082\u3068\u3002<\/p>\n<p>SPA\u3092\u5b9f\u88c5\u3057\u3088\u3046\u3068\u3057\u3066\u3044\u308b\u65b9\u306e\u4f55\u304b\u53c2\u8003\u306b\u306a\u308c\u3070\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\uff08Rails\u306b\u3064\u3044\u3066\u306e\u8aac\u660e\u3068\u304b\u306f\u5272\u611b\u3057\u3066\u304a\u308a\u307e\u3059\u3002\uff09<\/p>\n<h4>Rails5 API\u30b5\u30fc\u30d0\u30fc \uff08Gem Knock\u3092\u7528\u3044\u305fJWT\u8a8d\u8a3c\uff09<\/h4>\n<p>\u305d\u308c\u3067\u306f\u3001\u624b\u9806\u3092\u66f8\u3044\u3066\u884c\u304d\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u3001Rails API\u30e2\u30fc\u30c9\u306eweb\u30a2\u30d7\u30ea\u3092\u751f\u6210\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">$ bundle exec rails new jwt-server --api\r\n$ cd jwt-server<\/pre>\n<p>User Model\u3092 Rails \u30b8\u30a7\u30cd\u30ec\u30fc\u30c8\u30b3\u30de\u30f3\u30c9\u3092\u7528\u3044\u3066\u751f\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u30d1\u30b9\u30ef\u30fc\u30c9\u306fBcrypt\u3067\u6697\u53f7\u5316\u3057\u305f\u3082\u306e\u3092\u30bb\u30c3\u30c8\u3059\u308b\u305f\u3081\u3001has_secure_password\u30e1\u30bd\u30c3\u30c9\u3092\u7528\u3044\u3066\u751f\u6210\u3057\u3001\u305d\u308c\u3092\u4fdd\u5b58\u3059\u308b\u30ab\u30e9\u30e0\u300cpassword_digest\u300d\u3092\u7528\u610f\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">$ bin\/rails g modeluser password_digest:string name:string email:string--no-test-framework<\/pre>\n<p>app\/models\/user.rb\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3002<\/p>\n<p><strong>app\/models\/user.rb<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">class User &lt; ApplicationRecord\r\n  has_secure_password\r\n\r\n  validates :name, presence: true\r\n  validates :email, presence: true\r\nend<\/pre>\n<p>seed\u30d5\u30a1\u30a4\u30eb\u3092\u4fee\u6b63\u3057\u307e\u3059\u3002<\/p>\n<p><strong>db\/seeds.rb<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">User.destroy_all\r\n\r\nUser.create!({\r\n  name: 'daisuke.takayama',\r\n  email: 'test@user.com',\r\n  password: 'test123',\r\n  password_confirmation: 'test123'\r\n})<\/pre>\n<p>\u30c0\u30df\u30fc\u30c7\u30fc\u30bf\u3068\u3057\u3066\u3001User\u3092\u4e00\u4eba\u767b\u9332\u3057\u307e\u3059\u3002<\/p>\n<p>email\u3068\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u306f\u4e0a\u8a18\u306e\u69d8\u306b\u8a2d\u5b9a\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>\u6e96\u5099\u304c\u3067\u304d\u307e\u3057\u305f\u306e\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4f5c\u6210\u3068\u30de\u30a4\u30b0\u30ec\u30fc\u30c9\u3092\u5b9f\u884c\u3002<\/p>\n<p>seed\u30b3\u30de\u30f3\u30c9\u3067\u3001\u30c7\u30fc\u30bf\u3092\u633f\u5165\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">$ bin\/rails db:create db:migrate\r\n$ bin\/rails db:seed<\/pre>\n<p>\u3068\u3044\u3063\u305f\u4f5c\u696d\u304c\u3001<a href=\"https:\/\/github.com\/webcyou-org\/jwt-server\/commit\/1264dd2182dfba426422e856bf93a8489d38f72f\" target=\"_blank\" rel=\"noopener\">\u3053\u306e\u8fba\u306e\u30b3\u30df\u30c3\u30c8<\/a>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>User\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3082\u30b8\u30a7\u30cd\u30ec\u30fc\u30c8\u30b3\u30de\u30f3\u30c9\u3067\u751f\u6210\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">$ bin\/rails g controller Api\/V1\/Users<\/pre>\n<p>class\u3067\u751f\u6210\u3055\u308c\u305f\u306e\u3067\u3001module\u306b\u5909\u66f4\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/webcyou-org\/jwt-server\/commit\/ed6318b88b065eeb74e97dff0e0e1b4278c65060\" target=\"_blank\" rel=\"noopener\">\u305d\u306e\u8fba\u308a\u306e\u30b3\u30df\u30c3\u30c8<\/a>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h5>Gem Knock\u3092\u8ffd\u52a0<\/h5>\n<p>\u305d\u308c\u3067\u306f\u3001JWT\u8a8d\u8a3c Gem\u30d1\u30c3\u30b1\u30fc\u30b8\u3067\u3042\u308b\u300c<a href=\"https:\/\/github.com\/nsarno\/knock\">knock<\/a>\u300d\u3092\u8ffd\u52a0\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>Gemfile\u306b\u300c<a href=\"https:\/\/github.com\/nsarno\/knock\">knock<\/a>\u300d\u3092\u8ffd\u52a0\u3002<\/p>\n<p><strong>bundle \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">$ bundle install --path vendor\/bundle<\/pre>\n<p>knock\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">$ bin\/rails generate knock:install<\/pre>\n<p>\u4ee5\u4e0b\u304c\u8868\u793a\u3059\u308c\u3070\u6210\u529f\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">create  config\/initializers\/knock.rb<\/pre>\n<p>user\u304c\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3092\u767a\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u300ctoken_controller\u300d\u3092knock\u30b3\u30de\u30f3\u30c9\u3092\u7528\u3044\u3066\u751f\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">$ bin\/rails generate knock:token_controller user<\/pre>\n<p>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3068\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u304c\u751f\u6210\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">create  app\/controllers\/user_token_controller.rb\r\nroute  post 'user_token' =&gt; 'user_token#create'<\/pre>\n<p><strong>app\/controllers\/user_token_controller.rb<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">class UserTokenController &lt; Knock::AuthTokenController\r\nend<\/pre>\n<p>\u3053\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u300cuser_token_controller.rb\u300d\u304c\u751f\u6210\u3055\u308c\u308b\u306e\u3067\u3059\u304c\u3001\u300cApi\/V1\/\u300d\u306b\u3057\u305f\u304b\u3063\u305f\u306e\u3067\u3001\u624b\u52d5\u3067\u6301\u3063\u3066\u3044\u304d\u307e\u3057\u305f\u3002\uff08\u30b3\u30de\u30f3\u30c9\u3067\u751f\u6210\u3057\u305f\u304b\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u3061\u3087\u3063\u3068\u3084\u308a\u65b9\u308f\u304b\u3089\u306a\u304b\u3063\u305f\u3067\u3059\uff09<\/p>\n<p><strong>app\/controllers\/api\/v1\/user_token_controller.rb<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">module Api\r\n  module V1\r\n    class UserTokenController &lt; Knock::AuthTokenController\r\n    end\r\n  end\r\nend<\/pre>\n<p>\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3082\u4ee5\u4e0b\u306e\u69d8\u306b\u4fee\u6b63\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">Rails.application.routes.draw do\r\n  namespace :api do\r\n    namespace :v1 do\r\n      post 'user_token' =&gt; 'user_token#create'\r\n    end\r\n  end\r\nend<\/pre>\n<p>\u300ccontrollers\/application_controller.rb\u300d\u306b\u3001\u300cinclude Knock::Authenticable\u300d\u3092\u8ffd\u8a18\u3057\u3001application_controller\u306b\u3001Knock::Authenticable\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">class ApplicationController &lt; ActionController::API\r\n  include Knock::Authenticable\r\nend<\/pre>\n<p>\u3068\u3001<a href=\"https:\/\/github.com\/webcyou-org\/jwt-server\/commit\/faac31ad668b4e4bd8c5fe30075b0395461a14fb\" target=\"_blank\" rel=\"noopener\">\u3053\u306e\u8fba\u308a\u306e\u30b3\u30df\u30c3\u30c8\u306f\u3053\u3053\u3089\u3078\u3093<\/a>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>Users Controller\u4fee\u6b63<\/h4>\n<p><a class=\"link-gray-dark\" title=\"app\/controllers\/api\/v1\/users_controller.rb\" href=\"https:\/\/github.com\/webcyou-org\/jwt-server\/commit\/54f1cf8bb1cef179fb191c468a3b9644b2d7574c#diff-6a96117366c36e46f72e2f023808047e\">app\/controllers\/api\/v1\/users_controller.rb<\/a> \u3092\u4fee\u6b63\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">before_action :authenticate_user, except: &#x5B;:create, :logout]<\/pre>\n<p>\u3092\u8ffd\u52a0\u3057\u3001User\u30ea\u30bd\u30fc\u30b9\u3092\u4fdd\u8b77\u3057\u307e\u3059\u3002<\/p>\n<p>create\u30e1\u30bd\u30c3\u30c9\u3068logout\u30e1\u30bd\u30c3\u30c9\u306f\u3001except\u3067 \u5bfe\u8c61\u5916\u3068\u3057\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<p>create\u30e1\u30bd\u30c3\u30c9\u306f\u65b0\u898f\u30e6\u30fc\u30b6\u30fc\u4f5c\u6210\u3067\u3059\u304c\u3001\u3053\u3061\u3089\u306e\u8aac\u660e\u306f\u5272\u611b\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002<\/p>\n<p>index\u30e1\u30bd\u30c3\u30c9\u3067\u3001\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u3092\u8fd4\u5374\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">def index\r\n  render json: {status: 200, user: response_fields(current_user.to_json) }\r\nend<\/pre>\n<p>\u307e\u305f\u3001\u30ed\u30b0\u30a2\u30a6\u30c8\u306b\u95a2\u3057\u3066\u306f\u3001JWT\u3067\u306f\u30b5\u30fc\u30d0\u30fc\u5074\u306b\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u3044\u308b\u72b6\u614b\u306a\u3069\u3092\u4fdd\u6301\u3057\u306a\u3044\u8a2d\u8a08\u306a\u306e\u3067\u3001\u30b5\u30af\u30bb\u30b9\u3060\u3051\u8fd4\u5374\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u307e\u3067\u306e\u30b3\u30df\u30c3\u30c8\u304c\u3001<a href=\"https:\/\/github.com\/webcyou-org\/jwt-server\/commit\/54f1cf8bb1cef179fb191c468a3b9644b2d7574c\" target=\"_blank\" rel=\"noopener\">\u3053\u306e\u8fba\u308a<\/a>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>Users\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u4fee\u6b63\u306b\u4f34\u3044\u3001routes\u3082\u4fee\u6b63\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p><strong>config\/routes.rb<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">get 'users' =&gt; 'users#index'\r\npost 'users' =&gt; 'users#create'\r\ndelete 'users' =&gt; 'users#logout<\/pre>\n<p>\u3053\u308c\u307e\u3067\u306e\u30b3\u30df\u30c3\u30c8\u304c\u3001<a href=\"https:\/\/github.com\/webcyou-org\/jwt-server\/commit\/cab0ea595a609a7ae48e750045f9967eff79682a\" target=\"_blank\" rel=\"noopener\">\u3053\u306e\u8fba\u308a<\/a>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>curl\u3067\u306e\u691c\u8a3c<\/h4>\n<p>\u3068\u3001\u3053\u3053\u307e\u3067\u5b9f\u88c5\u3067\u304d\u308c\u3070\u691c\u8a3c\u304c\u884c\u3048\u308b\u3002<\/p>\n<p>\u306f\u305a\u3060\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u4f55\u5ea6\u884c\u3063\u3066\u3082 401\u30a8\u30e9\u30fc\u3068\u306a\u3063\u3066\u3057\u307e\u3046\u3002\u3002\u3002<\/p>\n<p>\u8abf\u3079\u305f\u3068\u3053\u308d\u3001Rails5.2\u4ee5\u964d\u3067\u3001\u3069\u3046\u3084\u3089\u300cRails.application.secrets.secret_key_base\u300d\u306f\u3001nil\u3092\u8fd4\u3059\u3088\u3046\u3067\u3001\u305d\u308c\u304c\u5f71\u97ff\u3057\u3066\u3044\u308b\u307d\u3044\u3002<\/p>\n<p>\u56de\u907f\u7b56\u3068\u3057\u3066\u300cskip_before_action :verify_authenticity_token\u300d\u3092\u300cUserTokenController\u300d\u306b\u8ffd\u52a0\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">module Api\r\n  module V1\r\n    class UserTokenController &lt; Knock::AuthTokenController\r\n      skip_before_action :verify_authenticity_token\r\n    end\r\n  end\r\nend<\/pre>\n<p><strong>knock\u306e\u30a4\u30b7\u30e5\u30fc<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/nsarno\/knock\/issues\/208\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/nsarno\/knock\/issues\/208<\/a><\/p>\n<p>\u3053\u308c\u307e\u3067\u306e\u30b3\u30df\u30c3\u30c8\u304c\u3001<a href=\"https:\/\/github.com\/webcyou-org\/jwt-server\/commit\/a3b7f4c9daad7f3060421103804cf6b0e698551c\" target=\"_blank\" rel=\"noopener\">\u3053\u306e\u8fba\u308a<\/a>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h5>curl Test<\/h5>\n<p><strong>\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u306e\u751f\u6210\uff08\u30e6\u30fc\u30b6\u30fc\u30ed\u30b0\u30a4\u30f3\u6642\uff09<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">$ curl -X \"POST\" \"http:\/\/localhost:3000\/api\/v1\/user_token\" -H \"Content-Type: application\/json\" -d $'{\"auth\": {\"email\": \"test@user.com\", \"password\": \"test123\"}}'<\/pre>\n<p>\u30ec\u30b9\u30dd\u30f3\u30b9\u306f\u3001jwt\u304c\u8fd4\u5374\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">{\"jwt\":\"eyJ0eXAiOiJKV1QiLCJhbG....\"}<\/pre>\n<p>\u4e0a\u8a18\u306eJWT\u3092\u7528\u3044\u3066\u3001\u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u3092\u53d6\u5f97\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">$ curl -X \"GET\" \"http:\/\/localhost:3000\/api\/v1\/users\" -H \"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbG....\" -H \"Content-Type: application\/json\"<\/pre>\n<p>\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u304c\u8fd4\u5374\u3055\u308c\u308b\u306e\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">{\"status\":200,\"user\":{\"id\":1,\"name\":\"daisuke.takayama\",\"email\":\"test@user.com\"}}<\/pre>\n<p>\u3068\u3001\u4eca\u56de\u306e\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u30bd\u30fc\u30b9\u306f\u3053\u3061\u3089\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/webcyou-org\/jwt-server\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/webcyou-org\/jwt-server<\/a><\/p>\n<p><strong>Github<\/strong><\/p>\n<p><strong>\u30fb\u30b5\u30fc\u30d0\u30fc\u5074\uff08Rails5\uff09<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/webcyou-org\/jwt-server\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/webcyou-org\/jwt-server<\/a><\/p>\n<p><strong>\u30fb\u30d5\u30ed\u30f3\u30c8\u5074\uff08Nuxt.js\uff09<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/webcyou-org\/jwt-front\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/webcyou-org\/jwt-front<\/a><\/p>\n<p>\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306eassets\u306f\u300c<a href=\"https:\/\/github.com\/webcyou-org\/jwt-front\/tree\/c68ce9614ce962d6afc54d809097da5de24a6013\" target=\"_blank\" rel=\"noopener\">frontend<\/a>\u300d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3067 submodule\u5316\u3057\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<h5>\u7d42\u308f\u308a\u306b<\/h5>\n<p>\u3068\u3044\u3063\u305f\u8a33\u3067\u3001\u4eca\u56de\u306fJWT\u306e\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u5b9f\u88c5\u3067\u3057\u305f\u3002<\/p>\n<p>\u6642\u9593\u304c\u3067\u304d\u305f\u3089\u3001Rails\u4ee5\u5916\u306b\u3082\u3001SpringBoot\u3068\u304b\u3001Django\u3068\u304b\u3067\u3082\u5b9f\u88c5\u3057\u3066\u307f\u3088\u3046\u304b\u3068\u601d\u3063\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<p>\uff08\u6700\u521d\u306f\u3001SpringBoot\u3067\u3084\u308d\u3046\u304b\u3068\u601d\u3044\u307e\u3057\u305f\u304c\u3001\u74b0\u5883\u69cb\u7bc9\u304cRails\u306e\u65b9\u304c\u697d\u3060\u3063\u305f\u306e\u3067\u3002\u3002\uff09<\/p>\n<p>\u307e\u305f\u3001\u540c\u3058\u304f\u6642\u9593\u304c\u3067\u304d\u305f\u3089\u3001\u30d5\u30ed\u30f3\u30c8\u5074\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3082\u3001Nuxt.js\u4ee5\u5916\u306eSPA\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u7528\u3044\u3066\u3084\u3063\u3066\u307f\u3088\u3046\u304b\u3068\u3002<\/p>\n<p>SPA\u3067\u306eJWT\u8a8d\u8a3c\u8a8d\u53ef\u3092\u4f5c\u6210\u3057\u3088\u3046\u3068\u3057\u3066\u3044\u308b\u65b9\u306e\u4f55\u304b\u306e\u53c2\u8003\u306b\u3067\u3082\u306a\u308c\u3070\u3068\u601d\u3063\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u3041\u3002<\/p>\n<p>\u307e\u305f\u307e\u305f\u3041\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9470\" src=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/jwt_fw.png\" alt=\"\" width=\"471\" height=\"143\" srcset=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/jwt_fw.png 471w, https:\/\/www.webcyou.com\/wp-content\/uploads\/2019\/03\/jwt_fw-300x91.png 300w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3069\u3082\u3067\u3059\u3002 SPA\u3067\u30b5\u30fc\u30d3\u30b9\u3092\u30ac\u30f3\u30ac\u30f3\u4f5c\u6210\u3055\u308c\u3066\u3044\u308b\u3067\u3057\u3087\u3046\u304b\uff1f SPA\u306e\u8a8d\u8a3c\u8a8d\u53ef\u306e\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3068\u3057\u3066\u3001\u300cJWT\uff08JSON Web Tokens\uff09\u300d\u3092\u7528\u3044\u308b\u65b9\u3082\u591a\u3044\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\uff1f \u524d\u56de\u3001\u300cNuxt.js \u3068  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[177,168,176,166,37,47,46,58,150,44,45,178,78,114,118],"class_list":{"0":"post-9464","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-javascript","7":"tag-css3","8":"tag-html","9":"tag-html5","10":"tag-iphone","11":"tag-iphone-","12":"tag-javascript-2","13":"tag-js","14":"tag-sdk","15":"tag-vagrant","16":"tag-web","18":"tag-178","19":"tag-78","20":"tag-114","21":"tag-118"},"_links":{"self":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/9464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9464"}],"version-history":[{"count":5,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/9464\/revisions"}],"predecessor-version":[{"id":9474,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/9464\/revisions\/9474"}],"wp:attachment":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}