fix: bump all backend deps by breaking cycling through not using oauth2

This commit is contained in:
Ruben Fiszel
2022-07-03 09:22:11 +02:00
parent 3b22a92947
commit e4a6378601
3 changed files with 175 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
<script lang="ts">
import { SvelteToast } from '@zerodevx/svelte-toast'
// Default toast options
const toastOptions = {
duration: 4000, // duration of progress bar tween to the `next` value
initial: 1, // initial progress bar value
next: 0, // next progress value
pausable: false, // pause progress bar tween on mouse hover
dismissable: true, // allow dismiss with close button
reversed: false, // insert new toast to bottom of stack
intro: { x: 256 }, // toast intro fly animation settings
theme: {} // css var overrides
}
</script>
<slot />
<SvelteToast options={toastOptions} />
<style>
:root {
--toastBackground: #eff6ff;
--toastBarBackground: #eff6ff;
--toastColor: #123456;
}
</style>
@@ -0,0 +1,102 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { page } from '$app/stores'
import { UserService, WorkspaceService } from '$lib/gen'
import { logout, logoutWithRedirect } from '$lib/logout'
import { superadmin, userStore, usersWorkspaceStore, workspaceStore } from '$lib/stores'
import { getUserExt, refreshSuperadmin } from '$lib/user'
import { sendUserToast } from '$lib/utils'
import { SvelteToast } from '@zerodevx/svelte-toast'
import { onMount } from 'svelte'
// Default toast options
const toastOptions = {
duration: 4000, // duration of progress bar tween to the `next` value
initial: 1, // initial progress bar value
next: 0, // next progress value
pausable: false, // pause progress bar tween on mouse hover
dismissable: true, // allow dismiss with close button
reversed: false, // insert new toast to bottom of stack
intro: { x: 256 }, // toast intro fly animation settings
theme: {} // css var overrides
}
const monacoEditorUnhandledErrors = [
'Model not found',
'Connection is disposed.',
'Connection got disposed.',
'Stopping the server timed out'
]
async function loadUser() {
try {
$usersWorkspaceStore = await WorkspaceService.listUserWorkspaces()
await refreshSuperadmin()
if ($workspaceStore) {
if ($userStore) {
sendUserToast(`Welcome back ${$userStore.username} to ${$workspaceStore}`)
} else if ($superadmin) {
console.log(
`You are a superadmin, you can go wherever you please, even at ${$workspaceStore}`
)
} else {
$userStore = await getUserExt($workspaceStore)
if (!userStore) {
throw Error('Not logged in')
}
}
} else {
goto('/user/workspaces')
let user = await UserService.globalWhoami()
sendUserToast(`Welcome back ${user.email}`)
}
} catch (e) {
console.error(e)
logoutWithRedirect($page.url.pathname)
}
}
onMount(() => {
loadUser()
window.onunhandledrejection = (event: PromiseRejectionEvent) => {
event.preventDefault()
if (event.reason?.message) {
const { message, body, status } = event.reason
// Unhandled errors from Monaco Editor don't logout the user
if (monacoEditorUnhandledErrors.includes(message)) {
return
}
if (status == '401') {
if ($page.url.pathname != '/user/login') {
sendUserToast('Logged out after a request was unauthorized', true)
logout($page.url.pathname)
}
} else {
if (body) {
sendUserToast(`${body}`, true)
} else {
sendUserToast(`${message}`, true)
}
}
} else {
console.log('Caught unhandled promise rejection without message', event)
}
}
})
</script>
<slot />
<SvelteToast options={toastOptions} />
<style>
:root {
--toastBackground: #eff6ff;
--toastBarBackground: #eff6ff;
--toastColor: #123456;
}
</style>
@@ -0,0 +1,47 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { page } from '$app/stores'
import { sendUserToast, sleep } from '$lib/utils'
import { onMount } from 'svelte'
import { UserService } from '$lib/gen'
import CenteredModal from '$lib/components/CenteredModal.svelte'
import Icon from 'svelte-awesome'
import { faSpinner } from '@fortawesome/free-solid-svg-icons'
import { userStore, workspaceStore } from '$lib/stores'
import { getUserExt } from '$lib/user'
let error = $page.url.searchParams.get('error')
let clientName = $page.params.client_name
let code = $page.url.searchParams.get('code') ?? undefined
let state = $page.url.searchParams.get('state') ?? undefined
onMount(async () => {
if (error) {
sendUserToast(`Error trying to login with ${clientName} ${error}`, true)
goto('/user/login')
} else if (code && state && clientName) {
try {
await UserService.loginWithOauth({ requestBody: { code, state }, clientName })
} catch (e) {
goto('/user/login')
sendUserToast(e.body ?? e.message, true)
return
}
if ($workspaceStore) {
$userStore = await getUserExt($workspaceStore)
goto('/')
} else {
goto('/user/workspaces')
}
} else {
sendUserToast('Missing code or state as query params', true)
goto('/user/login')
}
})
</script>
<CenteredModal title="Login from {clientName} in progress">
<div class="mx-auto w-0">
<Icon class="animate-spin" data={faSpinner} scale={2.0} />
</div>
</CenteredModal>