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
This commit is contained in:
ngc2207 2024-12-11 19:24:37 +08:00
parent 5ffbecefc2
commit b69cc453e8
3 changed files with 34 additions and 8 deletions

View File

@ -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"
}
} }

View File

@ -1,5 +1,15 @@
import NextAuth from "next-auth"; import NextAuth, { type DefaultSession } from "next-auth";
import Gitea from "@/lib/providers/gitea"; 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({ export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [ providers: [
@ -12,4 +22,16 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
}), }),
], ],
secret: process.env.AUTH_SECRET, 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;
},
},
}); });

View File

@ -2,7 +2,7 @@ import { Email as GiteaEmail, User } from "gitea-js";
import { OAuthConfig, OAuthUserConfig } from "next-auth/providers"; import { OAuthConfig, OAuthUserConfig } from "next-auth/providers";
export interface GiteaProfile extends User { export interface GiteaProfile extends User {
username: string; username?: string;
} }
export default function Gitea( export default function Gitea(
@ -65,12 +65,13 @@ export default function Gitea(
name: profile.username ?? profile.login, name: profile.username ?? profile.login,
email: profile.email, email: profile.email,
image: profile.avatar_url, image: profile.avatar_url,
language: profile.language,
}; };
}, },
options: config, options: config,
style: { style: {
logo: "/gitea.svg", logo: "/gitea.svg",
brandColor: "#609926", brandColor: "#609926",
} },
}; };
} }