enzostvs HF staff commited on
Commit
7bb6a57
β€’
1 Parent(s): 0fec1c4

wip signin

Browse files
package-lock.json CHANGED
@@ -25,6 +25,7 @@
25
  "@sveltejs/kit": "^1.27.4",
26
  "@types/cookie": "^0.5.1",
27
  "@types/js-cookie": "^3.0.6",
 
28
  "@typescript-eslint/eslint-plugin": "^6.0.0",
29
  "@typescript-eslint/parser": "^6.0.0",
30
  "autoprefixer": "^10.4.16",
@@ -1434,6 +1435,15 @@
1434
  "integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==",
1435
  "dev": true
1436
  },
 
 
 
 
 
 
 
 
 
1437
  "node_modules/@types/pug": {
1438
  "version": "2.0.10",
1439
  "resolved": "https://registry.npmjs.org/@types/pug/-/pug-2.0.10.tgz",
@@ -4429,6 +4439,12 @@
4429
  "node": ">=14.0"
4430
  }
4431
  },
 
 
 
 
 
 
4432
  "node_modules/update-browserslist-db": {
4433
  "version": "1.0.13",
4434
  "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.13.tgz",
 
25
  "@sveltejs/kit": "^1.27.4",
26
  "@types/cookie": "^0.5.1",
27
  "@types/js-cookie": "^3.0.6",
28
+ "@types/node": "^20.11.2",
29
  "@typescript-eslint/eslint-plugin": "^6.0.0",
30
  "@typescript-eslint/parser": "^6.0.0",
31
  "autoprefixer": "^10.4.16",
 
1435
  "integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==",
1436
  "dev": true
1437
  },
1438
+ "node_modules/@types/node": {
1439
+ "version": "20.11.2",
1440
+ "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.2.tgz",
1441
+ "integrity": "sha512-cZShBaVa+UO1LjWWBPmWRR4+/eY/JR/UIEcDlVsw3okjWEu+rB7/mH6X3B/L+qJVHDLjk9QW/y2upp9wp1yDXA==",
1442
+ "devOptional": true,
1443
+ "dependencies": {
1444
+ "undici-types": "~5.26.4"
1445
+ }
1446
+ },
1447
  "node_modules/@types/pug": {
1448
  "version": "2.0.10",
1449
  "resolved": "https://registry.npmjs.org/@types/pug/-/pug-2.0.10.tgz",
 
4439
  "node": ">=14.0"
4440
  }
4441
  },
4442
+ "node_modules/undici-types": {
4443
+ "version": "5.26.5",
4444
+ "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
4445
+ "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==",
4446
+ "devOptional": true
4447
+ },
4448
  "node_modules/update-browserslist-db": {
4449
  "version": "1.0.13",
4450
  "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.13.tgz",
package.json CHANGED
@@ -18,6 +18,7 @@
18
  "@sveltejs/kit": "^1.27.4",
19
  "@types/cookie": "^0.5.1",
20
  "@types/js-cookie": "^3.0.6",
 
21
  "@typescript-eslint/eslint-plugin": "^6.0.0",
22
  "@typescript-eslint/parser": "^6.0.0",
23
  "autoprefixer": "^10.4.16",
 
18
  "@sveltejs/kit": "^1.27.4",
19
  "@types/cookie": "^0.5.1",
20
  "@types/js-cookie": "^3.0.6",
21
+ "@types/node": "^20.11.2",
22
  "@typescript-eslint/eslint-plugin": "^6.0.0",
23
  "@typescript-eslint/parser": "^6.0.0",
24
  "autoprefixer": "^10.4.16",
src/lib/components/sidebar/Sidebar.svelte CHANGED
@@ -2,6 +2,7 @@
2
  import cookies from 'js-cookie';
3
  import Icon from "@iconify/svelte"
4
  import { get } from 'svelte/store';
 
5
  import { userStore, openWindowLogin } from "$lib/stores/use-user";
6
  import { SIDEBAR_MENUS } from "$lib/utils";
7
  import HFLogo from "$lib/assets/hf-logo.svg";
@@ -23,6 +24,11 @@
23
  cookies.remove("hf_access_token");
24
  window.location.href = "/";
25
  }
 
 
 
 
 
26
  </script>
27
 
28
  <button class="bg-transparent absolute top-10 right-8 cursor-pointer xl:hidden" on:click="{handleClick}">
 
2
  import cookies from 'js-cookie';
3
  import Icon from "@iconify/svelte"
4
  import { get } from 'svelte/store';
5
+
6
  import { userStore, openWindowLogin } from "$lib/stores/use-user";
7
  import { SIDEBAR_MENUS } from "$lib/utils";
8
  import HFLogo from "$lib/assets/hf-logo.svg";
 
24
  cookies.remove("hf_access_token");
25
  window.location.href = "/";
26
  }
27
+ // if (!e.data.code) return;
28
+ // if (e.data.type === "user-oauth" && e?.data?.code && !events.code) {
29
+ // events.code = e.data.code;
30
+ // loginFromCode(e.data.code);
31
+ // }
32
  </script>
33
 
34
  <button class="bg-transparent absolute top-10 right-8 cursor-pointer xl:hidden" on:click="{handleClick}">
src/lib/stores/use-user.ts CHANGED
@@ -1,17 +1,22 @@
1
  import { writable } from "svelte/store";
 
2
 
3
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
  export const userStore = writable<any>(null);
5
 
