-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): handle code exchange server-side
- Loading branch information
1 parent
37ca829
commit e8b2f36
Showing
2 changed files
with
31 additions
and
66 deletions.
There are no files selected for viewing
43 changes: 1 addition & 42 deletions
43
projects/client/src/lib/features/auth/components/AuthProvider.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,16 @@ | ||
<script lang="ts"> | ||
import { replaceState } from "$app/navigation"; | ||
import { page } from "$app/stores"; | ||
import { onMount } from "svelte"; | ||
import { AuthEndpoint } from "../AuthEndpoint"; | ||
import type { ClientAuthResponse } from "../models/SerializedAuthResponse"; | ||
import { provisionAuth, useAuth } from "../stores/useAuth"; | ||
import { provisionAuth } from "../stores/useAuth"; | ||
type AuthProviderProps = { | ||
url: string; | ||
isAuthorized: boolean; | ||
} & ChildrenProps; | ||
const { isAuthorized, url, children }: AuthProviderProps = $props(); | ||
provisionAuth({ | ||
isAuthorized, | ||
url, | ||
}); | ||
const store = useAuth(); | ||
function requestAuthStatus(code: string) { | ||
return fetch(AuthEndpoint.Exchange, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ code }), | ||
}).then((res) => res.json() as Promise<ClientAuthResponse>); | ||
} | ||
onMount(async () => { | ||
const url = new URL($page.url.href); | ||
const queryParams = url.searchParams; | ||
const code = queryParams.get("code"); | ||
if (!code) { | ||
return; | ||
} | ||
const { isAuthorized } = await requestAuthStatus(code); | ||
store.isAuthorized.set(isAuthorized); | ||
if (!isAuthorized) { | ||
return; | ||
} | ||
queryParams.delete("code"); | ||
replaceState( | ||
`${url.pathname}${queryParams.toString() ? "?" + queryParams.toString() : ""}`, | ||
{}, | ||
); | ||
}); | ||
</script> | ||
|
||
{@render children()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters