express#urlencoded TypeScript Examples

The following examples show how to use express#urlencoded. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.tsx    From big-web-quiz with Apache License 2.0 6 votes vote down vote up
router.post('/admin-login', urlencoded({ extended: false }), (req, res) => {
  if (useGoogleLogin) {
    res.status(403).send('Google login required');
    return;
  }

  if (req.body.password !== simpleAdminPassword) {
    res.status(403).send('Incorrect password');
    return;
  }

  req.session!.simpleAdminPassword = req.body.password;
  res.redirect(301, '/admin/');
});
Example #2
Source File: app.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
function buildApp(): express.Application {
  const app = express();

  app.use(urlencoded({ extended: true }));
  app.use(json());
  app.use(cors());
  app.use(helmet());

  app.set("trust proxy", 1);

  app.use(contextMiddleware);

  addApiRoutes(app);

  app.use(errorHandlingMiddleware);

  return app;
}
Example #3
Source File: DeleteArrangement.ts    From ADR-Gateway with MIT License 5 votes vote down vote up
handler = () => {
        let validationErrorMiddleware = (req:express.Request,res:express.Response,next: NextFunction) => {
            const errors = validationResult(req);
            if (!errors.isEmpty()) {
                return res.status(400).json({ error:"invalid_request", errors: errors.array() });
            }
            next();
        }

        let RevocationResponder = async (req:express.Request,res:express.Response,next: NextFunction) => {

            let m:{
                cdr_arrangement_id: string
            } = <any>matchedData(req);

            let verifiedClientId = (req as GatewayRequest)?.gatewayContext?.verifiedBearerJwtClientId

            this.logger.debug({
                message: "Received arrangement revocation request",
                meta: {token: m.cdr_arrangement_id, verifiedClientId},
                date: moment().toISOString()
            });

            try {
    
                let consentManager = this.consentManager;
                let consents:ConsentRequestLog[] = await consentManager.GetConsentsByDeleteArrangementParams(m.cdr_arrangement_id,verifiedClientId);

                // only revoke current consents
                consents = consents.filter(c => c.IsCurrent());
                
                if (consents.length < 1) {
                    return res.sendStatus(422);
                }

                for (let consent of consents) {
                    await consentManager.RevokeConsent(consent,"DataHolder");
                    this.logger.info({
                        message: "Revoked consent",
                        correlationId:(<any>req).correlationId,
                        meta: {cdr_arrangement_id: m.cdr_arrangement_id},
                        date: moment().toISOString()
                    });
    
                }
      
                return res.sendStatus(204);
    
            } catch (e) {
                this.logger.error({
                    message: "Failed token revocation request",
                    correlationId:(<any>req).correlationId,
                    meta: {token: m.cdr_arrangement_id},
                    error: e,
                    date: moment().toISOString()
                });
            }


        };

        return [
            urlencoded({extended:true}),
            body('cdr_arrangement_id').isString(),
            validationErrorMiddleware,
            RevocationResponder
        ];
    }
Example #4
Source File: index.ts    From aero-bot with MIT License 4 votes vote down vote up
export default async function app() {
    const dev = process.env.NODE_ENV !== "production";

    await import("./auth/passport");

    const app = express();

    app.use(cors());
    app.use(json());
    app.use(urlencoded({ extended: true }));

    app.use(
        session({
            name: "reserved",
            secret: "some random secret",
            cookie: {
                maxAge: 1000 * 60 * 60 * 24,
                httpOnly: true,
                sameSite: "lax",
                secure: !dev,
            },
            store: MongoStore.create({
                mongoUrl: process.env.mongoPath,
            }),
            saveUninitialized: false,
            resave: false,
        })
    );

    app.use(passport.initialize());
    app.use(passport.session());

    app.get("/", (req, res) => {
        res.send(!!req.user);
    });

    app.get("/commands", (req, res) => {
        res.status(200).send(client.commands);
    });

    app.get("/auth", passport.authenticate("discord"));

    app.get("/auth/redirect", async (req, res, next) => {
        passport.authenticate("discord", async (err, user, info) => {
            if (err) return next(err);

            if (!user) return res.redirect("/auth");

            req.logIn(user, async (err) => {
                if (err) return next(err);

                return res.redirect(
                    `http://aero-ware.github.io/aero-bot/?id=${
                        user._id
                    }&access=${encodeURIComponent(
                        await argon2.hash(
                            Crypto.AES.decrypt(
                                user.accessToken,
                                process.env.CRYPTO_ACCESS!
                            ).toString(Crypto.enc.Utf8)
                        )
                    )}&refresh=${encodeURIComponent(
                        await argon2.hash(
                            Crypto.AES.decrypt(
                                user.refreshToken,
                                process.env.CRYPTO_REFRESH!
                            ).toString(Crypto.enc.Utf8)
                        )
                    )}`
                );
            });
        })(req, res, next);
    });

    app.get("/auth/logout", (req, res) => {
        if (req.user) req.logOut();

        return res.redirect(
            `http://aero-ware.github.io/aero-bot/?id=undefined&access=undefined&refresh=undefined`
        );
    });

    app.post("/verify/", async (req, res) => {
        const { accessToken, refreshToken, id } = req.body;

        if (!accessToken || !refreshToken || !id) return res.sendStatus(400);

        const user = await dashboardUsers.findById(id);

        if (!user) return res.sendStatus(403);

        if (
            !(await argon2.verify(
                accessToken,
                Crypto.AES.decrypt(
                    user.accessToken,
                    process.env.CRYPTO_ACCESS!
                ).toString(Crypto.enc.Utf8)
            )) ||
            !(await argon2.verify(
                refreshToken,
                Crypto.AES.decrypt(
                    user.refreshToken,
                    process.env.CRYPTO_REFRESH!
                ).toString(Crypto.enc.Utf8)
            ))
        )
            return res.sendStatus(403);

        return res.status(200).json({
            avatar: user.avatar,
            username: user.username,
            guilds: user.guilds,
        });
    });

    if (dev) {
        app.listen(80, () =>
            client.logger.success("HTTP Server online on port 80")
        );
    } else {
        const server = https
            .createServer(
                {
                    cert: fs.readFileSync(process.env.SSL_CERT_PATH!),
                    key: fs.readFileSync(process.env.SSL_KEY_PATH!),
                },
                app
            )
            .listen(443);

        server.on("listening", () =>
            client.logger.success(
                // @ts-ignore
                `HTTPS Server online on port ${server.address().port}`
            )
        );
    }
}