mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
move to next-auth
This commit is contained in:
committed by
Thomas Ruoff
parent
9f4180388b
commit
b257bc8258
@@ -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
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user