From df1b38a7b5ad1a1d378419d6e3946dc47caa8879 Mon Sep 17 00:00:00 2001 From: Saiful Date: Thu, 26 Jan 2023 08:25:30 +0600 Subject: [PATCH 01/98] wip --- src/app.d.ts | 23 +-- src/hooks.server.ts | 162 ++++++++++-------- .../components/MainDrawer/LoginPrompt.svelte | 10 +- src/routes/+layout.svelte | 2 + 4 files changed, 108 insertions(+), 89 deletions(-) diff --git a/src/app.d.ts b/src/app.d.ts index ce033eca..aadc3e49 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -3,16 +3,17 @@ // and what to do when importing types declare namespace App { - interface Locals { - user: { - token?: string - userId?: string - user?: object - } - } - // interface PageData {} - // interface Error {} - // interface Platform {} + interface Locals { + user: { + token?: string + userId?: string + user?: object + isBanned?: boolean + } + } + // interface PageData {} + // interface Error {} + // interface Platform {} } -declare const __VERSION__ : string \ No newline at end of file +declare const __VERSION__: string diff --git a/src/hooks.server.ts b/src/hooks.server.ts index b0a8c16d..de8656c7 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -7,84 +7,96 @@ import type { Handle, HandleFetch } from '@sveltejs/kit' import { env } from '$env/dynamic/public' export const handle: Handle = async ({ event, resolve }) => { - const pathname = event.url.pathname - const userId = event.url.searchParams.get('userId') || event.cookies.get('userId') || '' - let token = event.url.searchParams.get('token') || event.cookies.get('token') || '' - let user = get(currentUser), - role = get(userRole) - - if (token && userId) { - if (!user) { - const response = await getUserDetails(token, userId) - if (response) { - if (response.freshJwt) { - token = response.freshJwt - } - user = response - currentUser.set(user) - } - } - - if (!role) { - try { - const headers: any = { - userId: userId - } - if (env.PUBLIC_CROSS_ORIGIN === 'false') { - headers['authorization'] = token - } else { - 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 - })?.name - - userRole.set(role) - } - } - } catch (e) { - console.log('something wrong', e) - } - } - - if (pathname === '/') { - event.cookies.set('token', token, { - path: '/', - maxAge: 60 * 60 * 24 * 30 - }) - event.cookies.set('userId', userId, { - path: '/', - maxAge: 60 * 60 * 24 * 30 - }) - } - - event.locals.user = { - userId, - token, - user - } - } - - if ( - Authenticate({ pathname, user_role: role || 'user' }) || - pathname === '/browse' || - pathname === '/' - ) { - return await resolve(event) - } - throw redirect(302, '/browse') + const pathname = event.url.pathname + const userId = event.url.searchParams.get('userId') || event.cookies.get('userId') || '' + let token = event.url.searchParams.get('token') || event.cookies.get('token') || '' + let user = get(currentUser), + role = get(userRole), + isBanned = false + + if (token && userId) { + if (!user) { + const response = await getUserDetails(token, userId) + if (response) { + if (response.freshJwt) { + token = response.freshJwt + } + user = response + currentUser.set(user) + } + } + + if (!role) { + try { + const headers: any = { + userId: userId + } + if (env.PUBLIC_CROSS_ORIGIN === 'false') { + headers['authorization'] = token + } else { + 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 + })?.name + + userRole.set(role) + } + } + } catch (e) { + console.log('something wrong', e) + } + } + + if (user && user.isBanned) { + isBanned = true + userRole.set('user') + } + + if (pathname === '/') { + event.cookies.set('token', token, { + path: '/', + maxAge: 60 * 60 * 24 * 30 + }) + event.cookies.set('userId', userId, { + path: '/', + maxAge: 60 * 60 * 24 * 30 + }) + } + + console.log('isBanned', isBanned) + + event.locals.user = { + userId, + token, + user, + isBanned + } + } + + console.log('role', role) + return await resolve(event) + + // if ( + // Authenticate({ pathname, user_role: role || 'user' }) || + // pathname === '/browse' || + // pathname === '/' + // ) { + // return await resolve(event) + // } + // throw redirect(302, '/browse') } export function handleError({ error }: { error: any }) { - console.log('error', error) - return { - message: 'Whoops something wrong!' - } + console.log('error', error) + return { + message: 'Whoops something wrong!' + } } //TODO: fix global handleFetch diff --git a/src/lib/components/MainDrawer/LoginPrompt.svelte b/src/lib/components/MainDrawer/LoginPrompt.svelte index 631980ea..3533d8f3 100644 --- a/src/lib/components/MainDrawer/LoginPrompt.svelte +++ b/src/lib/components/MainDrawer/LoginPrompt.svelte @@ -11,10 +11,14 @@ Accept: '*/*', 'x-api-key': env.PUBLIC_X_API_KEY } - }).then(async (response) => { - const res = await response.json() - window.location.replace(res) }) + .then(async (response) => { + const res = await response.json() + window.location.replace(res) + }) + .catch((err) => { + console.log('Something wrong in Oauth') + }) } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index a3835c38..d874e7ec 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -55,6 +55,8 @@ $: data.user, storeUserData() + $: console.log('test data', data.user) + function storeUserData() { if (browser) { if (data?.user?.user) { From d6586b2b3c88300709928e275f8d436f4a789735 Mon Sep 17 00:00:00 2001 From: Saiful Date: Thu, 26 Jan 2023 09:50:33 +0600 Subject: [PATCH 02/98] fix banned user --- src/hooks.server.ts | 19 ++++++++----------- src/routes/+layout.svelte | 13 +++++++++++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index de8656c7..d552574f 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -79,17 +79,14 @@ export const handle: Handle = async ({ event, resolve }) => { } } - console.log('role', role) - return await resolve(event) - - // if ( - // Authenticate({ pathname, user_role: role || 'user' }) || - // pathname === '/browse' || - // pathname === '/' - // ) { - // return await resolve(event) - // } - // throw redirect(302, '/browse') + if ( + Authenticate({ pathname, user_role: role || 'user' }) || + pathname === '/browse' || + pathname === '/' + ) { + return await resolve(event) + } + throw redirect(302, '/browse') } export function handleError({ error }: { error: any }) { diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index d874e7ec..207b6a94 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -55,8 +55,6 @@ $: data.user, storeUserData() - $: console.log('test data', data.user) - function storeUserData() { if (browser) { if (data?.user?.user) { @@ -85,6 +83,17 @@ + + {#if data && data.user && data.user.isBanned} +
+
+
+

