-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
305 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import e, { RequestHandler, Request, Response } from "express"; | ||
import { AuthClientRequest } from "../AuthTypes"; | ||
import { AUTH_ROUTES_AND_PARAMS, AuthHandler, HTTP_FAIL_CODES } from "../AuthHandler"; | ||
import { getReturnUrl } from "../utils/getReturnUrl"; | ||
import { DBOFullyTyped } from "../../DBSchemaBuilder"; | ||
|
||
export function setCatchAllRequestHandler(this: AuthHandler, app: e.Express) { | ||
const onLogout = async (req: Request, res: Response) => { | ||
const sid = this.validateSid(req.cookies?.[this.sidKeyName]); | ||
if (sid) { | ||
try { | ||
await this.throttledFunc(() => { | ||
return this.opts.logout?.(req.cookies?.[this.sidKeyName], this.dbo as any, this.db); | ||
}); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
res.redirect("/"); | ||
}; | ||
|
||
const requestHandler: RequestHandler = async (req, res, next) => { | ||
const { onGetRequestOK } = this.opts.expressConfig ?? {}; | ||
const clientReq: AuthClientRequest = { httpReq: req, res }; | ||
const getUser = async () => { | ||
const userOrCode = await this.getUserAndHandleError(clientReq); | ||
if (typeof userOrCode === "string") { | ||
res.status(HTTP_FAIL_CODES.BAD_REQUEST).json({ success: false, code: userOrCode }); | ||
throw userOrCode; | ||
} | ||
return userOrCode; | ||
}; | ||
const isLoggedInUser = async () => { | ||
const userInfo = await getUser(); | ||
return !!userInfo.user; | ||
}; | ||
if (this.prostgles.restApi) { | ||
if ( | ||
Object.values(this.prostgles.restApi.routes).some((restRoute) => | ||
this.matchesRoute(restRoute.split("/:")[0], req.path) | ||
) | ||
) { | ||
next(); | ||
return; | ||
} | ||
} | ||
try { | ||
const returnURL = getReturnUrl(req); | ||
|
||
if (this.matchesRoute(AUTH_ROUTES_AND_PARAMS.logoutGetPath, req.path)) { | ||
await onLogout(req, res); | ||
return; | ||
} | ||
|
||
if (this.matchesRoute(AUTH_ROUTES_AND_PARAMS.loginWithProvider, req.path)) { | ||
next(); | ||
return; | ||
} | ||
/** | ||
* Requesting a User route | ||
*/ | ||
if (this.isUserRoute(req.path)) { | ||
/* Check auth. Redirect to login if unauthorized */ | ||
const u = await isLoggedInUser(); | ||
if (!u) { | ||
res.redirect( | ||
`${AUTH_ROUTES_AND_PARAMS.login}?returnURL=${encodeURIComponent(req.originalUrl)}` | ||
); | ||
return; | ||
} | ||
|
||
/* If authorized and going to returnUrl then redirect. Otherwise serve file */ | ||
} else if (returnURL && (await isLoggedInUser())) { | ||
res.redirect(returnURL); | ||
return; | ||
|
||
/** If Logged in and requesting login then redirect to main page */ | ||
} else if ( | ||
this.matchesRoute(AUTH_ROUTES_AND_PARAMS.login, req.path) && | ||
(await isLoggedInUser()) | ||
) { | ||
res.redirect("/"); | ||
return; | ||
} | ||
|
||
onGetRequestOK?.(req, res, { | ||
getUser, | ||
dbo: this.dbo as DBOFullyTyped, | ||
db: this.db, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
const errorMessage = | ||
typeof error === "string" ? error | ||
: error instanceof Error ? error.message | ||
: ""; | ||
res.status(HTTP_FAIL_CODES.BAD_REQUEST).json({ | ||
error: | ||
"Something went wrong when processing your request" + | ||
(errorMessage ? ": " + errorMessage : ""), | ||
}); | ||
} | ||
}; | ||
|
||
app.get(AUTH_ROUTES_AND_PARAMS.catchAll, requestHandler); | ||
return requestHandler; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import e from "express"; | ||
import { AUTH_ROUTES_AND_PARAMS, AuthHandler, HTTP_FAIL_CODES } from "../AuthHandler"; | ||
import { LoginParams } from "../AuthTypes"; | ||
|
||
export function setLoginRequestHandler(this: AuthHandler, app: e.Express) { | ||
app.post(AUTH_ROUTES_AND_PARAMS.login, async (req, res) => { | ||
try { | ||
const loginParams: LoginParams = { | ||
type: "username", | ||
...req.body, | ||
}; | ||
|
||
await this.loginThrottledAndSetCookie(req, res, loginParams); | ||
} catch (error) { | ||
res.status(HTTP_FAIL_CODES.BAD_REQUEST).json({ error }); | ||
} | ||
}); | ||
} |
Oops, something went wrong.