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 the browse url #158

Merged
merged 1 commit into from
Jan 18, 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
72 changes: 33 additions & 39 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,41 @@ import { getUserDetails } from '$lib/stores/authStore'
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.user
}
}

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

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

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

return await resolve(event)
}


// const isAdminPage = /^\/admin\/(.*)/.test(route.id)
// const isProfilePage = /^\/profile\/(.*)/.test(route.id)
// const isPremiumPage = /^\/premium\/(.*)/.test(route.id)
Expand All @@ -60,11 +59,6 @@ export const handle: Handle = async ({ event, resolve }) => {
// }
// }






// import { remoteConfigStore } from '$lib/stores/remoteConfigStore'
// import type { Handle, HandleServerError, RequestEvent } from '@sveltejs/kit'
// import type { MaybePromise } from '@sveltejs/kit/types/private'
Expand Down
13 changes: 6 additions & 7 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,13 @@
Admin
</a>
</li>
{:else}
<li>
<a href="/browse">
<IconDrawerHome />
Browse
</a>
</li>
{/if}
<li>
<a href="/browse">
<IconDrawerHome />
Browse
</a>
</li>
<li>
<a href="">
<IconDrawerCommunity />
Expand Down
6 changes: 3 additions & 3 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { redirect } from '@sveltejs/kit'

/** @type {import('./$types').LayoutServerLoad} */
export function load({ locals }: { locals: any }) {
if (!locals.user) {
throw redirect(307, '/browse')
}
if (!locals.user || locals.user) {
throw redirect(307, '/browse')
}
}