From 11b7207501054087b5756a7d5677542978ef315c Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 14 Jan 2024 19:24:54 +0100 Subject: [PATCH] redirect urls --- server.ts | 2 +- src/controllers/calendarController.ts | 31 +++++++++++++++++++++++++++ src/routes/calendarRoutes.ts | 9 ++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/server.ts b/server.ts index 47a1a2b..deb117b 100644 --- a/server.ts +++ b/server.ts @@ -1,4 +1,4 @@ -import express, { Express, Request } from "express"; +import express, { Express } from "express"; import dotenv from "dotenv"; import bodyParser from "body-parser"; import swaggerUI from "swagger-ui-express"; diff --git a/src/controllers/calendarController.ts b/src/controllers/calendarController.ts index 77e06e0..b3c1b71 100644 --- a/src/controllers/calendarController.ts +++ b/src/controllers/calendarController.ts @@ -1,2 +1,33 @@ import { Request, Response } from "express"; import logger from "../logger/logger"; +import Store from "../models/store"; +import { getUserSession } from "../utils/utils"; + +// this request is needed to get the store id after the user has connected his calendar to redirect him back to the calendar page +export async function GetStoreId(req: Request, res: Response) { + try { + const userSession = await getUserSession(req); + + if (!userSession) { + return res.status(401).send({ err: "unauthorized" }); + } + + // check if user has a store + + const store = await Store.findOne({ + where: { + owner_user_id: userSession.user_id, + }, + attributes: ["store_id"], + }); + + if (!store) { + return res.status(401).send({ err: "unauthorized" }); + } + + res.status(200).send({ storeId: store.store_id }); + } catch (error) { + logger.error(error); + res.status(500).send({ err: "invalid request" }); + } +} diff --git a/src/routes/calendarRoutes.ts b/src/routes/calendarRoutes.ts index 3d15ff5..6579c6f 100644 --- a/src/routes/calendarRoutes.ts +++ b/src/routes/calendarRoutes.ts @@ -21,17 +21,16 @@ router.get( router.get( "/auth/google/callback", passport.authenticate("google", { - failureRedirect: - /* "https://customerdashboard.ex.umbach.dev/store/calendar/failed" */ "/login", + failureRedirect: process.env.PASSPORT_FAILURE_REDIRECT_URL as string, }), function (req, res) { // Successful authentication, redirect home. console.log("req.user", req.user); logger.info("req.user", req.user); - res.redirect( - /* "https://customerdashboard.ex.umbach.dev/store/calendar/finish" */ "/" - ); + res.redirect(process.env.PASSPORT_SUCCESS_REDIRECT_URL as string); } ); +router.get("/store", calendarController.GetStoreId); + export default router;