Skip to content

Commit

Permalink
๐Ÿ› nextauth route export๋กœ ์ˆ˜์ •, ํƒ€์ž… ์ˆ˜์ • ๋ฐ ์ง€์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
529539 committed May 29, 2024
1 parent 9176320 commit b2232cc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
29 changes: 20 additions & 9 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import NextAuth from "next-auth";
import GithubProvier from "next-auth/providers/github";
import { postLogin } from "@service/api/auth";
import type { JWT } from "next-auth/jwt";
import type { Session } from "next-auth";

const handler = NextAuth({
export const authOptions = {
pages: {
signIn: "/login",
},
Expand All @@ -11,11 +13,16 @@ const handler = NextAuth({
clientId: process.env.OAUTH_GITHUB_CLIENT_ID as string,
clientSecret: process.env.OAUTH_GITHUB_CLIENT_SECRET as string,
async profile(profile) {
const { id, name, avatar_url } = profile;
const { data } = await postLogin({ id, name, avatar_url });
const { id, name, avatar_url, login } = profile;
const { data } = await postLogin({
id,
name: name as string,
avatar_url,
});
return {
...profile,
id,
userId: login,
name,
image: avatar_url,
accessToken: data.data.accessToken,
Expand All @@ -24,16 +31,20 @@ const handler = NextAuth({
}),
],
callbacks: {
async jwt({ token, profile, user }) {
return { ...token, ...profile, ...user };
async jwt({ token, user }: { token: JWT; user: any }) {
return { ...token, ...user } as JWT;
},
async session({ session, token }) {
if (session.user && token.accessToken) {
async session({ session, token }: { session: Session; token: JWT }) {
if (session.user && token) {
session.user.id = token.id as number;
session.user.userId = token.userId as string;
session.user.accessToken = token.accessToken as string;
}
return session;
return { ...session };
},
},
});
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };
18 changes: 14 additions & 4 deletions src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import NextAuth, { type DefaultSession } from "next-auth";
import NextAuth, {
type DefaultSession,
type DefaultProfile,
type DefaultUser,
} from "next-auth";
import type { GithubProfile } from "next-auth/providers/github";

declare module "next-auth" {
interface Session {
user: {
id: number;
userId: string;
name: string;
image: string;
email: string;
accessToken: string;
} & DefaultSession["user"];
}
interface Profile extends DefaultProfile {
interface User extends DefaultUser {
id: number;
avatar_url: string;
accessToken: string;
}
}

import { JWT } from "@auth/core/jwt";

declare module "@auth/core/jwt" {
interface JWT {
id: number;
userId: string;
accessToken: string;
}
}

0 comments on commit b2232cc

Please sign in to comment.