From 565b01cd8e48f2706228344f9d44107e7de76482 Mon Sep 17 00:00:00 2001 From: talksik Date: Tue, 15 Mar 2022 16:16:31 -0400 Subject: [PATCH] setting up api more nad getting closer to the connection to backend and adding auth middleware almost --- packages/api/index.ts | 4 +--- packages/api/middleware/auth.ts | 28 ++++++++++++++++++++++ packages/api/routes/auth.ts | 15 ------------ packages/api/routes/index.ts | 4 ++-- packages/api/routes/user.ts | 16 +++++++++++++ packages/desktop/electron/electron.js | 6 ++--- packages/desktop/src/pages/Login/index.tsx | 2 +- packages/desktop/src/shared/constants.js | 2 +- 8 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 packages/api/middleware/auth.ts delete mode 100644 packages/api/routes/auth.ts create mode 100644 packages/api/routes/user.ts diff --git a/packages/api/index.ts b/packages/api/index.ts index c8b519d..5e3fd3b 100644 --- a/packages/api/index.ts +++ b/packages/api/index.ts @@ -1,13 +1,11 @@ import express, { Application, Request, Response } from "express"; import { NextFunction } from "express"; +import cors from "cors"; import getRoutes from "./routes"; -const cors = require("cors"); - const app = express(); - app.use(cors()); app.use((req: Request, res: Response, next: NextFunction) => { diff --git a/packages/api/middleware/auth.ts b/packages/api/middleware/auth.ts new file mode 100644 index 0000000..6a6f725 --- /dev/null +++ b/packages/api/middleware/auth.ts @@ -0,0 +1,28 @@ +import { NextFunction, Request, Response } from "express"; + +import { OAuth2Client } from "google-auth-library"; + +const client = new OAuth2Client( + "423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com" +); + +// used by specific routes that need to authentication +export const authCheck = async ( + req: Request, + res: Response, + next: NextFunction +) => { + console.log(req.headers); + + // todo get idToken from frontend + const ticket = await client.verifyIdToken({ + idToken: "", + audience: + "423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com", // Specify the CLIENT_ID of the app that accesses the backend + }); + const userid = ticket.getPayload()?.sub; + + console.log(userid); + + next(); +}; diff --git a/packages/api/routes/auth.ts b/packages/api/routes/auth.ts deleted file mode 100644 index 3024f9e..0000000 --- a/packages/api/routes/auth.ts +++ /dev/null @@ -1,15 +0,0 @@ -import express, { Application, Request, Response } from "express"; - -export default function getAuthRoutes() { - const router = express.Router(); - - router.get("/", check); - - return router; -} - -async function check(req: Request, res: Response) { - console.log(req); - - res.send({ message: "check" }); -} diff --git a/packages/api/routes/index.ts b/packages/api/routes/index.ts index 0c4950f..5744e8b 100644 --- a/packages/api/routes/index.ts +++ b/packages/api/routes/index.ts @@ -1,10 +1,10 @@ import express from "express"; -import getAuthRoutes from "./auth"; +import getUserRoutes from "./user"; export default function getRoutes() { const router = express.Router(); - router.use("/auth", getAuthRoutes()); + router.use("/user", getUserRoutes()); return router; } diff --git a/packages/api/routes/user.ts b/packages/api/routes/user.ts new file mode 100644 index 0000000..9d3186e --- /dev/null +++ b/packages/api/routes/user.ts @@ -0,0 +1,16 @@ +import express, { Application, Request, Response } from "express"; + +export default function getUserRoutes() { + const router = express.Router(); + + // get user details based on id from token + router.get("/", getUserDetails); + + return router; +} + +async function getUserDetails(req: Request, res: Response) { + console.log(req); + + res.send({ message: "check" }); +} diff --git a/packages/desktop/electron/electron.js b/packages/desktop/electron/electron.js index cf5cf14..09aa913 100644 --- a/packages/desktop/electron/electron.js +++ b/packages/desktop/electron/electron.js @@ -83,7 +83,7 @@ app.on("activate", () => { async function handleLogin() { // read saved refresh token if any // todo: fix this...should be working - const refreshToken = await store.get("refresh_token"); + const refreshToken = await store.get("tokens"); // todo: remove this when I have way of getting access token from a refresh token if (refreshToken && false) { @@ -96,10 +96,10 @@ async function handleLogin() { const token = await myApiOauth.openAuthWindowAndGetTokens(); // store the refresh token in cookies for app reopen - store.set("refresh_token", token.refresh_token); + store.set("tokens", token); // todo: send the access token to the renderer // send token to client - win.webContents.send(channels.AUTH_TOKEN, token.access_token); + win.webContents.send(channels.AUTH_TOKENS, token); } } diff --git a/packages/desktop/src/pages/Login/index.tsx b/packages/desktop/src/pages/Login/index.tsx index a9cae4c..78a7080 100644 --- a/packages/desktop/src/pages/Login/index.tsx +++ b/packages/desktop/src/pages/Login/index.tsx @@ -15,7 +15,7 @@ export default function Login() { }; useEffect(() => { - window.API.receive(channels.AUTH_TOKEN, async (accessToken: string) => { + window.API.receive(channels.AUTH_TOKENS, async (accessToken: string) => { console.log(accessToken); // get user info from access token diff --git a/packages/desktop/src/shared/constants.js b/packages/desktop/src/shared/constants.js index 3f1b346..7ae55e1 100644 --- a/packages/desktop/src/shared/constants.js +++ b/packages/desktop/src/shared/constants.js @@ -1,6 +1,6 @@ module.exports = { channels: { ACTIVATE_LOG_IN: "ACTIVATE_LOG_IN", - AUTH_TOKEN: "AUTH_TOKEN", + AUTH_TOKENS: "AUTH_TOKENS", }, };