trying with the store and such
This commit is contained in:
@@ -0,0 +1 @@
|
||||
MONGO_CONNECTION_STRING=mongodb+srv://default:[email protected]/default?retryWrites=true&w=majority
|
||||
@@ -21,12 +21,15 @@ export const authCheck = async (
|
||||
audience:
|
||||
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com", // Specify the CLIENT_ID of the app that accesses the backend
|
||||
});
|
||||
const userid = ticket.getPayload()?.sub;
|
||||
const userId = ticket.getPayload()?.sub;
|
||||
|
||||
console.log(userid);
|
||||
// used in subsequent handlers
|
||||
res.locals.userId = userId;
|
||||
|
||||
console.log(userId);
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
res.status(401).send(error);
|
||||
res.status(401).send("unauthorized");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,13 +4,16 @@
|
||||
"main": "index.ts",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@nirvana/core": "*",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.17.3",
|
||||
"google-auth-library": "^7.14.0"
|
||||
"google-auth-library": "^7.14.0",
|
||||
"mongodb": "^4.4.1"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "nodemon",
|
||||
"start": "ts-node index.ts"
|
||||
"dev": "NODE_ENV=development && nodemon",
|
||||
"start": "ts-node index.ts",
|
||||
"production": "NODE_ENV=production && nodemon"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.13",
|
||||
|
||||
@@ -1,16 +1,65 @@
|
||||
import { GoogleUserInfo, User } from "@nirvana/core/models";
|
||||
import express, { Application, Request, Response } from "express";
|
||||
|
||||
import { ObjectId } from "mongodb";
|
||||
import { authCheck } from "../middleware/auth";
|
||||
import { collections } from "../services/database.service";
|
||||
|
||||
export default function getUserRoutes() {
|
||||
const router = express.Router();
|
||||
|
||||
// get user details based on id from token
|
||||
router.use(express.json());
|
||||
|
||||
// get user details based on id token
|
||||
router.get("/", authCheck, getUserDetails);
|
||||
|
||||
//
|
||||
router.post("/", createUser);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use token from middleware and get user properties
|
||||
* create user if doesn't exist
|
||||
*/
|
||||
async function getUserDetails(req: Request, res: Response) {
|
||||
res.send({ message: "check" });
|
||||
const userId: string = res.locals.userId;
|
||||
|
||||
try {
|
||||
const query = { _id: new ObjectId(userId) };
|
||||
|
||||
// return user details if it passed auth middleware
|
||||
const user = (await collections.users?.findOne(query)) as unknown as User;
|
||||
|
||||
res.status(200).send(user);
|
||||
} catch (error) {
|
||||
res
|
||||
.status(404)
|
||||
.send(`unable to find a matching document with id: ${userId}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function createUser(req: Request, res: Response) {
|
||||
try {
|
||||
const { access_token } = req.query;
|
||||
|
||||
if (!access_token) {
|
||||
res.status(400);
|
||||
|
||||
return;
|
||||
}
|
||||
// create user if not exists
|
||||
|
||||
// get user info from access token
|
||||
const userInfo: GoogleUserInfo = await (
|
||||
await fetch(
|
||||
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${access_token}`
|
||||
)
|
||||
).json();
|
||||
|
||||
res.send();
|
||||
} catch (error) {
|
||||
res.status(500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
// External Dependencies
|
||||
import * as mongoDB from "mongodb";
|
||||
// import * as dotenv from "dotenv";
|
||||
|
||||
// Global Variables
|
||||
|
||||
export const collections: { users?: mongoDB.Collection } = {};
|
||||
|
||||
// Initialize Connection
|
||||
export async function connectToDatabase() {
|
||||
const client: mongoDB.MongoClient = new mongoDB.MongoClient(
|
||||
process.env.DB_CONN_STRING!
|
||||
);
|
||||
|
||||
await client.connect();
|
||||
|
||||
const db: mongoDB.Db = client.db(process.env.DB_NAME);
|
||||
|
||||
const usersCollection: mongoDB.Collection = db.collection("users");
|
||||
|
||||
collections.users = usersCollection;
|
||||
|
||||
console.log(
|
||||
`Successfully connected to database: ${db.databaseName} and collection: ${usersCollection.collectionName}`
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user