actually, base that on the env vars from here
This commit is contained in:
parent
ddd25e9650
commit
5a272095be
|
@ -13,6 +13,16 @@ declare module "http" {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const devSampleUser = getSampleUser();
|
||||||
|
function getSampleUser(): AT.LingdocsUser | "none" | undefined {
|
||||||
|
const e = process.env.DEV_SAMPLE;
|
||||||
|
if (e === "basic" || e === "student" || e === "admin") {
|
||||||
|
return sampleUsers[e];
|
||||||
|
}
|
||||||
|
if (e === "none") return e;
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchUser(cookies: any): Promise<AT.LingdocsUser | undefined> {
|
async function fetchUser(cookies: any): Promise<AT.LingdocsUser | undefined> {
|
||||||
if (!cookies) {
|
if (!cookies) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -36,12 +46,13 @@ async function fetchUser(cookies: any): Promise<AT.LingdocsUser | undefined> {
|
||||||
/**
|
/**
|
||||||
* express middleware to include the LingdocsUser in req.user if signed in
|
* express middleware to include the LingdocsUser in req.user if signed in
|
||||||
*
|
*
|
||||||
|
* to get sample users, set the DEV_SAMPLE env var to "basic", "student", "editor", or "admin"
|
||||||
|
*
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function lingdocsUserExpressMiddleware(devSampleUser?: "basic" | "student" | "admin" | "editor" | "none"): (req: Request, res: Response, next: NextFunction) => Promise<void> {
|
export async function lingdocsUserExpressMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||||
return async function expressMiddleware(req, res, next) {
|
|
||||||
const user = devSampleUser
|
const user = devSampleUser
|
||||||
? (devSampleUser === "none" ? undefined : sampleUsers[devSampleUser])
|
? (devSampleUser === "none" ? undefined : devSampleUser)
|
||||||
: await fetchUser(req.headers.cookie);
|
: await fetchUser(req.headers.cookie);
|
||||||
Object.defineProperty(
|
Object.defineProperty(
|
||||||
req,
|
req,
|
||||||
|
@ -50,7 +61,6 @@ export function lingdocsUserExpressMiddleware(devSampleUser?: "basic" | "student
|
||||||
);
|
);
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper for a next api route to include the LingdocsUser if logged in
|
* wrapper for a next api route to include the LingdocsUser if logged in
|
||||||
|
@ -63,13 +73,15 @@ export function lingdocsUserExpressMiddleware(devSampleUser?: "basic" | "student
|
||||||
* async function thingRoute(req, res) {
|
* async function thingRoute(req, res) {
|
||||||
* ...
|
* ...
|
||||||
*
|
*
|
||||||
|
* to get sample users, set the DEV_SAMPLE env var to "basic", "student", "editor", or "admin"
|
||||||
|
*
|
||||||
* @param handler
|
* @param handler
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function withLingdocsUserApiRoute(handler: NextApiHandler, devSampleUser?: "basic" | "student" | "admin" | "editor" | "none"): NextApiHandler {
|
export function withLingdocsUserApiRoute(handler: NextApiHandler): NextApiHandler {
|
||||||
return async function nextApiHandlerWrappedWithLingdocsUser(req, res) {
|
return async function nextApiHandlerWrappedWithLingdocsUser(req, res) {
|
||||||
const user = devSampleUser
|
const user = devSampleUser
|
||||||
? (devSampleUser === "none" ? undefined : sampleUsers[devSampleUser])
|
? (devSampleUser === "none" ? undefined : devSampleUser)
|
||||||
: await fetchUser(req.headers.cookies);
|
: await fetchUser(req.headers.cookies);
|
||||||
Object.defineProperty(
|
Object.defineProperty(
|
||||||
req,
|
req,
|
||||||
|
@ -88,6 +100,7 @@ export function withLingdocsUserApiRoute(handler: NextApiHandler, devSampleUser?
|
||||||
* export const getServerSideProps = withLingdocsUserSsr(
|
* export const getServerSideProps = withLingdocsUserSsr(
|
||||||
* async function getServerSideProps({ req }) {
|
* async function getServerSideProps({ req }) {
|
||||||
* ...
|
* ...
|
||||||
|
* to get sample users, set the DEV_SAMPLE env var to "basic", "student", "editor", or "admin"
|
||||||
*
|
*
|
||||||
* @param handler
|
* @param handler
|
||||||
* @returns
|
* @returns
|
||||||
|
@ -98,13 +111,12 @@ export function withLingdocsUserSsr<
|
||||||
handler: (
|
handler: (
|
||||||
context: GetServerSidePropsContext,
|
context: GetServerSidePropsContext,
|
||||||
) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,
|
) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,
|
||||||
devSampleUser?: "basic" | "student" | "admin" | "editor" | "none",
|
|
||||||
) {
|
) {
|
||||||
return async function nextGetServerSidePropsHandlerWrappedWithLingdocsUser(
|
return async function nextGetServerSidePropsHandlerWrappedWithLingdocsUser(
|
||||||
context: GetServerSidePropsContext,
|
context: GetServerSidePropsContext,
|
||||||
) {
|
) {
|
||||||
const user = devSampleUser
|
const user = devSampleUser
|
||||||
? (devSampleUser === "none" ? undefined : sampleUsers[devSampleUser])
|
? (devSampleUser === "none" ? undefined : devSampleUser)
|
||||||
: await fetchUser(context.req.cookies);
|
: await fetchUser(context.req.cookies);
|
||||||
Object.defineProperty(
|
Object.defineProperty(
|
||||||
context.req,
|
context.req,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@lingdocs/lingdocs-main",
|
"name": "@lingdocs/lingdocs-main",
|
||||||
"version": "0.2.3",
|
"version": "0.2.4",
|
||||||
"description": "types and functions for lingdocs stuff",
|
"description": "types and functions for lingdocs stuff",
|
||||||
"main": "dist/library.js",
|
"main": "dist/library.js",
|
||||||
"module": "dist/library.js",
|
"module": "dist/library.js",
|
||||||
|
|
Loading…
Reference in New Issue