Files
nirvana-v3/packages/api/middleware/auth.ts
T
2022-03-17 20:13:49 -04:00

36 lines
931 B
TypeScript

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
) => {
const { authorization } = req.headers;
console.log(authorization);
try {
const ticket = await client.verifyIdToken({
idToken: authorization ?? "",
audience:
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com", // Specify the CLIENT_ID of the app that accesses the backend
});
const userId = ticket.getPayload()?.sub;
// used in subsequent handlers
res.locals.userId = userId;
console.log(userId);
next();
} catch (error) {
res.status(401).send("unauthorized");
}
};