Skip to content

Commit

Permalink
remember redirect on login & improve sentry compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Aug 12, 2024
1 parent 2522f47 commit 3b770bb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
5 changes: 4 additions & 1 deletion components/Nav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<section class="flex gap-4 ml-auto">
<ThemeButton />
<ProfileIcon v-if="account" :account="account" />
<NuxtLink v-else to="/login" class="px-2"> Login </NuxtLink>
<NuxtLink v-else :to="loginLink" class="px-2"> Login </NuxtLink>
</section>
</nav>
</template>

<script lang="ts" setup>
import { BookOpenIcon, FlagIcon, MapIcon, MapPinIcon } from '@heroicons/vue/24/solid'
import type { RouteLocationRaw } from 'vue-router'
const route = useRoute()
Expand All @@ -26,6 +27,8 @@ const links = ref([
const active = computed(() => [...links.value].reverse().find(it => route.path.startsWith(it.to))?.to)
const loginLink = computed((): RouteLocationRaw => ({ name: 'login', query: { from: route.path } }))
const { account } = useSession()
</script>

Expand Down
11 changes: 11 additions & 0 deletions composables/auth.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { MeDocument, Permission, type SelfFragment } from '~/graphql/generated'

function refresh() {
const apollo = useApolloClient()
apollo.client.refetchQueries({ include: 'all' })
}

export function login(token: string) {
const cookie = useToken()
cookie.value = token

refresh()
}

export function logout() {
const cookie = useToken()
cookie.value = null

if (import.meta.client) {
window.location.reload()
}
}

interface Session {
Expand Down
8 changes: 5 additions & 3 deletions pages/login.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div id="buttons">
<a href="/auth/discord">
<a :href="`/auth/discord?from=${redirectTo}`">
<StyledButton id="discord"> Login via Discord</StyledButton>
</a>
<div v-if="result?.settings.development">
Expand Down Expand Up @@ -30,10 +30,12 @@ const { query } = useRoute()
const router = useRouter()
const { result } = useQuery(ApiSettingsDocument)
const redirectTo = computed(() => query.from?.toString() || '/me')
onMounted(() => {
if (query.token && typeof query.token === 'string') {
login(query.token)
router.replace('/me')
router.replace(redirectTo.value)
}
})
Expand All @@ -44,7 +46,7 @@ async function seededLogin(values: ImpersonateMutationVariables) {
const token = reponse?.data?.impersonate?.token
if (token) {
login(token)
router.push('/me')
router.push(redirectTo.value)
}
}
</script>
Expand Down
13 changes: 10 additions & 3 deletions plugins/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { browserTracingIntegration, init, replayIntegration } from '@sentry/vue'

export default defineNuxtPlugin({
setup: nuxt => {
setup: ({ vueApp }) => {
const config = useRuntimeConfig()
const router = useRouter()

if (!config.public.sentryDsn) {
// eslint-disable-next-line no-console
console.warn('Sentry DSN not set, skipping Sentry initialization')
return
}

init({
app: nuxt.vueApp,
app: vueApp,
dsn: config.public.sentryDsn,
environment: config.public.sentryEnvironment,
integrations: [browserTracingIntegration(), replayIntegration()],
integrations: [replayIntegration(), browserTracingIntegration({ router })],
tracesSampleRate: 1.0,
tracePropagationTargets: ['localhost'],
replaysSessionSampleRate: 1.0,
Expand Down
20 changes: 20 additions & 0 deletions server/plugins/sentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { captureException, close } from '@sentry/vue'
import { H3Error } from 'h3'

const IGNORED_STATUS_CODES = [404, 422]

export default defineNitroPlugin(nitroApp => {
nitroApp.hooks.hook('error', error => {
if (error instanceof H3Error) {
if (IGNORED_STATUS_CODES.includes(error.statusCode)) {
return
}
}

captureException(error)
})

nitroApp.hooks.hookOnce('close', async () => {
await close(2000)
})
})

0 comments on commit 3b770bb

Please sign in to comment.