feat: add email restrict

This commit is contained in:
moonrailgun 2024-08-04 17:28:20 +08:00
parent 7f7c95b11c
commit 0a0a27549a
2 changed files with 22 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import { Auth, AuthConfig, createActionURL } from '@auth/core';
import Nodemailer from '@auth/core/providers/nodemailer'; import Nodemailer from '@auth/core/providers/nodemailer';
import Credentials from '@auth/core/providers/credentials'; import Credentials from '@auth/core/providers/credentials';
import Github from '@auth/core/providers/github'; import Github from '@auth/core/providers/github';
import Google from '@auth/core/providers/google';
import { env } from '../utils/env.js'; import { env } from '../utils/env.js';
import { prisma } from './_client.js'; import { prisma } from './_client.js';
import type { PrismaClient, Prisma, User } from '@prisma/client'; import type { PrismaClient, Prisma, User } from '@prisma/client';
@ -83,7 +84,7 @@ export const authConfig: Omit<AuthConfig, 'raw'> = {
...env.auth.github, ...env.auth.github,
}), }),
env.auth.provider.includes('google') && env.auth.provider.includes('google') &&
Github({ Google({
id: 'google', id: 'google',
name: 'Google', name: 'Google',
...env.auth.google, ...env.auth.google,
@ -107,6 +108,23 @@ export const authConfig: Omit<AuthConfig, 'raw'> = {
return session; return session;
}, },
async signIn({ user, account, profile, email }) {
if (account?.type === 'oauth') {
if (env.auth.restrict.email) {
if (profile?.email) {
return profile.email.endsWith(env.auth.restrict.email);
}
}
}
if (account?.type === 'email' && env.auth.restrict.email) {
if (user.email) {
return user.email.endsWith(env.auth.restrict.email);
}
}
return true;
},
}, },
}; };

View File

@ -19,6 +19,9 @@ export const env = {
!!process.env.AUTH_GITHUB_ID && 'github', !!process.env.AUTH_GITHUB_ID && 'github',
!!process.env.AUTH_GOOGLE_ID && 'google', !!process.env.AUTH_GOOGLE_ID && 'google',
]), ]),
restrict: {
email: process.env.AUTH_RESTRICT_EMAIL, // for example: @example.com
},
secret: process.env.AUTH_SECRET || md5(jwtSecret), secret: process.env.AUTH_SECRET || md5(jwtSecret),
email: { email: {
server: process.env.EMAIL_SERVER, server: process.env.EMAIL_SERVER,