move to next-auth

This commit is contained in:
Thomas Ruoff
2021-10-02 00:35:28 +02:00
committed by Thomas Ruoff
parent 9f4180388b
commit b257bc8258
12 changed files with 536 additions and 1418 deletions

View File

@@ -1,23 +0,0 @@
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD
export function authenticateAdmin({
username,
password,
}: {
username: string
password: string
}): boolean {
if (username !== 'admin') {
return false
}
if (!ADMIN_PASSWORD) {
throw new Error('ADMIN_PASSWORD not set. Login disabled!')
}
if (password !== ADMIN_PASSWORD) {
return false
}
return true
}

View File

@@ -1,46 +0,0 @@
import { withIronSession, Handler } from 'next-iron-session'
import { getBaseURL } from '../helpers/url'
export enum USER_ROLE {
ADMIN = 'admin',
}
export type UserData = {
username: string
role: USER_ROLE
}
const SESSION_SECRET =
process.env.SESSION_SECRET || 'dev-env-default-secret-991823723'
export default function withSession(handler: Handler) {
return withIronSession(handler, {
password: SESSION_SECRET,
cookieName: 'pfadi-bussle-cookie',
cookieOptions: {
// the next line allows to use the session in non-https environements like
// Next.js dev mode (http://localhost:3000)
secure: process.env.NODE_ENV === 'production',
},
})
}
export function isAdminSession(req: any) {
const user = req?.session.get('user') as UserData
if (user && user.role === USER_ROLE.ADMIN) {
return user
}
return null
}
export function redirectToLogin(req: any, res: any) {
const redirectTargetUrl = `${getBaseURL()}/login?redirect=${encodeURIComponent(
req.url
)}`
res.writeHead(303, {
Location: redirectTargetUrl,
})
res.end()
}