Skip to content

Commit

Permalink
feat: Sessionを拡張してgithubのアクセストークンを取得
Browse files Browse the repository at this point in the history
  • Loading branch information
namidapoo committed Dec 26, 2024
1 parent dbc2d1c commit cfddd95
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default async function Home() {
const session = await auth();
if (!session?.user) return null;

console.log(session);

return (
<div className="flex min-h-dvh items-center justify-center bg-gradient-to-b from-pink-300 to-blue-300 p-4">
<div className="w-full max-w-md space-y-8 text-center">
Expand Down
21 changes: 19 additions & 2 deletions lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@ import GitHub from "next-auth/providers/github";
import "next-auth/jwt";

export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [GitHub],
providers: [
GitHub({
authorization: {
param: {
scope: "repo read:user",
},
},
}),
],
callbacks: {
async jwt({ token, profile }) {
async jwt({ token, profile, account }) {
if (account) {
token.accessToken = account.access_token;
}
if (profile) {
const { login } = profile;
token.user = { ...token.user, login };
Expand All @@ -15,6 +26,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
async session({ session, token }) {
const { login } = token.user;
session.user = { ...session.user, login };
session.accessToken = token.accessToken;
return session;
},
},
Expand All @@ -24,18 +36,23 @@ declare module "next-auth" {
user: {
login: string;
} & DefaultSession["user"];
accessToken: string;
}
interface User {
login: string;
}
interface Profile {
login: string;
}
interface Account {
access_token: string;
}
}
declare module "next-auth/jwt" {
interface JWT {
user: {
login: string;
};
accessToken: string;
}
}

0 comments on commit cfddd95

Please sign in to comment.