mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
cookies and login fix
This commit is contained in:
parent
885198d357
commit
57aea151f4
7 changed files with 19 additions and 21 deletions
|
@ -27,7 +27,6 @@ import dbtools from '../utils/dbtools'
|
|||
|
||||
interface Options {
|
||||
userDB: Database
|
||||
jsonResponse: boolean
|
||||
exceptions: Array<string>
|
||||
}
|
||||
|
||||
|
@ -43,9 +42,9 @@ export const testUser: User = {
|
|||
createdBy: 1,
|
||||
}
|
||||
|
||||
function renderLogin(_req: Request, res: Response, jsonResponse: boolean) {
|
||||
function renderLogin(req: Request, res: Response) {
|
||||
res.status(401) // Unauthorized
|
||||
if (jsonResponse) {
|
||||
if (req.is('application/json')) {
|
||||
res.json({
|
||||
result: 'nouser',
|
||||
msg: 'You are not logged in',
|
||||
|
@ -60,18 +59,16 @@ function renderLogin(_req: Request, res: Response, jsonResponse: boolean) {
|
|||
export default function (options: Options): RequestHandler {
|
||||
const {
|
||||
userDB,
|
||||
jsonResponse,
|
||||
exceptions,
|
||||
}: {
|
||||
userDB: Database
|
||||
jsonResponse: boolean
|
||||
exceptions: string[]
|
||||
} = options
|
||||
|
||||
return function (req: Request, res: Response, next: NextFunction) {
|
||||
const sessionID = req.cookies.sessionID
|
||||
const isException = exceptions.some((exc) => {
|
||||
return req.url.split('?')[0] === exc
|
||||
return req.originalUrl.split('?')[0] === exc
|
||||
})
|
||||
|
||||
if (process.env.NS_NOUSER) {
|
||||
|
@ -102,7 +99,7 @@ export default function (options: Options): RequestHandler {
|
|||
return
|
||||
}
|
||||
logger.DebugLog(`No session ID: ${req.url}`, 'auth', 1)
|
||||
renderLogin(req, res, jsonResponse)
|
||||
renderLogin(req, res)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -116,7 +113,7 @@ export default function (options: Options): RequestHandler {
|
|||
return
|
||||
}
|
||||
logger.DebugLog(`No user:${req.url}`, 'auth', 1)
|
||||
renderLogin(req, res, jsonResponse)
|
||||
renderLogin(req, res)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue