Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
machineuser
commited on
Commit
·
16601c5
1
Parent(s):
388ac76
Sync widgets demo
Browse files
packages/widgets/README.md
CHANGED
@@ -19,7 +19,7 @@ pnpm install
|
|
19 |
pnpm dev
|
20 |
```
|
21 |
|
22 |
-
If you want to try the "Sign-in with HF" feature locally, you will need to https://huggingface.co/settings/applications/new an OAuth application with `"openid"` and `"inference-api"` scopes and `http://localhost:5173/auth/callback/huggingface` as the redirect URL.
|
23 |
|
24 |
Then you can create a `.env.local` file with the following content:
|
25 |
|
|
|
19 |
pnpm dev
|
20 |
```
|
21 |
|
22 |
+
If you want to try the "Sign-in with HF" feature locally, you will need to https://huggingface.co/settings/applications/new an OAuth application with `"openid"`, `"profile"` and `"inference-api"` scopes and `http://localhost:5173/auth/callback/huggingface` as the redirect URL.
|
23 |
|
24 |
Then you can create a `.env.local` file with the following content:
|
25 |
|
packages/widgets/src/app.d.ts
CHANGED
@@ -17,6 +17,10 @@ declare module "@auth/core/types" {
|
|
17 |
export interface Session {
|
18 |
access_token?: string;
|
19 |
}
|
|
|
|
|
|
|
|
|
20 |
}
|
21 |
|
22 |
export {};
|
|
|
17 |
export interface Session {
|
18 |
access_token?: string;
|
19 |
}
|
20 |
+
|
21 |
+
export interface User {
|
22 |
+
username: string;
|
23 |
+
}
|
24 |
}
|
25 |
|
26 |
export {};
|
packages/widgets/src/hooks.server.ts
CHANGED
@@ -13,6 +13,18 @@ const handleSSO =
|
|
13 |
* SvelteKit has built-in CSRF protection, so we can skip the check
|
14 |
*/
|
15 |
skipCSRFCheck: skipCSRFCheck,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
providers: [
|
17 |
{
|
18 |
name: "Hugging Face",
|
@@ -32,19 +44,24 @@ const handleSSO =
|
|
32 |
* Get the access_token without an account in DB, to make calls to the inference API
|
33 |
*/
|
34 |
callbacks: {
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
},
|
44 |
-
|
45 |
return {
|
46 |
...session,
|
47 |
access_token: token.access_token,
|
|
|
|
|
|
|
48 |
};
|
49 |
},
|
50 |
},
|
|
|
13 |
* SvelteKit has built-in CSRF protection, so we can skip the check
|
14 |
*/
|
15 |
skipCSRFCheck: skipCSRFCheck,
|
16 |
+
cookies: {
|
17 |
+
sessionToken: {
|
18 |
+
name: "session_token",
|
19 |
+
options: {
|
20 |
+
httpOnly: true,
|
21 |
+
sameSite: "lax",
|
22 |
+
secure: true,
|
23 |
+
path: "/",
|
24 |
+
maxAge: 3600, // The OAuth token's lifetime is 3600 seconds
|
25 |
+
},
|
26 |
+
},
|
27 |
+
},
|
28 |
providers: [
|
29 |
{
|
30 |
name: "Hugging Face",
|
|
|
44 |
* Get the access_token without an account in DB, to make calls to the inference API
|
45 |
*/
|
46 |
callbacks: {
|
47 |
+
jwt({ token, account, profile }) {
|
48 |
+
return {
|
49 |
+
...token,
|
50 |
+
/**
|
51 |
+
* account & profile are undefined beyond the first login, in those
|
52 |
+
* cases `token.access_token` and `token.username` are defined
|
53 |
+
*/
|
54 |
+
...(account && { access_token: account.access_token }),
|
55 |
+
...(profile && { username: profile.preferred_username }),
|
56 |
+
};
|
57 |
},
|
58 |
+
session({ session, token }) {
|
59 |
return {
|
60 |
...session,
|
61 |
access_token: token.access_token,
|
62 |
+
user: Object.assign({}, session.user, {
|
63 |
+
username: token.username,
|
64 |
+
}),
|
65 |
};
|
66 |
},
|
67 |
},
|
packages/widgets/src/routes/+layout.server.ts
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
import { env } from "$env/dynamic/private";
|
2 |
import type { LayoutServerLoad } from "./$types.js";
|
3 |
|
4 |
-
|
5 |
-
const session = await locals.getSession();
|
6 |
|
|
|
7 |
return {
|
8 |
-
|
9 |
-
supportsOAuth
|
10 |
};
|
11 |
};
|
|
|
1 |
import { env } from "$env/dynamic/private";
|
2 |
import type { LayoutServerLoad } from "./$types.js";
|
3 |
|
4 |
+
const supportsOAuth = !!env.OAUTH_CLIENT_ID && !!env.OAUTH_CLIENT_SECRET;
|
|
|
5 |
|
6 |
+
export const load: LayoutServerLoad = async ({ locals }) => {
|
7 |
return {
|
8 |
+
session: supportsOAuth ? locals.getSession() : undefined,
|
9 |
+
supportsOAuth,
|
10 |
};
|
11 |
};
|
packages/widgets/src/routes/+page.svelte
CHANGED
@@ -8,7 +8,7 @@
|
|
8 |
import { browser } from "$app/environment";
|
9 |
|
10 |
export let data;
|
11 |
-
let apiToken = data.access_token || "";
|
12 |
|
13 |
function storeHFToken() {
|
14 |
window.localStorage.setItem("hf_token", apiToken);
|
@@ -537,7 +537,7 @@
|
|
537 |
<ModeSwitcher />
|
538 |
|
539 |
{#if data.supportsOAuth}
|
540 |
-
{#if !data.
|
541 |
<form class="contents" method="post" action="/auth/signin/huggingface" target={isIframe ? "_blank" : ""}>
|
542 |
<button type="submit" title="Sign in with Hugging Face">
|
543 |
<img
|
@@ -547,6 +547,14 @@
|
|
547 |
/>
|
548 |
</button>
|
549 |
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
550 |
{/if}
|
551 |
{:else}
|
552 |
<label>
|
|
|
8 |
import { browser } from "$app/environment";
|
9 |
|
10 |
export let data;
|
11 |
+
let apiToken = data.session?.access_token || "";
|
12 |
|
13 |
function storeHFToken() {
|
14 |
window.localStorage.setItem("hf_token", apiToken);
|
|
|
537 |
<ModeSwitcher />
|
538 |
|
539 |
{#if data.supportsOAuth}
|
540 |
+
{#if !data.session}
|
541 |
<form class="contents" method="post" action="/auth/signin/huggingface" target={isIframe ? "_blank" : ""}>
|
542 |
<button type="submit" title="Sign in with Hugging Face">
|
543 |
<img
|
|
|
547 |
/>
|
548 |
</button>
|
549 |
</form>
|
550 |
+
{:else}
|
551 |
+
<div class="flex items-center gap-2">
|
552 |
+
logged in as {data.session.user?.username}
|
553 |
+
<img src={data.session?.user?.image} alt="" class="w-6 h-6 rounded-full" />
|
554 |
+
<form method="post" action="/auth/signout">
|
555 |
+
<button type="submit" class="underline">Sign out</button>
|
556 |
+
</form>
|
557 |
+
</div>
|
558 |
{/if}
|
559 |
{:else}
|
560 |
<label>
|