diff --git a/client/src/util/API.ts b/client/src/util/API.ts
index bf55e63..546390d 100644
--- a/client/src/util/API.ts
+++ b/client/src/util/API.ts
@@ -3,6 +3,13 @@ import { IUser, IUserAuth, IFriendship, IRecipe, IIngredient, ICollection, IGroc
import { default as _instance } from "./axiosInstance";
module API {
+ export enum CRUDMETHOD {
+ GET,
+ PUT,
+ POST,
+ DELETE
+ }
+
export class Settings {
private static APISTRING = import.meta.env.APISTRING || "http://localhost:8080";
private static token?: string;
@@ -23,7 +30,7 @@ module API {
abstract class RestController
{
protected instance = _instance;
protected endpoint: string;
- protected headers?: any
+ protected headers?: any;
constructor(endpoint: string, token: string) {
this.endpoint = endpoint;
@@ -35,6 +42,19 @@ module API {
};
}
+ async customRoute(method: CRUDMETHOD, path: string, data?: any, requireHeaders = true) {
+ switch (method) {
+ case CRUDMETHOD.GET:
+ return this.instance.get(this.endpoint + path, (requireHeaders && this.headers));
+ case CRUDMETHOD.PUT:
+ return this.instance.put(this.endpoint + path, data, (requireHeaders && this.headers));
+ case CRUDMETHOD.POST:
+ return this.instance.post(this.endpoint + path, data, (requireHeaders && this.headers));
+ case CRUDMETHOD.DELETE:
+ return this.instance.delete(this.endpoint + path, (requireHeaders && this.headers));
+ }
+ }
+
async getAll() {
const response = await this.instance.get(this.endpoint, this.headers);
return Promise.resolve(response.data);
@@ -144,6 +164,11 @@ module API {
constructor(token: string) {
super(Settings.getAPISTRING() + "/app/collection", token);
}
+
+ async getAllAuthored() {
+ const response = await this.customRoute(CRUDMETHOD.GET, "?authored=true");
+ return Promise.resolve(response.data);
+ }
}
export class GroceryList extends RestController {
diff --git a/server/auth/middlewares.ts b/server/auth/middlewares.ts
index dd09d7c..21031d7 100644
--- a/server/auth/middlewares.ts
+++ b/server/auth/middlewares.ts
@@ -5,7 +5,7 @@ import { IUser } from "../schemas";
dotenv.config();
export function restrictAccess(req: Request, res: Response, next: NextFunction) {
- if (req.session.user == undefined) {
+ if (req.user == undefined) {
res.send("content restricted");
} else {
next();
diff --git a/server/controllers/CollectionCtl.ts b/server/controllers/CollectionCtl.ts
index 10bf36a..1e87b31 100644
--- a/server/controllers/CollectionCtl.ts
+++ b/server/controllers/CollectionCtl.ts
@@ -23,6 +23,13 @@ export default class CollectionCtl {
return new ControllerResponse(code, data);
}
+ async getAllAuthored(id: number | string) {
+ const result = await CollectionInstance.getAllAuthored(id);
+ const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
+ const data = result || "No collections found";
+ return new ControllerResponse(code, data);
+ }
+
async getUserDefault(id: number | string) {
const result = await CollectionInstance.getUserDefault(id);
const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
diff --git a/server/models/collection.ts b/server/models/collection.ts
index b15f4f1..3974f76 100644
--- a/server/models/collection.ts
+++ b/server/models/collection.ts
@@ -18,6 +18,22 @@ export class Collection {
}
}
+ async getAllAuthored(id: number | string) {
+ console.log(id, typeof id);
+ try {
+ const statement = `
+ SELECT * FROM recipin.collection
+ WHERE ownerid = $1;
+ `
+ const result = await pool.query(statement, [id]);
+ console.log(result.rows);
+ if (result.rows.length) return result.rows;
+ return null;
+ } catch (e: any) {
+ throw new Error(e);
+ }
+ }
+
async getUserDefault(id: number | string) {
try {
const statement = `
@@ -46,7 +62,6 @@ export class Collection {
}
async post(data: ICollection) {
- console.log('new default collection');
const { name, active, ismaincollection, ownerid } = data;
try {
const statement = `
diff --git a/server/routes/auth.ts b/server/routes/auth.ts
index 43c0be2..a9c5b0d 100644
--- a/server/routes/auth.ts
+++ b/server/routes/auth.ts
@@ -16,11 +16,6 @@ const router = Router();
export const authRoute = (app: Express) => {
app.use('/auth', router);
- router.use((req, res, next) => {
- console.log(req.session);
- next();
- })
-
router.get('/', restrictAccess, (req, res, next) => {
if (req.session.user) {
const user = req.session.user;
@@ -44,8 +39,6 @@ export const authRoute = (app: Express) => {
router.post('/login', async (req, res, next) => {
try {
const data: IUserAuth = req.body;
- console.log(data);
-
const response: ControllerResponse = await AuthInstance.login(data);
if (response.ok) {
@@ -70,8 +63,6 @@ export const authRoute = (app: Express) => {
return next(err);
})
- console.log(req.session);
-
res.cookie('token', token, { httpOnly: true });
res.json({ token });
} else {
diff --git a/server/routes/collection.ts b/server/routes/collection.ts
index 5682ff9..45dfbf1 100644
--- a/server/routes/collection.ts
+++ b/server/routes/collection.ts
@@ -1,6 +1,7 @@
import { Express, Router } from "express";
import { checkIsAdmin, restrictAccess } from "../auth/middlewares";
import CollectionCtl from "../controllers/CollectionCtl";
+import { IUser } from "../schemas";
const CollectionInstance = new CollectionCtl();
const router = Router();
@@ -8,12 +9,6 @@ const router = Router();
export const collectionRoute = (app: Express) => {
app.use('/app/collection', router);
- router.use((req, res, next) => {
- console.log('what gives');
- console.log(req.body);
- next();
- })
-
router.get('/:id', async (req, res, next) => {
const { id } = req.params;
try {
@@ -24,11 +19,32 @@ export const collectionRoute = (app: Express) => {
}
})
- // implement is admin on this route
- router.get('/', checkIsAdmin, async (req, res, next) => {
+ router.get('&authored=true', restrictAccess, async (req, res, next) => {
+ const user = req.user as IUser;
+ console.log(user.id);
try {
- const { code, data } = await CollectionInstance.getAll();
+ const { code, data } = await CollectionInstance.getAllAuthored(user.id as number);
res.status(code).send(data);
+ } catch (e) {
+ next(e);
+ }
+ })
+
+ // implement is admin on this route
+ router.get('/', restrictAccess, async (req, res, next) => {
+ const user = req.user as IUser;
+ const { authored } = req.query;
+
+ try {
+ if (authored || authored == "true") {
+ const { code, data } = await CollectionInstance.getAllAuthored(user.id as number);
+ res.status(code).send(data);
+ } else {
+ if (user.isadmin) {
+ const { code, data } = await CollectionInstance.getAll();
+ res.status(code).send(data);
+ }
+ }
} catch(e) {
next(e);
}
@@ -36,7 +52,6 @@ export const collectionRoute = (app: Express) => {
router.post('/', async (req, res, next) => {
const data = req.body;
- console.log(req.body ?? "sanity check");
try {
const result = await CollectionInstance.post(data);
diff --git a/server/routes/index.ts b/server/routes/index.ts
index 039b797..04f5161 100644
--- a/server/routes/index.ts
+++ b/server/routes/index.ts
@@ -21,9 +21,7 @@ export const routes = async (app: Express) => {
// middleware to check for auth on cookies on each request in protected routes
app.use('/app', async (req, res, next) => {
// pull jwt from request headers
- console.log(req.headers);
const token = req.headers['authorization']?.split(" ")[1];
- console.log(token);
if (!token) {
res.status(403).send("Unauthorized, did not receive token");
@@ -32,8 +30,8 @@ export const routes = async (app: Express) => {
if (err) {
res.status(403).send(err);
} else {
- console.log(data);
- req.user = data;
+ // @ts-ignore
+ req.user = data.user;
next();
}
})