6
-
7
  export const openWindowLogin = async () => {
8
  return window.open(
9
  "/api/auth/login",
10
  "Login to LoRAs Studio",
11
  "menubar=no,width=500,height=777,location=no,resizable=no,scrollbars=yes,status=no"
12
  );
13
- // const response = await fetch(`/api/auth/login`);
14
- // const { redirect } = await response.json();
15
- // if (redirect) {
16
- // }
 
 
 
 
 
17
  };
 
1
  import { writable } from "svelte/store";
2
+ import cookies from "js-cookie";
3
 
4
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
  export const userStore = writable<any>(null);
6
 
 
7
  export const openWindowLogin = async () => {
8
  return window.open(
9
  "/api/auth/login",
10
  "Login to LoRAs Studio",
11
  "menubar=no,width=500,height=777,location=no,resizable=no,scrollbars=yes,status=no"
12
  );
13
+ };
14
+
15
+ export const loginFromCode = async (code: string) => {
16
+ const request = await fetch(`/api/auth?code=${code}`);
17
+ const { ok, token } = await request.json();
18
+ if (ok) {
19
+ cookies.set("hf_access_token", token, { expires: 1 });
20
+ window.location.reload();
21
+ }
22
  };
src/routes/+layout.svelte CHANGED
@@ -1,11 +1,13 @@
1
  <script>
2
  import { get } from "svelte/store";
 
 
 
3
  import Sidebar from "$lib/components/sidebar/Sidebar.svelte";
4
  import "$lib/styles/tailwind.css"
5
- import { userStore } from "$lib/stores/use-user";
6
  import Dialog from "$lib/components/dialog/Dialog.svelte";
7
  import { loginModalStore } from "$lib/stores/use-login-modal";
8
- import Icon from "@iconify/svelte";
9
 
10
  export let data;
11
  userStore.set(data?.user?.user);
@@ -15,6 +17,14 @@
15
  loginModalStore.subscribe((v) => {
16
  open = v;
17
  });
 
 
 
 
 
 
 
 
18
  </script>
19
 
20
  <div class="flex items-start">
 
1
  <script>
2
  import { get } from "svelte/store";
3
+ import { browser } from '$app/environment';
4
+ import Icon from "@iconify/svelte";
5
+
6
  import Sidebar from "$lib/components/sidebar/Sidebar.svelte";
7
  import "$lib/styles/tailwind.css"
8
+ import { userStore, loginFromCode } from "$lib/stores/use-user";
9
  import Dialog from "$lib/components/dialog/Dialog.svelte";
10
  import { loginModalStore } from "$lib/stores/use-login-modal";
 
11
 
12
  export let data;
13
  userStore.set(data?.user?.user);
 
17
  loginModalStore.subscribe((v) => {
18
  open = v;
19
  });
20
+
21
+ if (browser) {
22
+ window.addEventListener("message", async (event) => {
23
+ if (event.data?.type === "user-oauth") {
24
+ await loginFromCode(event.data.code);
25
+ }
26
+ });
27
+ }
28
  </script>
29
 
30
  <div class="flex items-start">
src/routes/api/auth/+server.ts ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /** @type {import('./$types').RequestHandler} */
2
+
3
+ import { json } from '@sveltejs/kit';
4
+
5
+ export async function POST(request) {
6
+ const code = request.url.searchParams.get('code')
7
+
8
+ if (!code) {
9
+ return json({
10
+ message: `No code provided`,
11
+ }, {
12
+ status: 400
13
+ })
14
+ }
15
+
16
+ const REDIRECT_URI = `https://${process.env.SPACE_HOST}/login/callback`;
17
+ const Authorization = `Basic ${Buffer.from(
18
+ `${process.env.OAUTH_CLIENT_ID}:${process.env.OAUTH_CLIENT_SECRET}`
19
+ ).toString("base64")}`;
20
+
21
+ const request_auth = await fetch("https://huggingface.co/oauth/token", {
22
+ method: "POST",
23
+ headers: {
24
+ "Content-Type": "application/x-www-form-urlencoded",
25
+ Authorization,
26
+ },
27
+ body: new URLSearchParams({
28
+ grant_type: "authorization_code",
29
+ code: code,
30
+ redirect_uri: REDIRECT_URI,
31
+ }),
32
+ });
33
+
34
+ const { access_token } = await request_auth.json();
35
+
36
+ if (!access_token) {
37
+ return json({
38
+ message: `No access token provided`,
39
+ }, {
40
+ status: 400
41
+ })
42
+ }
43
+
44
+ return json({
45
+ ok: true,
46
+ token: access_token
47
+ })
48
+ }
src/routes/login/callback/+page.svelte CHANGED
@@ -1,4 +1,29 @@
1
- <div>
2
- <h1>Callback</h1>
3
- <p>Callback page</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  </div>
 
1
+ <script lang="ts">
2
+ import { browser } from '$app/environment';
3
+ export let data;
4
+
5
+ if (browser && data?.code) {
6
+ if (window.opener) {
7
+ window.opener.postMessage(
8
+ {
9
+ code: data?.code,
10
+ type: "user-oauth",
11
+ },
12
+ window.location.origin
13
+ );
14
+ setTimeout(() => window.close(), 200);
15
+ } else {
16
+ window.postMessage(
17
+ {
18
+ code: data?.code,
19
+ type: "user-oauth",
20
+ },
21
+ window.location.origin
22
+ );
23
+ }
24
+ }
25
+ </script>
26
+
27
+ <div class="p-10">
28
+ <h1 class="text-white text-2xl font-bold">Login in progress..</h1>
29
  </div>
src/routes/login/callback/+page.ts ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ export async function load({ url }) {
2
+ const code = url.searchParams.get("code")
3
+
4
+ return {
5
+ code
6
+ }
7
+ }