mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
Tighten down sign in
Email: only from ADMIN_EMAIL Github: only user tomru
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import NextAuth from 'next-auth'
|
import NextAuth from 'next-auth'
|
||||||
import EmailProvider from 'next-auth/providers/email'
|
import EmailProvider from 'next-auth/providers/email'
|
||||||
|
import GitHubProvider from "next-auth/providers/github";
|
||||||
|
|
||||||
import { MongoDBAdapter } from '@next-auth/mongodb-adapter'
|
import { MongoDBAdapter } from '@next-auth/mongodb-adapter'
|
||||||
import { MONGO_URI } from '../../../db'
|
import { MONGO_URI } from '../../../db'
|
||||||
@@ -8,6 +9,9 @@ import { MongoClient } from 'mongodb'
|
|||||||
|
|
||||||
let client: MongoClient
|
let client: MongoClient
|
||||||
|
|
||||||
|
const ADMIN_EMAIL = process.env.ADMIN_EMAIL
|
||||||
|
const GITHUB_USERS_GRANTED = ['111471'];
|
||||||
|
|
||||||
async function getMongoClient() {
|
async function getMongoClient() {
|
||||||
if (!client) {
|
if (!client) {
|
||||||
client = new MongoClient(MONGO_URI)
|
client = new MongoClient(MONGO_URI)
|
||||||
@@ -22,6 +26,10 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
secret: process.env.NEXTAUTH_SECRET,
|
secret: process.env.NEXTAUTH_SECRET,
|
||||||
adapter: MongoDBAdapter(getMongoClient()),
|
adapter: MongoDBAdapter(getMongoClient()),
|
||||||
providers: [
|
providers: [
|
||||||
|
GitHubProvider({
|
||||||
|
clientId: process.env.GITHUB_CLIENT_ID,
|
||||||
|
clientSecret: process.env.GITHUB_CLIENT_SECRET
|
||||||
|
}),
|
||||||
EmailProvider({
|
EmailProvider({
|
||||||
server: {
|
server: {
|
||||||
host: 'smtp.sendgrid.net',
|
host: 'smtp.sendgrid.net',
|
||||||
@@ -34,5 +42,23 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
from: process.env.FROM_EMAIL,
|
from: process.env.FROM_EMAIL,
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
callbacks: {
|
||||||
|
async signIn({ account, email }) {
|
||||||
|
// if user sigin requested magic link via EmailProvider
|
||||||
|
if (account.provider === 'email') {
|
||||||
|
if (email.verificationRequest) {
|
||||||
|
// only allow admins by email entered
|
||||||
|
return account.providerAccountId === ADMIN_EMAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if user accesses with magic link, also only allow admin
|
||||||
|
return account.providerAccountId === ADMIN_EMAIL
|
||||||
|
} else if (account.provider === 'github') {
|
||||||
|
// only one and only one user
|
||||||
|
return GITHUB_USERS_GRANTED.includes(account.providerAccountId);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user