Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix banned and maintanance auth #220

Merged
merged 5 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ declare namespace App {
token?: string
userId?: string
user?: object
isBanned?: boolean
}
isBanned?: boolean
}
// interface PageData {}
// interface Error {}
Expand Down
49 changes: 36 additions & 13 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down Expand Up @@ -53,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: '/',
Expand All @@ -69,22 +68,46 @@ export const handle: Handle = async ({ event, resolve }) => {
})
}

console.log('isBanned', isBanned)

event.locals.user = {
userId,
token,
user,
isBanned
event.locals = {
user: {
userId,
token,
user
}
}
}

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' }) ||
pathname === '/browse' ||
pathname === '/'
) {
return await resolve(event)
if (maintenance_mode && !['/contact', '/legal', '/maintenance'].includes(pathname) && !user) {
if (pathname === '/maintenance') {
return await resolve(event)
} else {
throw redirect(302, '/maintenance')
}
} else {
return await resolve(event)
}
}
throw redirect(302, '/browse')
}
Expand Down
51 changes: 24 additions & 27 deletions src/lib/stores/remoteConfigStore.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
import { env } from '$env/dynamic/public'
import { writable, type Writable } from "svelte/store"
import { writable, type Writable } from 'svelte/store'

export const isMaintenanceModeEnabled: Writable<boolean> = writable(false)
export const isFeatureVideoResponsesEnabled: Writable<boolean> = writable(false)
export const isFeatureGroupChatEnabled: Writable<boolean> = writable(false)
export const isFeatureMintPageEnabled: Writable<boolean> = writable(false)
export const isFeaturePremiumPageEnabled: Writable<boolean> = 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
}
export { createRemoteConfig, getRemoteConfigs, getRemoteConfigByKey }
4 changes: 3 additions & 1 deletion src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 4 additions & 3 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
NProgress.start()
}
if (!$navigating) {
storeUserData()
NProgress.done()
}
}
Expand All @@ -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
}
}
}
Expand All @@ -87,7 +88,7 @@
<label for="my-drawer-2" class="btn btn-ghost normal-case text-xl drawer-button lg:hidden"
>Mage</label>

{#if data && data.user && data.user.isBanned}
{#if data && data.isBanned}
<div class="alert alert-error shadow-lg">
<div>
<div class="font-bold text-white ">
Expand Down