mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
fix: session auth issues
This commit is contained in:
parent
1708b36818
commit
08cc97f557
70 changed files with 607 additions and 427 deletions
|
|
@ -1,18 +1,22 @@
|
|||
import { getAPIUrl } from '@services/config/config'
|
||||
import {
|
||||
RequestBody,
|
||||
RequestBodyForm,
|
||||
RequestBodyFormWithAuthHeader,
|
||||
RequestBodyWithAuthHeader,
|
||||
} from '@services/utils/ts/requests'
|
||||
|
||||
export async function createActivity(data: any, chapter_id: any, org_id: any) {
|
||||
export async function createActivity(
|
||||
data: any,
|
||||
chapter_id: any,
|
||||
org_id: any,
|
||||
access_token: string
|
||||
) {
|
||||
data.content = {}
|
||||
// remove chapter_id from data
|
||||
delete data.chapterId
|
||||
|
||||
const result = await fetch(
|
||||
`${getAPIUrl()}activities/?coursechapter_id=${chapter_id}&org_id=${org_id}`,
|
||||
RequestBody('POST', data, null)
|
||||
RequestBodyWithAuthHeader('POST', data, null, access_token)
|
||||
)
|
||||
const res = await result.json()
|
||||
return res
|
||||
|
|
@ -22,7 +26,8 @@ export async function createFileActivity(
|
|||
file: File,
|
||||
type: string,
|
||||
data: any,
|
||||
chapter_id: any
|
||||
chapter_id: any,
|
||||
access_token: string
|
||||
) {
|
||||
// Send file thumbnail as form data
|
||||
const formData = new FormData()
|
||||
|
|
@ -44,7 +49,7 @@ export async function createFileActivity(
|
|||
|
||||
const result: any = await fetch(
|
||||
endpoint,
|
||||
RequestBodyForm('POST', formData, null)
|
||||
RequestBodyFormWithAuthHeader('POST', formData, null, access_token)
|
||||
)
|
||||
const res = await result.json()
|
||||
return res
|
||||
|
|
@ -53,7 +58,8 @@ export async function createFileActivity(
|
|||
export async function createExternalVideoActivity(
|
||||
data: any,
|
||||
activity: any,
|
||||
chapter_id: any
|
||||
chapter_id: any,
|
||||
access_token: string
|
||||
) {
|
||||
// add coursechapter_id to data
|
||||
data.chapter_id = chapter_id
|
||||
|
|
@ -61,25 +67,29 @@ export async function createExternalVideoActivity(
|
|||
|
||||
const result = await fetch(
|
||||
`${getAPIUrl()}activities/external_video`,
|
||||
RequestBody('POST', data, null)
|
||||
RequestBodyWithAuthHeader('POST', data, null, access_token)
|
||||
)
|
||||
const res = await result.json()
|
||||
return res
|
||||
}
|
||||
|
||||
export async function getActivity(activity_id: any, next: any) {
|
||||
export async function getActivity(
|
||||
activity_id: any,
|
||||
next: any,
|
||||
access_token: string
|
||||
) {
|
||||
const result = await fetch(
|
||||
`${getAPIUrl()}activities/${activity_id}`,
|
||||
RequestBody('GET', null, next)
|
||||
RequestBodyWithAuthHeader('GET', null, next, access_token)
|
||||
)
|
||||
const res = await result.json()
|
||||
return res
|
||||
}
|
||||
|
||||
export async function deleteActivity(activity_id: any) {
|
||||
export async function deleteActivity(activity_id: any, access_token: string) {
|
||||
const result = await fetch(
|
||||
`${getAPIUrl()}activities/${activity_id}`,
|
||||
RequestBody('DELETE', null, null)
|
||||
RequestBodyWithAuthHeader('DELETE', null, null, access_token)
|
||||
)
|
||||
const res = await result.json()
|
||||
return res
|
||||
|
|
@ -98,10 +108,14 @@ export async function getActivityWithAuthHeader(
|
|||
return res
|
||||
}
|
||||
|
||||
export async function updateActivity(data: any, activity_uuid: string) {
|
||||
export async function updateActivity(
|
||||
data: any,
|
||||
activity_uuid: string,
|
||||
access_token: string
|
||||
) {
|
||||
const result = await fetch(
|
||||
`${getAPIUrl()}activities/${activity_uuid}`,
|
||||
RequestBody('PUT', data, null)
|
||||
RequestBodyWithAuthHeader('PUT', data, null, access_token)
|
||||
)
|
||||
const res = await result.json()
|
||||
return res
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { RequestBody, errorHandling } from '@services/utils/ts/requests'
|
||||
import { RequestBody, RequestBodyWithAuthHeader, errorHandling } from '@services/utils/ts/requests'
|
||||
import { getAPIUrl } from '@services/config/config'
|
||||
|
||||
/*
|
||||
|
|
@ -6,19 +6,19 @@ import { getAPIUrl } from '@services/config/config'
|
|||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function startCourse(course_uuid: string, org_slug: string) {
|
||||
export async function startCourse(course_uuid: string, org_slug: string,access_token:any) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}trail/add_course/${course_uuid}`,
|
||||
RequestBody('POST', null, null)
|
||||
RequestBodyWithAuthHeader('POST', null, null,access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function removeCourse(course_uuid: string, org_slug: string) {
|
||||
export async function removeCourse(course_uuid: string, org_slug: string,access_token:any) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}trail/remove_course/${course_uuid}`,
|
||||
RequestBody('DELETE', null, null)
|
||||
RequestBodyWithAuthHeader('DELETE', null, null,access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
|
|
@ -27,11 +27,11 @@ export async function removeCourse(course_uuid: string, org_slug: string) {
|
|||
export async function markActivityAsComplete(
|
||||
org_slug: string,
|
||||
course_uuid: string,
|
||||
activity_uuid: string
|
||||
activity_uuid: string,access_token:any
|
||||
) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}trail/add_activity/${activity_uuid}`,
|
||||
RequestBody('POST', null, null)
|
||||
RequestBodyWithAuthHeader('POST', null, null,access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
import { OrderPayload } from '@components/Dashboard/Course/EditCourseStructure/EditCourseStructure'
|
||||
import { getAPIUrl } from '@services/config/config'
|
||||
import { RequestBody, errorHandling } from '@services/utils/ts/requests'
|
||||
import {
|
||||
RequestBodyWithAuthHeader,
|
||||
errorHandling,
|
||||
} from '@services/utils/ts/requests'
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
|
|
@ -8,28 +11,40 @@ import { RequestBody, errorHandling } from '@services/utils/ts/requests'
|
|||
*/
|
||||
|
||||
//TODO : depreciate this function
|
||||
export async function getCourseChaptersMetadata(course_uuid: any, next: any) {
|
||||
export async function getCourseChaptersMetadata(
|
||||
course_uuid: any,
|
||||
next: any,
|
||||
access_token: any
|
||||
) {
|
||||
const result = await fetch(
|
||||
`${getAPIUrl()}chapters/meta/course_${course_uuid}`,
|
||||
RequestBody('GET', null, next)
|
||||
RequestBodyWithAuthHeader('GET', null, next, access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function updateChaptersMetadata(course_uuid: any, data: any) {
|
||||
export async function updateChaptersMetadata(
|
||||
course_uuid: any,
|
||||
data: any,
|
||||
access_token: any
|
||||
) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}chapters/course/course_${course_uuid}/order`,
|
||||
RequestBody('PUT', data, null)
|
||||
RequestBodyWithAuthHeader('PUT', data, null, access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function updateChapter(coursechapter_id: any, data: any) {
|
||||
export async function updateChapter(
|
||||
coursechapter_id: any,
|
||||
data: any,
|
||||
access_token: any
|
||||
) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}chapters/${coursechapter_id}`,
|
||||
RequestBody('PUT', data, null)
|
||||
RequestBodyWithAuthHeader('PUT', data, null, access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
|
|
@ -37,30 +52,31 @@ export async function updateChapter(coursechapter_id: any, data: any) {
|
|||
|
||||
export async function updateCourseOrderStructure(
|
||||
course_uuid: any,
|
||||
data: OrderPayload
|
||||
data: OrderPayload,
|
||||
access_token: any
|
||||
) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}chapters/course/${course_uuid}/order`,
|
||||
RequestBody('PUT', data, null)
|
||||
RequestBodyWithAuthHeader('PUT', data, null, access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function createChapter(data: any) {
|
||||
export async function createChapter(data: any, access_token: any) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}chapters/`,
|
||||
RequestBody('POST', data, null)
|
||||
RequestBodyWithAuthHeader('POST', data, null, access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
export async function deleteChapter(coursechapter_id: any) {
|
||||
export async function deleteChapter(coursechapter_id: any, access_token: any) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}chapters/${coursechapter_id}`,
|
||||
RequestBody('DELETE', null, null)
|
||||
RequestBodyWithAuthHeader('DELETE', null, null, access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { getAPIUrl } from '../config/config'
|
||||
import {
|
||||
RequestBody,
|
||||
RequestBodyWithAuthHeader,
|
||||
errorHandling,
|
||||
} from '@services/utils/ts/requests'
|
||||
|
|
@ -10,36 +9,30 @@ import {
|
|||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function deleteCollection(collection_uuid: any) {
|
||||
export async function deleteCollection(
|
||||
collection_uuid: any,
|
||||
access_token: any
|
||||
) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}collections/${collection_uuid}`,
|
||||
RequestBody('DELETE', null, null)
|
||||
RequestBodyWithAuthHeader('DELETE', null, null, access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
// Create a new collection
|
||||
export async function createCollection(collection: any) {
|
||||
export async function createCollection(collection: any, access_token: any) {
|
||||
console.log(collection)
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}collections/`,
|
||||
RequestBody('POST', collection, null)
|
||||
RequestBodyWithAuthHeader('POST', collection, null, access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
// Get a colletion by id
|
||||
export async function getCollectionById(collection_uuid: any) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}collections/${collection_uuid}`,
|
||||
{ next: { revalidate: 10 } }
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function getCollectionByIdWithAuthHeader(
|
||||
export async function getCollectionById(
|
||||
collection_uuid: any,
|
||||
access_token: string,
|
||||
next: any
|
||||
|
|
@ -52,17 +45,7 @@ export async function getCollectionByIdWithAuthHeader(
|
|||
return res
|
||||
}
|
||||
|
||||
// Get collections
|
||||
// TODO : add per org filter
|
||||
export async function getOrgCollections() {
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/page/1/limit/10`, {
|
||||
next: { revalidate: 10 },
|
||||
})
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function getOrgCollectionsWithAuthHeader(
|
||||
export async function getOrgCollections(
|
||||
org_id: string,
|
||||
access_token: string,
|
||||
next: any
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { getAPIUrl } from '@services/config/config'
|
|||
import {
|
||||
RequestBody,
|
||||
RequestBodyForm,
|
||||
RequestBodyFormWithAuthHeader,
|
||||
RequestBodyWithAuthHeader,
|
||||
errorHandling,
|
||||
getResponseMetadata,
|
||||
|
|
@ -12,19 +13,10 @@ import {
|
|||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function getOrgCourses(org_id: number, next: any) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}courses/org_slug/${org_id}/page/1/limit/10`,
|
||||
RequestBody('GET', null, next)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function getOrgCoursesWithAuthHeader(
|
||||
export async function getOrgCourses(
|
||||
org_id: number,
|
||||
next: any,
|
||||
access_token: string
|
||||
access_token: any
|
||||
) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}courses/org_slug/${org_id}/page/1/limit/10`,
|
||||
|
|
@ -34,7 +26,7 @@ export async function getOrgCoursesWithAuthHeader(
|
|||
return res
|
||||
}
|
||||
|
||||
export async function getCourseMetadataWithAuthHeader(
|
||||
export async function getCourseMetadata(
|
||||
course_uuid: any,
|
||||
next: any,
|
||||
access_token: string
|
||||
|
|
@ -47,30 +39,30 @@ export async function getCourseMetadataWithAuthHeader(
|
|||
return res
|
||||
}
|
||||
|
||||
export async function updateCourse(course_uuid: any, data: any) {
|
||||
export async function updateCourse(course_uuid: any, data: any, access_token:any) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}courses/${course_uuid}`,
|
||||
RequestBody('PUT', data, null)
|
||||
RequestBodyWithAuthHeader('PUT', data, null,access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function getCourse(course_uuid: string, next: any) {
|
||||
export async function getCourse(course_uuid: string, next: any, access_token:any) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}courses/${course_uuid}`,
|
||||
RequestBody('GET', null, next)
|
||||
RequestBodyWithAuthHeader('GET', null, next,access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function updateCourseThumbnail(course_uuid: any, thumbnail: any) {
|
||||
export async function updateCourseThumbnail(course_uuid: any, thumbnail: any, access_token:any) {
|
||||
const formData = new FormData()
|
||||
formData.append('thumbnail', thumbnail)
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}courses/${course_uuid}/thumbnail`,
|
||||
RequestBodyForm('PUT', formData, null)
|
||||
RequestBodyFormWithAuthHeader('PUT', formData, null,access_token)
|
||||
)
|
||||
const res = await getResponseMetadata(result)
|
||||
return res
|
||||
|
|
@ -79,7 +71,8 @@ export async function updateCourseThumbnail(course_uuid: any, thumbnail: any) {
|
|||
export async function createNewCourse(
|
||||
org_id: string,
|
||||
course_body: any,
|
||||
thumbnail: any
|
||||
thumbnail: any,
|
||||
access_token: any
|
||||
) {
|
||||
// Send file thumbnail as form data
|
||||
const formData = new FormData()
|
||||
|
|
@ -96,16 +89,16 @@ export async function createNewCourse(
|
|||
|
||||
const result = await fetch(
|
||||
`${getAPIUrl()}courses/?org_id=${org_id}`,
|
||||
RequestBodyForm('POST', formData, null)
|
||||
RequestBodyFormWithAuthHeader('POST', formData, null, access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function deleteCourseFromBackend(course_uuid: any) {
|
||||
export async function deleteCourseFromBackend(course_uuid: any, access_token:any) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}courses/${course_uuid}`,
|
||||
RequestBody('DELETE', null, null)
|
||||
RequestBodyWithAuthHeader('DELETE', null, null,access_token)
|
||||
)
|
||||
const res = await errorHandling(result)
|
||||
return res
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
import { getAPIUrl } from '@services/config/config'
|
||||
import { RequestBody, getResponseMetadata } from '@services/utils/ts/requests'
|
||||
import {
|
||||
RequestBody,
|
||||
RequestBodyWithAuthHeader,
|
||||
getResponseMetadata,
|
||||
} from '@services/utils/ts/requests'
|
||||
|
||||
export async function createCourseUpdate(body: any) {
|
||||
export async function createCourseUpdate(body: any, access_token: string) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}courses/${body.course_uuid}/updates`,
|
||||
RequestBody('POST', body, null)
|
||||
RequestBodyWithAuthHeader('POST', body, null, access_token)
|
||||
)
|
||||
const res = await getResponseMetadata(result)
|
||||
return res
|
||||
|
|
@ -12,11 +16,12 @@ export async function createCourseUpdate(body: any) {
|
|||
|
||||
export async function deleteCourseUpdate(
|
||||
course_uuid: string,
|
||||
update_uuid: number
|
||||
update_uuid: number,
|
||||
access_token: string
|
||||
) {
|
||||
const result: any = await fetch(
|
||||
`${getAPIUrl()}courses/${course_uuid}/update/${update_uuid}`,
|
||||
RequestBody('DELETE', null, null)
|
||||
RequestBodyWithAuthHeader('DELETE', null, null, access_token)
|
||||
)
|
||||
const res = await getResponseMetadata(result)
|
||||
return res
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ export const RequestBodyWithAuthHeader = (
|
|||
headers: HeadersConfig,
|
||||
redirect: 'follow',
|
||||
credentials: 'include',
|
||||
body: data,
|
||||
body: (method === 'POST' || method === 'PUT') ? JSON.stringify(data) : null,
|
||||
// Next.js
|
||||
next: next,
|
||||
}
|
||||
|
|
@ -41,6 +41,27 @@ export const RequestBodyWithAuthHeader = (
|
|||
|
||||
export const RequestBodyForm = (method: string, data: any, next: any) => {
|
||||
let HeadersConfig = new Headers({})
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: 'follow',
|
||||
credentials: 'include',
|
||||
body: (method === 'POST' || method === 'PUT') ? JSON.stringify(data) : null,
|
||||
// Next.js
|
||||
next: next,
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
export const RequestBodyFormWithAuthHeader = (
|
||||
method: string,
|
||||
data: any,
|
||||
next: any,
|
||||
access_token: string
|
||||
) => {
|
||||
let HeadersConfig = new Headers({
|
||||
Authorization: `Bearer ${access_token}`,
|
||||
})
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
|
|
@ -53,9 +74,13 @@ export const RequestBodyForm = (method: string, data: any, next: any) => {
|
|||
return options
|
||||
}
|
||||
|
||||
export const swrFetcher = async (url: string) => {
|
||||
export const swrFetcher = async (url: string, token?: string) => {
|
||||
// Create the request options
|
||||
let HeadersConfig = new Headers({ 'Content-Type': 'application/json' })
|
||||
let HeadersConfig = new Headers(
|
||||
token
|
||||
? { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }
|
||||
: { 'Content-Type': 'application/json' }
|
||||
)
|
||||
let options: any = {
|
||||
method: 'GET',
|
||||
headers: HeadersConfig,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue