From b69cc453e88b1c0fb98049125a990cada2aa86a3 Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Wed, 11 Dec 2024 19:24:37 +0800 Subject: [PATCH] feat(auth): extend NextAuth user and session to include language support refactor(gitea): make username optional in GiteaProfile interface style(eslint): add warning rule for empty object type in ESLint configuration --- .eslintrc.json | 5 ++++- src/auth.ts | 26 ++++++++++++++++++++++++-- src/lib/providers/gitea.ts | 11 ++++++----- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 3722418..e878823 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,3 +1,6 @@ { - "extends": ["next/core-web-vitals", "next/typescript"] + "extends": ["next/core-web-vitals", "next/typescript"], + "rules": { + "@typescript-eslint/no-empty-object-type": "warn" + } } diff --git a/src/auth.ts b/src/auth.ts index 21d7603..8fef9af 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,5 +1,15 @@ -import NextAuth from "next-auth"; -import Gitea from "@/lib/providers/gitea"; +import NextAuth, { type DefaultSession } from "next-auth"; +import Gitea, { GiteaProfile } from "@/lib/providers/gitea"; + +declare module "next-auth" { + interface User extends GiteaProfile {} + + interface Session { + user: { + language: string; + } & DefaultSession["user"]; + } +} export const { handlers, signIn, signOut, auth } = NextAuth({ providers: [ @@ -12,4 +22,16 @@ export const { handlers, signIn, signOut, auth } = NextAuth({ }), ], secret: process.env.AUTH_SECRET, + callbacks: { + jwt({ token, user }) { + if (user) { + token.language = user.language; + } + return token; + }, + session({ session, token }) { + session.user.language = token.language as string; + return session; + }, + }, }); diff --git a/src/lib/providers/gitea.ts b/src/lib/providers/gitea.ts index 28a07d1..e9adc4c 100644 --- a/src/lib/providers/gitea.ts +++ b/src/lib/providers/gitea.ts @@ -2,7 +2,7 @@ import { Email as GiteaEmail, User } from "gitea-js"; import { OAuthConfig, OAuthUserConfig } from "next-auth/providers"; export interface GiteaProfile extends User { - username: string; + username?: string; } export default function Gitea( @@ -65,12 +65,13 @@ export default function Gitea( name: profile.username ?? profile.login, email: profile.email, image: profile.avatar_url, + language: profile.language, }; }, options: config, - style:{ - logo:"/gitea.svg", - brandColor:"#609926", - } + style: { + logo: "/gitea.svg", + brandColor: "#609926", + }, }; }