Files
windmill/frontend/src/lib/logout.ts
T
Ruben Fiszel cddec6469e fix(backend): tighten security around cookies to avoid csrf (#859)
* fix(backend): tighten security around cookies to avoid csrf

* everything httponly

* sqlx

* also handle 404

* shoter Windmill v message

* fix chart
2022-11-05 21:21:13 +01:00

29 lines
818 B
TypeScript

import { goto } from '$app/navigation'
import { UserService } from '$lib/gen'
import { clearStores } from './stores.js'
import { sendUserToast } from './utils.js'
export async function logoutWithRedirect(rd?: string): Promise<void> {
await clearUser()
if (rd?.split('?')[0] != '/user/login') {
const error = document.cookie.includes('token')
? `error=${encodeURIComponent('You have been logged out because your session has expired.')}&`
: ''
goto(`/user/login?${error}${rd ? 'rd=' + encodeURIComponent(rd) : ''}`, { replaceState: true })
}
}
export async function logout(): Promise<void> {
await clearUser()
goto(`/user/login`)
sendUserToast('you have been logged out')
}
async function clearUser() {
try {
clearStores()
await UserService.logout()
} catch (error) {
console.error(error)
}
}