2023-02-27 22:01:48 +00:00
|
|
|
import { component$, Slot } from '@builder.io/qwik'
|
|
|
|
import { loader$ } from '@builder.io/qwik-city'
|
|
|
|
import { checkAuth } from '~/utils/checkAuth'
|
|
|
|
|
2023-03-06 16:24:39 +00:00
|
|
|
type AuthLoaderData = {
|
2023-02-27 22:01:48 +00:00
|
|
|
loginUrl: string
|
|
|
|
isAuthorized: boolean
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:24:39 +00:00
|
|
|
export const authLoader = loader$<Promise<AuthLoaderData>>(async ({ platform, request, cookie }) => {
|
2023-03-06 11:14:10 +00:00
|
|
|
const jwt = cookie.get('CF_Authorization')?.value ?? ''
|
|
|
|
const isAuthorized = await checkAuth(request, jwt, platform.ACCESS_AUTH_DOMAIN, platform.ACCESS_AUD)
|
2023-03-06 16:24:39 +00:00
|
|
|
// FIXME(sven): remove hardcoded value
|
|
|
|
const UI_CLIENT_ID = '924801be-d211-495d-8cac-e73503413af8'
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
redirect_uri: request.url,
|
|
|
|
response_type: 'code',
|
|
|
|
client_id: UI_CLIENT_ID,
|
|
|
|
scope: 'all',
|
|
|
|
})
|
|
|
|
const loginUrl = new URL('/oauth/authorize?' + params, 'https://' + platform.DOMAIN)
|
2023-02-27 22:01:48 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
isAuthorized,
|
2023-03-06 16:24:39 +00:00
|
|
|
loginUrl,
|
2023-02-27 22:01:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
export default component$(() => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Slot />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
})
|