cleaning a bunch of the auth stuff but not done

This commit is contained in:
talksik
2022-04-01 11:10:37 -04:00
parent 95fe01af03
commit 685c03e505
18 changed files with 554 additions and 678 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ app.get("/", (req: Request, res: Response) => {
res.send("hello world.");
});
app.use("/api/users", getUserRoutes());
app.use("/api/user", getUserRoutes());
app.use("/api/search", getSearchRoutes());
app.use("/api/conversations", getConversationRoutes());
app.use("/api/contacts", getContactsRoutes());
+14 -12
View File
@@ -14,21 +14,23 @@ export const authCheck = async (
) => {
const { authorization } = req.headers;
// verify jwt token
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;
const email = ticket.getPayload()?.email;
// 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;
// const email = ticket.getPayload()?.email;
if (!userId) throw new Error("No google user Id found");
// if (!userId) throw new Error("No google user Id found");
// used in subsequent handlers
// todo: have to get our database id for the user instead of google's id
res.locals.userId = userId;
res.locals.email = email;
// // used in subsequent handlers
// // todo: have to get our database id for the user instead of google's id
// res.locals.userId = userId;
// res.locals.email = email;
next();
} catch (error) {
+33 -12
View File
@@ -1,6 +1,7 @@
import { GoogleUserInfo, User } from "@nirvana/core/models";
import express, { Application, Request, Response } from "express";
import { OAuth2Client } from "google-auth-library";
import { ObjectID } from "bson";
import { ObjectId } from "mongodb";
import { UserService } from "../services/user.service";
@@ -8,31 +9,48 @@ import { UserStatus } from "../../core/models/user.model";
import { authCheck } from "../middleware/auth";
import { collections } from "../services/database.service";
const client = new OAuth2Client(
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com"
);
export default function getUserRoutes() {
const router = express.Router();
router.use(express.json());
// get user details based on id token
router.get("/", authCheck, getUserDetails);
// router.get("/", authCheck, getUserDetails);
router.get("/login", login);
router.get("/authCheck", authCheck);
return router;
}
/**
* Use token from middleware and get user properties
* create user if doesn't exist
/** Create user if doesn't exist
* Returns jwt token for client and user details
*/
async function getUserDetails(req: Request, res: Response) {
const email: string = res.locals.email;
const userId: string = res.locals.userId;
async function login(req: Request, res: Response) {
// passed in accesstoken no matter what
const { access_token } = req.query;
const { access_token, id_token } = req.query;
try {
const ticket = await client.verifyIdToken({
idToken: (id_token as string) ?? "",
audience:
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com", // Specify the CLIENT_ID of the app that accesses the backend
});
const googleUserId = ticket.getPayload()?.sub as string;
const email = ticket.getPayload()?.email as string;
if (!googleUserId || !email) {
res.status(401).send("no google account found");
return;
}
// return user details if it passed auth middleware
const user = await UserService.getUserByGoogleId(userId);
const user = await UserService.getUserByEmail(email);
// if no user found, then go ahead and create user
if (!user) {
@@ -49,7 +67,7 @@ async function getUserDetails(req: Request, res: Response) {
// create initial user model object
const newUser = new User(
userId,
googleUserId,
userInfo.email,
userInfo.verifiedEmail,
userInfo.name,
@@ -67,6 +85,8 @@ async function getUserDetails(req: Request, res: Response) {
newUser._id = insertResult?.insertedId;
// create jwt token with new user info
insertResult
? res.status(200).send(newUser)
: res.status(500).send("Failed to create account, already exists");
@@ -74,7 +94,8 @@ async function getUserDetails(req: Request, res: Response) {
return;
}
// otherwise, just return the user details
// create jwt token with existing user info
res.status(200).send(user);
} catch (error) {
console.log(error);