setting up api more nad getting closer to the connection to backend and adding auth middleware almost
This commit is contained in:
@@ -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) => {
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
@@ -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" });
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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" });
|
||||
}
|
||||
Reference in New Issue
Block a user