-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
178 additions
and
88 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 +1,15 @@ | ||
import { HoudiniClient } from '$houdini'; | ||
import { HoudiniClient, getClientSession } from '$houdini'; | ||
import { houdiniPlugin as authPlugin } from '$lib/oauth/auth.svelte'; | ||
|
||
export default new HoudiniClient({ | ||
url: `${import.meta.env.VITE_BACKEND_URL ?? ''}/graphql` | ||
|
||
// uncomment this to configure the network call (for things like authentication) | ||
// for more information, please visit here: https://www.houdinigraphql.com/guides/authentication | ||
// fetchParams({ session }) { | ||
// return { | ||
// headers: { | ||
// Authentication: `Bearer ${session.token}`, | ||
// } | ||
// } | ||
// } | ||
url: `${import.meta.env.VITE_BACKEND_URL ?? ''}/graphql`, | ||
plugins: [authPlugin], | ||
fetchParams(wha) { | ||
const session = getClientSession(); | ||
return { | ||
headers: { | ||
...session.headers | ||
} | ||
}; | ||
} | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { setClientSession, type ClientPlugin } from '$houdini'; | ||
|
||
const houdiniPlugin: ClientPlugin = () => { | ||
return { | ||
async start(ctx, { next }) { | ||
setClientSession({ headers: { owo: 'uwu' } }); | ||
next(ctx); | ||
} | ||
}; | ||
}; | ||
|
||
export { houdiniPlugin }; |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { RegisterOAuthAppStore } from '$houdini'; | ||
|
||
import { readable, type Readable } from 'svelte/store'; | ||
import { z } from 'zod'; | ||
|
||
const OAUTH_STORAGE_KEY = 'oauth_app'; | ||
const OAUTH_APP_SCHEMA = z.object({ | ||
id: z.string().min(1), | ||
secret: z.string().min(1) | ||
}); | ||
|
||
const REGISTER_APP = new RegisterOAuthAppStore(); | ||
|
||
type OAuthApplicationTy = z.infer<typeof OAUTH_APP_SCHEMA>; | ||
|
||
async function registerOAuthApp(): Promise<OAuthApplicationTy> { | ||
const redirectUri = `${window.location.origin}/oauth-callback`; | ||
|
||
try { | ||
const response = await REGISTER_APP.mutate({ | ||
redirectUri | ||
}); | ||
|
||
if (response.errors) { | ||
throw new Error(response.errors.map((error) => error.message).join('\n')); | ||
} | ||
|
||
// If we don't have any errors, we can assume the data is well formed. | ||
// And if the data isn't well formed, then we're fucked anyways. | ||
|
||
return response.data!.registerOauthApplication; | ||
} catch (error) { | ||
console.error(`Failed to register OAuth app: ${error}`); | ||
throw error; | ||
} | ||
} | ||
|
||
async function registerAndStore(): Promise<void> { | ||
const oauthApp = await registerOAuthApp(); | ||
localStorage.setItem(OAUTH_STORAGE_KEY, JSON.stringify(oauthApp)); | ||
} | ||
|
||
async function loadOAuthApp(): Promise<OAuthApplicationTy> { | ||
const rawApp = localStorage.getItem(OAUTH_STORAGE_KEY); | ||
|
||
if (rawApp) { | ||
try { | ||
return OAUTH_APP_SCHEMA.parseAsync(JSON.parse(rawApp)); | ||
} catch (error) { | ||
console.error(`Failed to load OAuth app. Error: ${error}`); | ||
console.error(`Registering new..`); | ||
|
||
await registerAndStore(); | ||
return await loadOAuthApp(); | ||
} | ||
} else { | ||
await registerAndStore(); | ||
return await loadOAuthApp(); | ||
} | ||
} | ||
|
||
class OAuthApplication { | ||
#data = $state<OAuthApplicationTy | undefined>(); | ||
|
||
get data() { | ||
return this.#data; | ||
} | ||
|
||
constructor() { | ||
loadOAuthApp() | ||
.then((loadedApp) => (this.#data = loadedApp)) | ||
.catch(console.error); | ||
} | ||
} | ||
|
||
export { OAuthApplication }; |
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
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
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