Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Sessionを拡張してgithubのアクセストークンを取得 #6

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading