Skip to content

Commit

Permalink
oauth progress
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Aug 12, 2024
1 parent 3e4a447 commit 5d41499
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 12 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
packages = rec {
default = main;

devenv-up = self.devShells.${system}.default.config.procfileScript;

cli = craneLib.buildPackage (
commonArgs
// {
Expand Down
1 change: 1 addition & 0 deletions kitsune-fe/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions kitsune-fe/src/client.ts
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) {

Check warning on line 7 in kitsune-fe/src/client.ts

View workflow job for this annotation

GitHub Actions / Spell-check repository source

"wha" should be "what".
const session = getClientSession();
return {
headers: {
...session.headers
}
};
}
});
12 changes: 12 additions & 0 deletions kitsune-fe/src/lib/oauth/auth.svelte.ts
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 };
76 changes: 76 additions & 0 deletions kitsune-fe/src/lib/oauth/client.svelte.ts
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 };

0 comments on commit 5d41499

Please sign in to comment.