﻿
{"id":9419,"date":"2018-09-16T22:44:58","date_gmt":"2018-09-16T22:44:58","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=9419"},"modified":"2018-09-16T22:44:58","modified_gmt":"2018-09-16T22:44:58","slug":"node-js-jwt-token-kullanimi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/node-js-jwt-token-kullanimi\/","title":{"rendered":"Node.js &#8211; JWT Token Kullan\u0131m\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bir \u00f6nceki <a href=\"http:\/\/www.gencayyildiz.com\/blog\/node-js-jwt-ile-token-olusturma\/\" rel=\"noopener\" target=\"_blank\">Node.js \u2013 JWT \u0130le Token Olu\u015fturma<\/a> ba\u015fl\u0131kl\u0131 makalemde Node.js&#8217;de token nas\u0131l olu\u015fturulur incelemi\u015ftik. Bu i\u00e7eri\u011fimizde ise olu\u015fturulan bu token ile API&#8217;a eri\u015fim i\u015flemlerinin nas\u0131l yap\u0131laca\u011f\u0131n\u0131 inceleyece\u011fiz.<\/p>\n<p>Her\u015feyden \u00f6nce olu\u015fturulan bir token\u0131 API&#8217;da kullanabilmek i\u00e7in bir <a href=\"http:\/\/www.gencayyildiz.com\/blog\/express-js-middlewareara-katman-nedir-nasil-kullanilir\/\" rel=\"noopener\" target=\"_blank\">Middleware<\/a> olu\u015fturulmas\u0131 gerekti\u011fini ifade ederek ba\u015flayal\u0131m. Bu middleware \u00fczerinden herhangi bir get ya da post iste\u011fine kar\u015f\u0131l\u0131k \u00fcretilen bir token\u0131n olup olmad\u0131\u011f\u0131 denetlenecek ve bu denetleme neticesine g\u00f6re ilgili rotalar tetiklenecektir.<\/p>\n<h3>Token Olu\u015fturma ve Altyap\u0131 Kurulumu<\/h3>\n<p>\u0130lk olarak a\u015fa\u011f\u0131daki gibi token \u00fcreten mod\u00fcl\u00fcm\u00fcz\u00fc in\u015fa edelim;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n--- tokenRouter.js ---\r\nconst router = require(&quot;express&quot;).Router();\r\nconst jwt = require(&quot;jsonwebtoken&quot;);\r\n\r\nrouter.post(&quot;\/getToken&quot;, (request, response, next) =&gt; {\r\n    const { userName, password } = request.body;\r\n    const payLoad = {\r\n        userName,\r\n        password,\r\n        email: &quot;gyildizmail@gmail.com&quot;\r\n    };\r\n    const token = jwt.sign(payLoad, request.app.get(&quot;api_secret_key&quot;), { expiresIn: 120 });\r\n    response.json({\r\n        status: true,\r\n        token\r\n    });\r\n});\r\n\r\nmodule.exports = router;\r\n<\/pre>\n<p>Ard\u0131ndan ana mod\u00fcl\u00fcm\u00fcz\u00fc a\u015fa\u011f\u0131daki gibi olu\u015ftural\u0131m;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst app = require(&quot;express&quot;)();\r\nconst bodyParser = require(&quot;body-parser&quot;);\r\n\r\napp.set(&quot;api_secret_key&quot;, require(&quot;.\/config&quot;).api_secret_key);\r\napp.use(bodyParser.json());\r\napp.use(bodyParser.urlencoded({ extended: true }));\r\n\r\napp.use(&quot;\/token&quot;, require(&quot;.\/routers\/tokenRouter&quot;));\r\n\r\napp.listen(5000, () =&gt; console.log(&quot;Yay\u0131n ba\u015flad\u0131...&quot;));\r\n<\/pre>\n<p>Burada global olarak tan\u0131mlanan &#8220;api_secret_key&#8221; de\u011fi\u015fkeninde kullan\u0131lan &#8220;config&#8221; mod\u00fcl\u00fcn\u00fcde a\u015fa\u011f\u0131daki gibi olu\u015ftural\u0131m;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nmodule.exports = {\r\n    api_secret_key: &quot;\u00f6l\u00fcme gidelim dedinde, mazotmu yok dedik.&quot;\r\n};\r\n<\/pre>\n<p>Evet&#8230; Bu noktada &#8220;http:\/\/localhost:5000\/token\/getToken&#8221; adresine a\u015fa\u011f\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde oldu\u011fu gibi post talebi g\u00f6nderdi\u011fimiz vakit s\u0131k\u0131nt\u0131s\u0131z token de\u011ferimiz olu\u015fturulmaktad\u0131r.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131.png\" alt=\"Node.js - JWT Token Kullan\u0131m\u0131\" width=\"1302\" height=\"415\" class=\"aligncenter size-full wp-image-9429\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131.png 1302w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-300x96.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-768x245.png 768w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-1024x326.png 1024w\" sizes=\"auto, (max-width: 1302px) 100vw, 1302px\" \/><\/a><\/p>\n<p>\u015eimdi olu\u015fturulan bu token de\u011ferini API&#8217;ye \u00f6zel eri\u015fim i\u015flemleri i\u00e7in kullanmaya s\u0131ra geldi.<\/p>\n<h3>\u00dcretilmi\u015f Token&#8217;\u0131n Kullan\u0131m\u0131<\/h3>\n<p>API&#8217;ye yap\u0131lacak taleplerde token\u0131n \u00fcretilip \u00fcretilmedi\u011fini kontrol edece\u011fimiz middleware&#8217;\u0131 olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n--- verify-token.js ---\r\nconst jwt = require(&quot;jsonwebtoken&quot;);\r\n\r\nmodule.exports = (request, response, next) =&gt; {\r\n    const token = request.headers&#x5B;&quot;x-access-token&quot;] || request.body.token || request.query.token;\r\n    if (!token)\r\n        response.send(&quot;Token bulunmamaktad\u0131r.&quot;);\r\n    else {\r\n        jwt.verify(token, request.app.get(&quot;api_secret_key&quot;), (error, decoded) =&gt; {\r\n            if (error)\r\n                response.send(&quot;Beklenmeyen bir hatayla kar\u015f\u0131la\u015f\u0131ld\u0131.&quot;);\r\n            else {\r\n                request.decode = decoded;\r\n                next();\r\n            }\r\n        });\r\n    }\r\n};\r\n<\/pre>\n<p>Yukar\u0131daki middleware i\u00e7erisindeki kodlar\u0131 incelerseniz e\u011fer 5. sat\u0131rda &#8220;token&#8221; de\u011fi\u015fkenine birden fazla alandan de\u011fer set edilmektedir. Bunun sebebi bizlerin \u00fcretilen tokena requestten \u00fc\u00e7 farkl\u0131 yolla eri\u015febilmemizden kaynaklanmas\u0131d\u0131r. Bu yollar;<\/p>\n<ol>\n<li><strong>Header<\/strong><br \/>\n<em>request.headers[&#8220;x-access-token&#8221;]<\/em>\n<\/li>\n<li><strong>Post Body<\/strong><br \/>\n<em>request.body.token<\/em>\n<\/li>\n<li><strong>Get Query<\/strong><br \/>\n<em>request.query.token<\/em>\n<\/li>\n<\/ol>\n<p>Dolay\u0131s\u0131yla \u00f6nceden \u00fcretilmi\u015f token, request taraf\u0131ndan bu \u00fc\u00e7 farkl\u0131 yolla eri\u015filebilir olaca\u011f\u0131ndan dolay\u0131 ilgili ifadede t\u00fcm alanlar denetlenmekte ve hangisinde veri varsa ilgili de\u011fi\u015fken taraf\u0131ndan elde edilmektedir.<\/p>\n<p>Ayr\u0131ca kodun devam\u0131nda &#8220;jsonwebtoken&#8221; mod\u00fcl\u00fcn\u00fcn &#8220;verify&#8221; fonksiyonu arac\u0131l\u0131\u011f\u0131yla elde edilen token, secret key ile birlikte denetlenmekte ve 3. parametrede yaz\u0131lan callback fonksiyonunun &#8220;decoded&#8221; parametresi arac\u0131l\u0131\u011f\u0131yla \u00e7\u00f6z\u00fcmlenen token requestin &#8220;decode&#8221; \u00f6zelli\u011fine atanmaktad\u0131r ve next fonksiyonu ile talepte bulunulan routea y\u00f6nlendirme sa\u011flanmaktad\u0131r. Bu \u00e7\u00f6z\u00fcm neticesinde &#8220;decoded&#8221; parametresinden asl\u0131nda token olu\u015fum s\u00fcrecinde belli ba\u015fl\u0131 verilerimizi temsil eden &#8220;Payload&#8221; nesnesi elde edilecektir.<\/p>\n<p>\u015eimdi olu\u015fturdu\u011fumuz bu middleware&#8217;\u0131 ana mod\u00fcl\u00fcm\u00fczde &#8220;use&#8221; metodu ile uygulamaya katmal\u0131y\u0131z.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst app = require(&quot;express&quot;)();\r\nconst bodyParser = require(&quot;body-parser&quot;);\r\n\r\n\r\napp.set(&quot;api_secret_key&quot;, require(&quot;.\/config&quot;).api_secret_key);\r\napp.use(bodyParser.json());\r\napp.use(bodyParser.urlencoded({ extended: true }));\r\n\r\napp.use(&quot;\/employee&quot;, require(&quot;.\/middleWares\/verify-token&quot;));\r\napp.use(&quot;\/employee&quot;, require(&quot;.\/routers\/employeeRouter&quot;));\r\napp.use(&quot;\/token&quot;, require(&quot;.\/routers\/tokenRouter&quot;));\r\n\r\napp.listen(5000, () =&gt; console.log(&quot;Yay\u0131n ba\u015flad\u0131...&quot;));\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funa dikkat ederseniz e\u011fer yazm\u0131\u015f oldu\u011fumuz &#8220;verify-token&#8221; isimli middleware &#8220;\/employee&#8221; de\u011feri alt\u0131ndaki routelar i\u00e7in token kontrol mekanizmas\u0131 sa\u011flayacakt\u0131r. Dolay\u0131s\u0131yla &#8220;employeeRouter&#8221; mod\u00fcl\u00fcm\u00fcz\u00fcde a\u015fa\u011f\u0131daki gibi in\u015fa edelim;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n--- employeeRouter.js ---\r\nconst router = require(&quot;express&quot;).Router();\r\n\r\nrouter.get(&quot;\/get&quot;, (request, response, next) =&gt; {\r\n    response.send(&quot;...&quot;);\r\n});\r\n\r\nrouter.post(&quot;\/post&quot;, (request, response, next) =&gt; {\r\n    response.send(&quot;...&quot;);\r\n});\r\nmodule.exports = router;\r\n<\/pre>\n<h3>Test Edelim<\/h3>\n<p>Evet&#8230; Art\u0131k t\u00fcm \u00e7al\u0131\u015fmalar\u0131m\u0131z bitmi\u015f bulunmaktad\u0131r. \u015eimdi Postman uygulamas\u0131yla testlerimizi ger\u00e7ekle\u015fitirelim;<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-1.png\" alt=\"Node.js - JWT Token Kullan\u0131m\u0131\" width=\"325\" height=\"256\" class=\"aligncenter size-full wp-image-9446\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-1.png 325w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-1-300x236.png 300w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><\/a><br \/>\nG\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi direkt olarak &#8220;\/employee\/get&#8221; adresine talepte bulundu\u011fumuz zaman token ile ilgili uyar\u0131yla kar\u015f\u0131la\u015fmaktay\u0131z. \u015eimdi \u00fc\u00e7 farkl\u0131 y\u00f6ntemle token\u0131m\u0131z\u0131 g\u00f6nderelim.<span style=\"font-size:9px;\">(Tabi ilk olarak &#8220;http:\/\/localhost:5000\/token\/getToken&#8221; adresinden token \u00fcretmeyi unutmuyoruz)<\/span><\/p>\n<ul>\n<li><strong>Header<\/strong><br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-2.png\" alt=\"Node.js - JWT Token Kullan\u0131m\u0131\" width=\"340\" height=\"292\" class=\"aligncenter size-full wp-image-9448\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-2.png 340w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-2-300x258.png 300w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a>\n<\/li>\n<li><strong>Post Body<\/strong><br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-3.png\" alt=\"Node.js - JWT Token Kullan\u0131m\u0131\" width=\"340\" height=\"332\" class=\"aligncenter size-full wp-image-9451\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-3.png 340w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-3-300x293.png 300w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a>\n<\/li>\n<li><strong>Get Query<\/strong><br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-4.png\" alt=\"Node.js - JWT Token Kullan\u0131m\u0131\" width=\"681\" height=\"355\" class=\"aligncenter size-full wp-image-9453\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-4.png 681w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-JWT-Token-Kullan\u0131m\u0131-4-300x156.png 300w\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" \/><\/a>\n<\/li>\n<\/ul>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bir \u00f6nceki Node.js \u2013 JWT \u0130le Token Olu\u015fturma ba\u015fl\u0131kl\u0131 makalemde Node.js&#8217;de token nas\u0131l olu\u015fturulur incelemi\u015ftik. Bu i\u00e7eri\u011fimizde ise olu\u015fturulan bu token ile API&#8217;a eri\u015fim i\u015flemlerinin nas\u0131l yap\u0131laca\u011f\u0131n\u0131 inceleyece\u011fiz. Her\u015feyden \u00f6nce olu\u015fturulan bir token\u0131&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":8600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2145],"tags":[2629,2628,2626,2627],"class_list":["post-9419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","tag-jwt-kullanimi","tag-jwt-token-kullanimi","tag-node-js-token-kullanimi","tag-token-kullanimi"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=9419"}],"version-history":[{"count":31,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9419\/revisions"}],"predecessor-version":[{"id":9455,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9419\/revisions\/9455"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/8600"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=9419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=9419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=9419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}