{"id":3750,"date":"2022-09-10T15:25:05","date_gmt":"2022-09-10T06:25:05","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=3750"},"modified":"2022-09-11T05:15:27","modified_gmt":"2022-09-10T20:15:27","slug":"nextauth-js-backend-api-route","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=3750","title":{"rendered":"<small>NextAuth.js: unstable_getServerSession<\/small>"},"content":{"rendered":"<p>1. reference<br \/>\n<strong> Backend &#8211; API Route <\/strong><br \/>\nTo protect an API Route, you can use the unstable_getServerSession() method.<br \/>\n<a href=\"https:\/\/next-auth.js.org\/getting-started\/example#backend---api-route\">https:\/\/next-auth.js.org\/getting-started\/example#backend&#8212;api-route<\/a><\/p>\n<pre>\r\nimport { unstable_getServerSession } from \"next-auth\/next\"\r\nimport { authOptions } from \".\/auth\/[...nextauth]\"\r\nexport default async (req, res) => {\r\n  const session = await unstable_getServerSession(req, res, authOptions)\r\n  if (session) {\r\n    res.send({\r\n      content:\r\n        \"This is protected content. You can access this content because you are signed in.\",\r\n    })\r\n  } else {\r\n    res.send({\r\n      error: \"You must be signed in to view the protected content on this page.\",\r\n    })\r\n  }\r\n}\r\n<\/pre>\n<p>2. \u305d\u306e\u307e\u307e\u3067\u306f\u52d5\u304b\u306a\u3044<\/p>\n<p>3. point<\/p>\n<pre>\r\n import { authOptions } from \".\/auth\/[...nextauth]\"\r\n        ^^^^^^^^^^^^^^^\r\n<\/pre>\n<p>4. Change code at &#8216;pages\/api\/auth\/[&#8230;nextauth]&#8217;<br \/>\n&#8211; export const authOptions: NextAuthOptions = {<br \/>\n&#8211; export default NextAuth(authOptions);<\/p>\n<pre>\r\nimport { NextAuth } from 'next-auth'\r\nimport type { NextAuthOptions } from 'next-auth'\r\n\r\nexport const authOptions: NextAuthOptions = {\r\n  providers: [\r\n    GithubProvider({\r\n      clientId: process.env.GITHUB_ID,\r\n      clientSecret: process.env.GITHUB_SECRET,\r\n    }),\r\n  ],\r\n  callbacks: {\r\n    async signIn({ user, account, profile, email, credentials }) {\r\n      return true\r\n    },\r\n    async redirect({ url, baseUrl }) {\r\n      return baseUrl\r\n    },\r\n    async jwt({ token, account }) {\r\n      \/\/ Persist the OAuth access_token to the token right after signin\r\n      if (account) {\r\n        token.accessToken = account.access_token\r\n      }\r\n      return token\r\n    },\r\n    async session({ session, token, user }) {\r\n      \/\/ Send properties to the client, like an access_token from a provider.\r\n      session.accessToken = token.accessToken\r\n      return session\r\n    }\r\n  }\r\n}\r\n\r\nexport default NextAuth(authOptions);\r\n<\/pre>\n<p>5. How to use with server code<br \/>\n&#8211; import<\/p>\n<pre>\r\nimport { unstable_getServerSession } from \"next-auth\/next\"\r\nimport { authOptions } from \".\/auth\/[...nextauth]\"\r\n<\/pre>\n<p>&#8211; in function<\/p>\n<pre>\r\nconst session = await unstable_getServerSession(req, res, authOptions)\r\nconsole.log(session)\r\n<\/pre>\n<p>&#8211; console log<\/p>\n<pre>\r\n{\r\n  user: {\r\n    name: 'keybord cat',\r\n    email: 'k-cat@hogehoge.io',\r\n    image: 'https:\/\/avatars.githubusercontent.com\/u\/135275922?v=4'\r\n  },\r\n  expires: '2022-10-10T06:22:24.952Z',\r\n  accessToken: 'gho_kJ4Gl5uhnnPYuufMbmTWJQszhKcSBq0Lat22'\r\n}\r\n<\/pre>\n<p>6. unstable_getServerSession ( google \u7ffb\u8a33 )<br \/>\n<span><br \/>\n\u30b5\u30fc\u30d0\u30fc\u5074\u3001\u3064\u307e\u308a API \u30eb\u30fc\u30c8\u307e\u305f\u306f getServerSideProps \u304b\u3089\u547c\u3073\u51fa\u3059\u5834\u5408\u306f\u3001getSession \u306e\u4ee3\u308f\u308a\u306b\u3053\u306e\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30bb\u30c3\u30b7\u30e7\u30f3 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002 \u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067 NextAuth.js \u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u5834\u5408\u306b\u7279\u306b\u4fbf\u5229\u3067\u3059\u3002 \u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092 getSession \u30b5\u30fc\u30d0\u30fc\u5074\u3067\u4f7f\u7528\u3059\u308b\u3068\u3001API \u30eb\u30fc\u30c8\u3078\u306e\u4f59\u5206\u306a\u30d5\u30a7\u30c3\u30c1\u304c\u56de\u907f\u3055\u308c\u308b\u305f\u3081\u3001\u5fdc\u7b54\u6642\u9593\u304c\u5927\u5e45\u306b\u77ed\u7e2e\u3055\u308c\u307e\u3059 (\u3053\u308c\u306f\u901a\u5e38\u3001Next.js \u3067\u306f\u63a8\u5968\u3055\u308c\u307e\u305b\u3093)\u3002 \u3055\u3089\u306b\u3001unstable_getServerSession \u306f Cookie \u306e\u6709\u52b9\u671f\u9650\u3092\u6b63\u3057\u304f\u66f4\u65b0\u3057\u3001callbacks.jwt \u307e\u305f\u306f callbacks.session \u304c\u4f55\u304b\u3092\u5909\u66f4\u3057\u305f\u5834\u5408\u306b\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u5185\u5bb9\u3092\u66f4\u65b0\u3057\u307e\u3059\u3002<br \/>\n<\/span><\/p>\n<p>7. WARRNING<br \/>\nThis feature is experimental and may be removed or changed in the future.<br \/>\n\u3068\u3042\u308b\u304c,\u4fbf\u5229\u306a\u306e\u3067\u6b8b\u3057\u3066\u307b\u3057\u3044<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. reference Backend &#8211; API Route To protect an API Route, you can use the unstable_getServerSession() me&#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\/3750"}],"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=3750"}],"version-history":[{"count":25,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/3750\/revisions"}],"predecessor-version":[{"id":3766,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/3750\/revisions\/3766"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}