Your account is banned

+
+
+
+ {/if} + From 22f52a5ca2a9c63bbb91d049097217749e0a3a17 Mon Sep 17 00:00:00 2001 From: Saiful Date: Thu, 26 Jan 2023 10:43:54 +0600 Subject: [PATCH 03/98] fix maintenance mode --- src/hooks.server.ts | 14 ++++++-- src/lib/stores/remoteConfigStore.ts | 51 ++++++++++++++--------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index d552574f..24479195 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -2,6 +2,7 @@ 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 { Authenticate } from '$lib/authentication/authentication' import type { Handle, HandleFetch } from '@sveltejs/kit' import { env } from '$env/dynamic/public' @@ -14,6 +15,9 @@ export const handle: Handle = async ({ event, resolve }) => { role = get(userRole), isBanned = false + await getRemoteConfigs() + const maintenance_mode = get(isMaintenanceModeEnabled) || false + if (token && userId) { if (!user) { const response = await getUserDetails(token, userId) @@ -69,8 +73,6 @@ export const handle: Handle = async ({ event, resolve }) => { }) } - console.log('isBanned', isBanned) - event.locals.user = { userId, token, @@ -79,12 +81,18 @@ export const handle: Handle = async ({ event, resolve }) => { } } + console.log('pathname', pathname) + if ( Authenticate({ pathname, user_role: role || 'user' }) || pathname === '/browse' || pathname === '/' ) { - return await resolve(event) + if (maintenance_mode && !['/contact', '/legal', '/maintenance'].includes(pathname) && !user) { + throw redirect(302, '/maintenance') + } else { + return await resolve(event) + } } throw redirect(302, '/browse') } diff --git a/src/lib/stores/remoteConfigStore.ts b/src/lib/stores/remoteConfigStore.ts index a5679230..048c07c4 100644 --- a/src/lib/stores/remoteConfigStore.ts +++ b/src/lib/stores/remoteConfigStore.ts @@ -1,5 +1,5 @@ import { env } from '$env/dynamic/public' -import { writable, type Writable } from "svelte/store" +import { writable, type Writable } from 'svelte/store' export const isMaintenanceModeEnabled: Writable = writable(false) export const isFeatureVideoResponsesEnabled: Writable = writable(false) @@ -7,37 +7,34 @@ export const isFeatureGroupChatEnabled: Writable = writable(false) export const isFeatureMintPageEnabled: Writable = writable(false) export const isFeaturePremiumPageEnabled: Writable = writable(false) -async function createRemoteConfig({ flagKey, flagValue }: { flagKey: string, flagValue: string }) { - return await fetch(`${env.PUBLIC_API_URL}/remote-config`, { - method: 'PUT', - body: JSON.stringify({ flagKey, flagValue }) - }).then(response => response.json()) +async function createRemoteConfig({ flagKey, flagValue }: { flagKey: string; flagValue: string }) { + return await fetch(`${env.PUBLIC_API_URL}/remote-config`, { + method: 'PUT', + body: JSON.stringify({ flagKey, flagValue }) + }).then((response) => response.json()) } async function getRemoteConfigs() { - return await fetch(`${env.PUBLIC_API_URL}/remote-config`, { - method: 'GET' - }).then(async response => { - const remoteConfigs = await response.json() - 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) - }) - }) + return await fetch(`${env.PUBLIC_API_URL}/remote-configs`, { + method: 'GET' + }).then(async (response) => { + const remoteConfigs = await response.json() + 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) + }) + }) } async function getRemoteConfigByKey({ flagKey }: { flagKey: string }) { - return await fetch(`${env.PUBLIC_API_URL}/remote-config?flagKey=${flagKey}`, { - method: 'GET' - }).then(response => response.json()) + return await fetch(`${env.PUBLIC_API_URL}/remote-config?flagKey=${flagKey}`, { + method: 'GET' + }).then((response) => response.json()) } - -export { - createRemoteConfig, - getRemoteConfigs, - getRemoteConfigByKey -} \ No newline at end of file +export { createRemoteConfig, getRemoteConfigs, getRemoteConfigByKey } From 82ea3178eb2b6cd625308fbba0467a2cf82cbdd0 Mon Sep 17 00:00:00 2001 From: Saiful Date: Thu, 26 Jan 2023 11:43:16 +0600 Subject: [PATCH 04/98] fixed ban user to logout --- src/app.d.ts | 2 +- src/hooks.server.ts | 39 +++++++++++++++++++++++++----------- src/routes/+layout.server.ts | 4 +++- src/routes/+layout.svelte | 7 ++++--- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/app.d.ts b/src/app.d.ts index aadc3e49..72257e05 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -8,8 +8,8 @@ declare namespace App { token?: string userId?: string user?: object - isBanned?: boolean } + isBanned?: boolean } // interface PageData {} // interface Error {} diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 24479195..664d1836 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -57,11 +57,6 @@ export const handle: Handle = async ({ event, resolve }) => { } } - if (user && user.isBanned) { - isBanned = true - userRole.set('user') - } - if (pathname === '/') { event.cookies.set('token', token, { path: '/', @@ -73,15 +68,31 @@ export const handle: Handle = async ({ event, resolve }) => { }) } - event.locals.user = { - userId, - token, - user, - isBanned + event.locals = { + user: { + userId, + token, + user + } } } - console.log('pathname', pathname) + if (user && user.isBanned) { + isBanned = true + + const cookieItem = ['token', 'userId'] + cookieItem.forEach((item) => { + event.cookies.set(item, '', { + path: '/', + expires: new Date(0) + }) + }) + + currentUser.set(null) + userRole.set('user') + + event.locals['isBanned'] = isBanned + } if ( Authenticate({ pathname, user_role: role || 'user' }) || @@ -89,7 +100,11 @@ export const handle: Handle = async ({ event, resolve }) => { pathname === '/' ) { if (maintenance_mode && !['/contact', '/legal', '/maintenance'].includes(pathname) && !user) { - throw redirect(302, '/maintenance') + if (pathname === '/maintenance') { + return await resolve(event) + } else { + throw redirect(302, '/maintenance') + } } else { return await resolve(event) } diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index 74991e1b..f9468615 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -1,7 +1,9 @@ import type { LayoutServerLoad } from './$types' export const load: LayoutServerLoad = async ({ locals }) => { + console.log('locals', locals) return { - user: locals.user + user: locals.user, + isBanned: locals.isBanned } } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 2c1c162c..73c04b30 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -48,6 +48,7 @@ NProgress.start() } if (!$navigating) { + storeUserData() NProgress.done() } } @@ -56,12 +57,12 @@ let nav_drawer: HTMLInputElement - $: data.user, storeUserData() - function storeUserData() { if (browser) { if (data?.user?.user) { $currentUser = data.user.user + } else { + $currentUser = null } } } @@ -87,7 +88,7 @@ - {#if data && data.user && data.user.isBanned} + {#if data && data.isBanned}
From 33b5de72f94be77a0563024477fa3f48fd18f7d2 Mon Sep 17 00:00:00 2001 From: Gagan Suie Date: Fri, 27 Jan 2023 00:25:35 -0600 Subject: [PATCH 05/98] Fix: removed onhover of profile more icon --- src/routes/profile/[customUsername]/+page.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/profile/[customUsername]/+page.svelte b/src/routes/profile/[customUsername]/+page.svelte index 7e8ff31e..b23cf0a5 100644 --- a/src/routes/profile/[customUsername]/+page.svelte +++ b/src/routes/profile/[customUsername]/+page.svelte @@ -134,7 +134,7 @@ -