{"id":3786,"date":"2022-09-10T17:32:08","date_gmt":"2022-09-10T08:32:08","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=3786"},"modified":"2022-09-11T04:47:21","modified_gmt":"2022-09-10T19:47:21","slug":"nextauth-js-getting-started-add-api-route","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=3786","title":{"rendered":"<small>NextAuth.js : Getting Started &#8211; Add API route<\/small>"},"content":{"rendered":"<p>1. Reference<br \/>\n<a href=\"https:\/\/next-auth.js.org\/getting-started\/example#add-api-route\">https:\/\/next-auth.js.org\/getting-started\/example#add-api-route<\/a><\/p>\n<p>2. Add API route<\/p>\n<pre>\r\nimport NextAuth from \"next-auth\"\r\nimport GithubProvider from \"next-auth\/providers\/github\"\r\nexport const authOptions = {\r\n  \/\/ Configure one or more authentication providers\r\n  providers: [\r\n    GithubProvider({\r\n      clientId: process.env.GITHUB_ID,\r\n      clientSecret: process.env.GITHUB_SECRET,\r\n    }),\r\n    \/\/ ...add more providers here\r\n  ],\r\n}\r\nexport default NextAuth(authOptions)\r\n<\/pre>\n<p>3. \u30cf\u30de\u308b\u5834\u6240<br \/>\n\u4ee5\u4e0b\u306e2\u3064\u306fGITHUB\u5185,NextAuth.js\u5185\u3067\u691c\u7d22\u3057\u3066\u3082\u51fa\u3066\u3053\u306a\u3044<br \/>\n&#8211; GITHUB_ID<br \/>\n&#8211; GITHUB_SECRET<\/p>\n<p>yahoo \u77e5\u6075\u888b\u3067github ID\u3063\u3066\u3069\u308c\u306e\u3053\u3068\u3067\u3059\u304b\uff1f<br \/>\n\u30d9\u30b9\u30c8\u30a2\u30f3\u30b5\u30fc\uff1a<br \/>\n\u30a2\u30ab\u30a6\u30f3\u30c8\u306f\u4f5c\u6210\u6e08\u307f\u3067\u3059\u304b\uff1fgithub\u306e\u30a2\u30ab\u30a6\u30f3\u30c8\u767b\u9332\u6642\u306b\u8a2d\u5b9a\u3057\u305fID\u3067\u3059\u3002<\/p>\n<p>4. OAuth \u306e\u8aac\u660e\u306e\u6700\u5f8c\u306e\u65b9\u306b\u30d2\u30f3\u30c8\u304c\u3042\u308b<br \/>\n&#8211; Home > Configuration > Providers > OAuth<br \/>\n<a href=\"https:\/\/next-auth.js.org\/configuration\/providers\/oauth#override-default-options\">https:\/\/next-auth.js.org\/configuration\/providers\/oauth#override-default-options<\/a><br \/>\n&#8211; clientId: process.env.GOOGLE_CLIENT_ID,<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000^^^^^^^^<br \/>\n&#8211; clientSecret: process.env.GOOGLE_CLIENT_SECRET,<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000^^^^^^^^<\/p>\n<pre>\r\n$ cat \/api\/auth\/[...nextauth].js\r\nimport GoogleProvider from \"next-auth\/providers\/google\"\r\n\r\nGoogleProvider({\r\n  clientId: process.env.GOOGLE_CLIENT_ID,\r\n  clientSecret: process.env.GOOGLE_CLIENT_SECRET,\r\n\r\n  profile(profile) {\r\n    return {\r\n      \/\/ Return all the profile information you need.\r\n      \/\/ The only truly required field is `id`\r\n      \/\/ to be able identify the account when added to a database\r\n    }\r\n  },\r\n})\r\n<\/pre>\n<p>5. In short<br \/>\nGITHUB_ID -> GITHUB \u306e \u2018client id\u2019<br \/>\nGITGUB_SECRET -> GITHUB \u306e \u2018client secret\u2019<\/p>\n<p>6. Create \u2018client id\u2019 and \u2018client secret\u2019<\/p>\n<pre>\r\nGITHUB\r\n  |- Settings \r\n        |- Developers settings - OAuth Apps - [New OAuth App]\r\n<\/pre>\n<p>Register a new OAuth application<br \/>\nhttps:\/\/github.com\/settings\/applications\/new<\/p>\n<p>7. \u5143\u306eAdd API route \u306ecode\u306f\u3069\u3046\u3067\u3042\u308c\u3070\u3001\u8ff7\u308f\u306a\u304b\u3063\u305f\u304b\uff1f<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n\u5143\n<\/td>\n<td>\n\u8981\u3000\u5909\u66f4\n<\/td>\n<\/tr>\n<tr>\n<td>\n    GithubProvider({<br \/>\n     &nbsp; clientId: process.env.GITHUB_ID,<br \/>\n     &nbsp; clientSecret: process.env.GITHUB_SECRET,<br \/>\n    }),\n<\/td>\n<td>\n    GithubProvider({<br \/>\n     &nbsp; clientId: process.env.GITHUB_CLIENT_ID,<br \/>\n     &nbsp; clientSecret: process.env.GITHUB_CLIENT_SECRET,<br \/>\n    }),\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>1. Reference https:\/\/next-auth.js.org\/getting-started\/example#add-api-route 2. Add API route import NextAuth f&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/3786"}],"collection":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3786"}],"version-history":[{"count":45,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/3786\/revisions"}],"predecessor-version":[{"id":3834,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/3786\/revisions\/3834"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}