Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
machineuser commited on
Commit ·
16601c5
1
Parent(s): 388ac76
Sync widgets demo
Browse filesFiles changed (5)
18 unmodified lines1920212223242518 unmodified linespnpm dev```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.Then you can create a `.env.local` file with the following content:18 unmodified lines1920212223242518 unmodified linespnpm dev```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.Then you can create a `.env.local` file with the following content:
16 unmodified lines17181920212216 unmodified linesexport interface Session {access_token?: string;}}export {};16 unmodified lines1718192021222324252616 unmodified linesexport interface Session {access_token?: string;}export interface User {username: string;}}export {};
12 unmodified lines13141516171813 unmodified lines3233343536373839404142434445464748495012 unmodified lines* SvelteKit has built-in CSRF protection, so we can skip the check*/skipCSRFCheck: skipCSRFCheck,providers: [{name: "Hugging Face",13 unmodified lines* Get the access_token without an account in DB, to make calls to the inference API*/callbacks: {async jwt({ token, account }) {if (account) {return {...token,access_token: account.access_token,};}return token;},async session({ session, token }) {return {...session,access_token: token.access_token,};},},12 unmodified lines13141516171819202122232425262728293013 unmodified lines44454647484950515253545556575859606162636465666712 unmodified lines* SvelteKit has built-in CSRF protection, so we can skip the check*/skipCSRFCheck: skipCSRFCheck,cookies: {sessionToken: {name: "session_token",options: {httpOnly: true,sameSite: "lax",secure: true,path: "/",maxAge: 3600, // The OAuth token's lifetime is 3600 seconds},},},providers: [{name: "Hugging Face",13 unmodified lines* Get the access_token without an account in DB, to make calls to the inference API*/callbacks: {jwt({ token, account, profile }) {return {...token,/*** account & profile are undefined beyond the first login, in those* cases `token.access_token` and `token.username` are defined*/...(account && { access_token: account.access_token }),...(profile && { username: profile.preferred_username }),};},session({ session, token }) {return {...session,access_token: token.access_token,user: Object.assign({}, session.user, {username: token.username,}),};},},
1234567891011import { env } from "$env/dynamic/private";import type { LayoutServerLoad } from "./$types.js";export const load: LayoutServerLoad = async ({ locals }) => {const session = await locals.getSession();return {access_token: session?.access_token,supportsOAuth: !!env.OAUTH_CLIENT_ID && !!env.OAUTH_CLIENT_SECRET,};};1234567891011import { env } from "$env/dynamic/private";import type { LayoutServerLoad } from "./$types.js";const supportsOAuth = !!env.OAUTH_CLIENT_ID && !!env.OAUTH_CLIENT_SECRET;export const load: LayoutServerLoad = async ({ locals }) => {return {session: supportsOAuth ? locals.getSession() : undefined,supportsOAuth,};};
7 unmodified lines891011121314522 unmodified lines5375385395405415425433 unmodified lines5475485495505515527 unmodified linesimport { browser } from "$app/environment";export let data;let apiToken = data.access_token || "";function storeHFToken() {window.localStorage.setItem("hf_token", apiToken);522 unmodified lines<ModeSwitcher />{#if data.supportsOAuth}{#if !data.access_token}<form class="contents" method="post" action="/auth/signin/huggingface" target={isIframe ? "_blank" : ""}><button type="submit" title="Sign in with Hugging Face"><img3 unmodified lines/></button></form>{/if}{:else}<label>7 unmodified lines891011121314522 unmodified lines5375385395405415425433 unmodified lines5475485495505515525535545555565575585595607 unmodified linesimport { browser } from "$app/environment";export let data;let apiToken = data.session?.access_token || "";function storeHFToken() {window.localStorage.setItem("hf_token", apiToken);522 unmodified lines<ModeSwitcher />{#if data.supportsOAuth}{#if !data.session}<form class="contents" method="post" action="/auth/signin/huggingface" target={isIframe ? "_blank" : ""}><button type="submit" title="Sign in with Hugging Face"><img3 unmodified lines/></button></form>{:else}<div class="flex items-center gap-2">logged in as {data.session.user?.username}<img src={data.session?.user?.image} alt="" class="w-6 h-6 rounded-full" /><form method="post" action="/auth/signout"><button type="submit" class="underline">Sign out</button></form></div>{/if}{:else}<label>
packages/widgets/README.md+1-1
packages/widgets/src/app.d.ts+4
packages/widgets/src/hooks.server.ts+26-9
packages/widgets/src/routes/+layout.server.ts+4-4
packages/widgets/src/routes/+page.svelte+10-2