Closed19

next-authを触ってみる

yuto-myuto-m

page routerぽいけど一応公式ドキュメントを読んだ。
そんなに難しくなさそう。
app routerのドキュメント欲しい。
公式見た感じ簡単だからpage routerでやろうかな。

yuto-myuto-m

page routerでやる。
とりまnpx create-next-app . --ts --use-npm testでnextのテンプレをpage routerで立てる。

yuto-myuto-m

簡単すぎワロた。

OAuth完了すると謎のバスが出る

yuto-myuto-m

でもこれってgithubのapiとかidとかどうやって取得するんやろか。
getServerSessionで取得できるsession情報を見ても下記しかないからあんまり意味なくないかっていう。

callbackで取得するぽい。

{
  user: {
    name: 'Yuto Miyakawa',
    email: undefined,
    image: 'https://avatars.githubusercontent.com/u/43704306?v=4'
  },
  expires: '2023-12-21T12:49:56.559Z'
}
yuto-myuto-m

取得したaccessTokenで下記のライブラリ初期化したらgithubのapi叩けるとこまでできた。
あとは取得したデータ使って何するかっていう感じだからもう終わりで良いかなっていう。
https://github.com/octokit/core.js#readme

yuto-myuto-m

sessionの扱いは2パターンあってjwtがデフォルトで、jwtはHttpOnlyかつサーバー側にのみあるkeyでencryptされてるから仮にjwtが盗まれてもdecryptされないので安全て書いてあった。

https://authjs.dev/concepts/session-strategies#jwt

Auth.js libraries can create sessions using JSON Web Tokens (JWT). This is the default session strategy for Auth.js libraries. When a user signs in, a JWT is created in a HttpOnly cookie. Making the cookie HttpOnly prevents JavaScript from accessing it client-side (document.cookie), which makes it harder for attackers to steal the value. In addition, the JWT is encrypted with a secret key only known to the server. So even if an attacker were to steal the JWT from the cookie, they would not be able to decrypt it. Combined with a short expiration time, this makes JWTs a secure way to create sessions.

とはいえ、encryptedされたjwtトークンの内容が絶対にdecrpytされるという仮定はしない方が良いとも書いてあるのでやはりフロントに機密情報(ex. アクセストークン)を渡すのはちょっとリスク高い。

Even if appropriately configured, information stored in an encrypted JWT should not be assumed to be impossible to decrypt at some point - e.g. due to the discovery of a defect or advances in technology. Data stored in an encrypted JSON Web Token (JWE) may be compromised at some point. The recommendation is to generate a secret with high entropy.

ちなみにDBにsession idを保存するパターンもあるが、パフォーマンスを考慮するとJWTを使いつつJWTに秘密情報を含めない運用の方が楽そうではある。
ただ、JWTで実現が難しいログイン制限やログアウトしたら全ての端末からログアウトするみたいな機能を作りたいならDBの方が良いとのこと。

yuto-myuto-m

next jsのapi routeでendpoint増やす方法
https://nextjs.org/docs/pages/building-your-application/routing/api-routes

getToken()はサーバーサイドのみで動く。
https://next-auth.js.org/tutorials/securing-pages-and-api-routes#using-gettoken

JWT callbackでtokenにアクセストークンを含めつつ、session callbackではアクセストークンを含めなければクライアント側でアクセストークンが触れる心配はなくなりそう。
https://next-auth.js.org/configuration/callbacks#jwt-callback

安全のためにデフォルトだとtokenの一部しかフロントで扱えなくなってる
https://next-auth.js.org/configuration/callbacks#session-callback

The session callback is called whenever a session is checked. By default, only a subset of the token is returned for increased security. If you want to make something available you added to the token (like access_token and user.id from above) via the jwt() callback, you have to explicitly forward it here to make it available to the client.

yuto-myuto-m

jwtてどのユーザーが、どのjwtに紐づいているかってどうやって分かるんだろうか。
sessionならsession idだけど、それぽいのがついてるんだろうか。

yuto-myuto-m

sendgridのアカウントがなぜか作れなくて死亡した。
company nameとかをnoneにしたから?

別のgmailアカウントでアカウント作ってみたので承認されるまで待ち。

このスクラップは5ヶ月前にクローズされました