feat: init routes protection

This commit is contained in:
swve 2023-04-10 19:59:50 +02:00
parent 49b6d1dfe7
commit 1ad8ee12b1
25 changed files with 82 additions and 64 deletions

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
export async function uploadNewImageFile(file: any, activity_id: string) {
// Send file thumbnail as form data

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
export async function uploadNewPDFFile(file: any, activity_id: string) {
// Send file thumbnail as form data

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
export async function submitQuizBlock(activity_id: string, data: any) {

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
export async function uploadNewVideoFile(file: any, activity_id: string) {
// Send file thumbnail as form data

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
export async function createActivity(data: any, chapter_id: any, org_id: any) {
data.content = {};

View file

@ -1,4 +1,4 @@
import { RequestBody } from "@services/utils/requests";
import { RequestBody } from "@services/utils/ts/requests";
import { getAPIUrl } from "@services/config/config";
/*

View file

@ -1,6 +1,5 @@
import { initialData } from "../../components/Pages/CourseEdit/Draggables/data";
import { getAPIUrl } from "@services/config/config";
import { RequestBody } from "@services/utils/requests";
import { RequestBody, errorHandling } from "@services/utils/ts/requests";
/*
This file includes only POST, PUT, DELETE requests
@ -9,38 +8,26 @@ import { RequestBody } from "@services/utils/requests";
//TODO : depreciate this function
export async function getCourseChaptersMetadata(course_id: any) {
const response = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null));
if (!response.ok) {
const error: any = new Error(`Error ${response.status}: ${response.statusText}`, {});
error.status = response.status;
throw error;
}
const data = await response.json();
return data;
const result = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null));
const res = await errorHandling(result);
return res;
}
export async function updateChaptersMetadata(course_id: any, data: any) {
const result: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("PUT", data))
.then((result) => result.json())
.catch((error) => console.log("error", error));
return result;
const result: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("PUT", data));
const res = await errorHandling(result);
return res;
}
export async function createChapter(data: any, course_id: any) {
const result: any = await fetch(`${getAPIUrl()}chapters/?course_id=course_${course_id}`, RequestBody("POST", data))
.then((result) => result.json())
.catch((error) => console.log("error", error));
const result: any = await fetch(`${getAPIUrl()}chapters/?course_id=course_${course_id}`, RequestBody("POST", data));
const res = await errorHandling(result);
return result;
return res;
}
export async function deleteChapter(coursechapter_id: any) {
const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, RequestBody("DELETE", null))
.then((result) => result.json())
.catch((error) => console.log("error", error));
return result;
const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, RequestBody("DELETE", null));
const res = await errorHandling(result);
return res;
}

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "../config/config";
import { RequestBody } from "../utils/requests";
import { RequestBody } from "../utils/ts/requests";
/*
This file includes only POST, PUT, DELETE requests

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
/*
This file includes only POST, PUT, DELETE requests

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody } from "../utils/requests";
import { RequestBody } from "../utils/ts/requests";
/*
This file includes only POST, PUT, DELETE requests

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody } from "@services/utils/requests";
import { RequestBody } from "@services/utils/ts/requests";
/*
This file includes only POST, PUT, DELETE requests

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody } from "@services/utils/requests";
import { RequestBody } from "@services/utils/ts/requests";
/*
This file includes only POST, PUT, DELETE requests

View file

@ -1,5 +1,5 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody } from "@services/utils/requests";
import { RequestBody } from "@services/utils/ts/requests";
/*
This file includes only POST, PUT, DELETE requests

View file

@ -0,0 +1,14 @@
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context";
import { NextRouter } from "next/router";
export const denyAccessToUser = (error : any, router: AppRouterInstance) => {
if (error.status === 401) {
router.push("/login");
}
if (error.status === 403) {
router.push("/login");
// TODO : add a message to the user to tell him he is not allowed to access this page, route to /error
}
}

View file

@ -1,3 +1,6 @@
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context";
import { denyAccessToUser } from "../react/middlewares/views";
export const RequestBody = (method: string, data: any) => {
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
let options: any = {
@ -24,7 +27,7 @@ export const RequestBodyForm = (method: string, data: any) => {
return options;
};
export const swrFetcher = async (url: string, body: any) => {
export const swrFetcher = async (url: string, body: any, router?: AppRouterInstance) => {
// Create the request options
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
let options: any = {
@ -39,15 +42,25 @@ export const swrFetcher = async (url: string, body: any) => {
options.body = JSON.stringify(body);
}
// Fetch the data
const res = await fetch(url, options);
try {
// Fetch the data
const request = await fetch(url, options);
let res = errorHandling(request);
// If the response is not in the 200 range, throw an error
// Return the data
return res;
} catch (error: any) {
if (router) {
denyAccessToUser(error, router);
}
}
};
export const errorHandling = (res: any) => {
if (!res.ok) {
const error = new Error("An error occurred while fetching the data.");
const error: any = new Error(`Error ${res.status}: ${res.statusText}`, {});
error.status = res.status;
throw error;
}
// Return the data
return res.json();
};