feat: 全站登录保护 + 邮箱白名单 (ALLOWED_EMAILS)
This commit is contained in:
parent
28a6f4bc45
commit
c42392d9df
@ -1,6 +1,11 @@
|
|||||||
import type { NextAuthOptions } from "next-auth";
|
import type { NextAuthOptions } from "next-auth";
|
||||||
import GithubProvider from "next-auth/providers/github";
|
import GithubProvider from "next-auth/providers/github";
|
||||||
|
|
||||||
|
const allowedEmails = (process.env.ALLOWED_EMAILS ?? "")
|
||||||
|
.split(",")
|
||||||
|
.map((e) => e.trim().toLowerCase())
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
export const authOptions: NextAuthOptions = {
|
export const authOptions: NextAuthOptions = {
|
||||||
providers: [
|
providers: [
|
||||||
GithubProvider({
|
GithubProvider({
|
||||||
@ -13,6 +18,10 @@ export const authOptions: NextAuthOptions = {
|
|||||||
signIn: "/login",
|
signIn: "/login",
|
||||||
},
|
},
|
||||||
callbacks: {
|
callbacks: {
|
||||||
|
async signIn({ user }) {
|
||||||
|
if (allowedEmails.length === 0) return true;
|
||||||
|
return allowedEmails.includes((user.email ?? "").toLowerCase());
|
||||||
|
},
|
||||||
async session({ session, token }) {
|
async session({ session, token }) {
|
||||||
if (session.user && token.sub) {
|
if (session.user && token.sub) {
|
||||||
(session.user as { id?: string }).id = token.sub;
|
(session.user as { id?: string }).id = token.sub;
|
||||||
|
|||||||
14
src/middleware.ts
Normal file
14
src/middleware.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
export { default } from "next-auth/middleware";
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
matcher: [
|
||||||
|
/*
|
||||||
|
* 保护所有路由,以下除外:
|
||||||
|
* - /login 登录页
|
||||||
|
* - /api/auth/* NextAuth 回调
|
||||||
|
* - /_next/* Next.js 静态资源
|
||||||
|
* - /favicon.ico
|
||||||
|
*/
|
||||||
|
"/((?!login|api/auth|_next/static|_next/image|favicon\\.ico).*)",
|
||||||
|
],
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user