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

Route gurads #181

Merged
merged 6 commits into from
Jan 20, 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
78 changes: 45 additions & 33 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
import { redirect } from '@sveltejs/kit'
import { getUserDetails } from '$lib/stores/authStore'
import { Authenticate } from '$lib/authentication/authentication'
import type { Handle } from '@sveltejs/kit'

export const handle: Handle = async ({ event, resolve }) => {
const userId = event.url.searchParams.get('userId') || event.cookies.get('userId') || ''
let token = event.url.searchParams.get('token') || event.cookies.get('token') || ''
let user

if (event.locals && event.locals.user) {
user = event.locals.user.user
}

if (!token || !userId) {
return await resolve(event)
}

if (token && userId) {
if (!user) {
const response = await getUserDetails(token, userId)
if (response) {
if (response.freshJwt) {
token = response.freshJwt
}
user = response
}
}

event.cookies.set('token', token)
event.cookies.set('userId', userId)
event.locals.user = {
userId,
token,
user
}
}

return await resolve(event)
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

if (event.locals && event.locals.user) {
user = event.locals.user.user
}

if (token && userId) {
if (!user) {
const response = await getUserDetails(token, userId)
if (response) {
if (response.freshJwt) {
token = response.freshJwt
}
user = response
user.isAdmin = true
}
}

event.cookies.set('token', token)
event.cookies.set('userId', userId)
event.locals.user = {
userId,
token,
user
}
}

const user_role = (user && user.isAdmin && 'admin') || '*'

if (Authenticate(pathname, user_role) || pathname === '/browse' || pathname === '/') {
return await resolve(event)
}
throw redirect(302, '/browse')
}

export function handleError({ error }) {
console.log('error', error)
return {
message: 'Whoops something wrong!'
}
}

// const isAdminPage = /^\/admin\/(.*)/.test(route.id)
Expand Down
17 changes: 17 additions & 0 deletions src/lib/authentication/authentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import routes from './routes'

function Authenticate(pathname, user_role) {
for (const section in routes) {
for (const sub_section in routes[section]) {
const route = routes[section][sub_section]
const route_access = route['access']
const pattern = new RegExp(`^/${section}${route.path}`, 'i')
if (pattern.test(pathname + '/') && route_access.includes(user_role)) {
return true
}
}
}
return false
}

export { Authenticate }
41 changes: 41 additions & 0 deletions src/lib/authentication/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const user_contexts = ['*']
const admin_contexts = ['admin']

const routes = {
admin: [
{ path: '/admins', access: admin_contexts },
{ path: '/channels', access: admin_contexts },
{ path: '/fun-facts', access: admin_contexts },
{ path: '/fun-facts', access: admin_contexts },
{ path: '/legal', access: admin_contexts },
{ path: '/settings', access: admin_contexts },
{ path: '/users', access: admin_contexts }
],
browse: [
{ path: '/', access: user_contexts },
{ path: '/(.*)/$', access: user_contexts }
],
channel: [
{ path: '/', access: user_contexts },
{ path: '/(.*)/$', access: user_contexts }
],
profile: [
{ path: '/', access: admin_contexts },
{ path: '/(.*)/$', access: admin_contexts }
],
search: [
{ path: '/', access: user_contexts },
{ path: '/(.*)/', access: user_contexts }
],
maintenance: [{ path: '/', access: user_contexts }],
settings: [{ path: '/', access: user_contexts }],
premium: [{ path: '/', access: admin_contexts }],
careers: [{ path: '/', access: user_contexts }],
contact: [{ path: '/', access: user_contexts }],
videos: [{ path: '/', access: user_contexts }],
legal: [{ path: '/', access: user_contexts }],
logout: [{ path: '/', access: admin_contexts }],
'creator-space': [{ path: '/', access: user_contexts }]
}

export default routes
24 changes: 17 additions & 7 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import '$lib/assets/styles/tailwind-output.css'
// @ts-ignore
import NProgress from 'nprogress'
import { goto } from '$app/navigation'
import { browser } from '$app/environment'
import { navigating } from '$app/stores'
import { currentUser } from '$lib/stores/authStore'
Expand Down Expand Up @@ -60,6 +61,13 @@
}
}
}

function logout() {
setTimeout(() => {
$currentUser = null
}, 500)
goto('/logout')
}
</script>

<svelte:head>
Expand Down Expand Up @@ -168,12 +176,14 @@
Mint <span class="badge">New</span>
</a>
</li>
<li>
<a href="/premium" class="text-pink-500">
<IconDrawerPremium />
Premium <span class="badge">New</span>
</a>
</li>
{#if $currentUser}
<li>
<a href="/premium" class="text-pink-500">
<IconDrawerPremium />
Premium <span class="badge">New</span>
</a>
</li>
{/if}
<li>
<a href="/careers">
<IconDrawerCareers />
Expand Down Expand Up @@ -202,7 +212,7 @@
</li>
{#if $currentUser}
<li>
<button>
<button on:click={logout}>
<IconDrawerLogOut />
Log Out</button>
</li>
Expand Down
10 changes: 10 additions & 0 deletions src/routes/admin/admins/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'

export const load = (async ({ params }) => {
// const post = null
// if (post) {
// return post
// }
// throw error(404, 'Not found')
}) satisfies PageServerLoad
10 changes: 10 additions & 0 deletions src/routes/admin/channels/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'

export const load = (async ({ params }) => {
// const post = null
// if (post) {
// return post
// }
// throw error(404, 'Not found')
}) satisfies PageServerLoad
10 changes: 10 additions & 0 deletions src/routes/admin/fun-facts/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'

export const load = (async ({ params }) => {
// const post = null
// if (post) {
// return post
// }
// throw error(404, 'Not found')
}) satisfies PageServerLoad
10 changes: 10 additions & 0 deletions src/routes/admin/legal/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'

export const load = (async ({ params }) => {
// const post = null
// if (post) {
// return post
// }
// throw error(404, 'Not found')
}) satisfies PageServerLoad
10 changes: 10 additions & 0 deletions src/routes/admin/settings/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'

export const load = (async ({ params }) => {
// const post = null
// if (post) {
// return post
// }
// throw error(404, 'Not found')
}) satisfies PageServerLoad
10 changes: 10 additions & 0 deletions src/routes/admin/users/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'

export const load = (async ({ params }) => {
// const post = null
// if (post) {
// return post
// }
// throw error(404, 'Not found')
}) satisfies PageServerLoad
10 changes: 10 additions & 0 deletions src/routes/careers/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'

export const load = (async ({ params }) => {
// const post = null
// if (post) {
// return post
// }
// throw error(404, 'Not found')
}) satisfies PageServerLoad
Empty file.
1 change: 0 additions & 1 deletion src/routes/logout/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { PageServerLoad } from './$types'
export const load: PageServerLoad = async ({ cookies }) => {
// eat the cookie
const cookieItem = ['token', 'userId']

cookieItem.forEach((item) => {
cookies.set(item, '', {
path: '/',
Expand Down
Empty file.