diff --git a/src/app.html b/src/app.html index aca596b8..882beccd 100644 --- a/src/app.html +++ b/src/app.html @@ -2,15 +2,15 @@ - - - - %sveltekit.head% + + + + %sveltekit.head% -
%sveltekit.body%
+ class="scrollbar-thin scrollbar-thumb-[#aeaeae] scrollbar-track-[#25252A] overflow-y-scroll scrollbar-thumb-rounded"> +
%sveltekit.body%
\ No newline at end of file diff --git a/src/hooks.server.ts b/src/hooks.server.ts index c3161c39..0b4b2e37 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,39 +1,48 @@ -import { redirect } from '@sveltejs/kit' -import { get } from 'svelte/store' -import { getUserDetails, userRole, currentUser } from '$lib/stores/authStore' -import { getUserRole, getRoles } from '$lib/stores/adminStore' -import { getRemoteConfigs, isMaintenanceModeEnabled } from '$lib/stores/remoteConfigStore' +import { redirect, type HandleFetch } from '@sveltejs/kit' +import { get as getWritableVal } from 'svelte/store' +import { + isMaintenanceModeEnabled, + isFeatureVideoResponsesEnabled, + isFeatureGroupChatEnabled, + isFeatureMintPageEnabled, + isFeaturePremiumPageEnabled +} from '$lib/stores/remoteConfigStore' import { Authenticate } from '$lib/authentication/authentication' import type { Handle } from '@sveltejs/kit' import { env } from '$env/dynamic/public' -import { - isChannelPage, - userId as userIdWritable, - token as tokenWritable -} from '$lib/stores/helperStore' +import { get } from '$lib/api' +import { current_user, user_role } from '$lib/stores/authStore' export const handle: Handle = async ({ event, resolve }) => { const pathname = event.url.pathname const userId = event.url.searchParams.get('userId') || event.cookies.get('userId') || '' - userIdWritable.set(userId) let token = event.url.searchParams.get('token') || event.cookies.get('token') || '' - tokenWritable.set(token) - let user = get(currentUser), - role = get(userRole), + + let user: any = event.locals.user?.user || '', isBanned = false + const role = getWritableVal(user_role) + + const remoteConfigs = await get('/remote-configs', { userId, token }) + remoteConfigs.map((config: { flagKey: string; flagValue: boolean }) => { + if (config.flagKey === 'maintenance-mode') isMaintenanceModeEnabled.set(config.flagValue) + if (config.flagKey === 'feature-video-responses') + isFeatureVideoResponsesEnabled.set(config.flagValue) + if (config.flagKey === 'feature-group-chat') isFeatureGroupChatEnabled.set(config.flagValue) + if (config.flagKey === 'feature-mint-page') isFeatureMintPageEnabled.set(config.flagValue) + if (config.flagKey === 'feature-premium-page') isFeaturePremiumPageEnabled.set(config.flagValue) + }) - await getRemoteConfigs() - const maintenance_mode = get(isMaintenanceModeEnabled) || false + const maintenance_mode = getWritableVal(isMaintenanceModeEnabled) || false if (token && userId) { if (!user) { - const response = await getUserDetails(token, userId) + const response = await get('/auth/me', { userId, token }) if (response) { if (response.freshJwt) { token = response.freshJwt } - user = response - currentUser.set(user) + user = response.user + current_user.set(user) } } @@ -48,19 +57,19 @@ export const handle: Handle = async ({ event, resolve }) => { headers['x-api-key'] = env.PUBLIC_API_KEY } - const all_roles = await getRoles(true, headers) - if (Array.isArray(all_roles)) { - const get_role = await getUserRole(true, headers) - if (get_role && get_role.role) { - role = all_roles.find((item) => { - return item._id == get_role.role + const allRoles = await get('/roles', headers) + if (Array.isArray(allRoles)) { + const userRole = await get('/roles/role-mapping', headers) + if (userRole && userRole.role) { + const usersRoleName = allRoles.find((item) => { + return item._id == userRole.role })?.name - userRole.set(role) + user_role.set(usersRoleName) } } - } catch (e) { - console.log('something wrong', e) + } catch (err) { + console.log('something went wrong', err) } } @@ -86,7 +95,6 @@ export const handle: Handle = async ({ event, resolve }) => { if (user && user.isBanned) { isBanned = true - const cookieItem = ['token', 'userId'] cookieItem.forEach((item) => { event.cookies.set(item, '', { @@ -94,10 +102,7 @@ export const handle: Handle = async ({ event, resolve }) => { expires: new Date(0) }) }) - - currentUser.set(null) - userRole.set('user') - + user_role.set('user') event.locals['isBanned'] = isBanned } @@ -113,23 +118,38 @@ export const handle: Handle = async ({ event, resolve }) => { throw redirect(302, '/maintenance') } } else { - console.log('pathname', pathname) - if (pathname.includes('/channel')) { - isChannelPage.set(false) - } else { - isChannelPage.set(false) - } return await resolve(event) } } throw redirect(302, '/browse') } -export function handleError({ error }: { error: any }) { +export const handleError = ({ error }: { error: any }) => { console.log('error', error) // example integration with https://sentry.io/ // Sentry.captureException(error, { event, errorId }); return { - message: 'Whoops something wrong!' + message: 'Whoops something went wrong!' } } + +export const handleFetch = (async ({ + event, + request, + fetch +}: { + event: any + request: any + fetch: any +}) => { + if (request.url.startsWith(env.PUBLIC_API_URL)) { + request.headers['userId'] = event.locals.user.userId + if (env.PUBLIC_CROSS_ORIGIN === 'false') { + request.headers['authorization'] = event.locals.user.token + } else { + request.headers['x-api-key'] = env.PUBLIC_X_API_KEY + } + } + + return fetch(request) +}) satisfies HandleFetch diff --git a/src/lib/api.ts b/src/lib/api.ts new file mode 100644 index 00000000..cc9fe4ca --- /dev/null +++ b/src/lib/api.ts @@ -0,0 +1,58 @@ +import { error } from '@sveltejs/kit' +import { env } from '$env/dynamic/public' + +const base = env.PUBLIC_API_URL + +async function send({ + method, + path, + data, + headers +}: { + method: string + path: string + data?: any + headers?: any +}) { + const opts: any = { method, headers: {} } + + if (data) { + opts.headers['Content-Type'] = 'application/json' + opts.body = JSON.stringify(data) + } + + // if headers sent manually + if (headers && headers.userId) { + opts.headers['userId'] = headers.userId + } + + if (env.PUBLIC_CROSS_ORIGIN === 'false') { + opts.headers['authorization'] = headers.token + } else { + opts.headers['x-api-key'] = env.PUBLIC_X_API_KEY + } + + const res = await fetch(`${base}/${path}`, opts) + if (res.ok || res.status === 422) { + const text = await res.text() + return text ? JSON.parse(text) : {} + } + + return { error: res.status } +} + +export function get(path: string, headers?: any) { + return send({ method: 'GET', path, headers }) +} + +export function del(path: string) { + return send({ method: 'DELETE', path }) +} + +export function post(path: string, data: any) { + return send({ method: 'POST', path, data }) +} + +export function put(path: string, data: any = {}) { + return send({ method: 'PUT', path, data }) +} diff --git a/src/lib/components/Browse/AddCategory.svelte b/src/lib/components/Browse/AddCategoryDrawer.svelte similarity index 100% rename from src/lib/components/Browse/AddCategory.svelte rename to src/lib/components/Browse/AddCategoryDrawer.svelte diff --git a/src/lib/components/Browse/Carousel.svelte b/src/lib/components/Browse/Carousel.svelte deleted file mode 100644 index 7e2377df..00000000 --- a/src/lib/components/Browse/Carousel.svelte +++ /dev/null @@ -1,110 +0,0 @@ - - -
- - -
- -
- -