Spaces:
Runtime error
Runtime error
hugging face sign in + save generation with user.sub
Browse files- app/api/auth/route.ts +0 -1
- app/api/collections/route.ts +3 -3
- app/api/route.ts +2 -1
- components/input-generation.tsx +1 -1
- components/login/index.tsx +33 -10
- components/main/hooks/useCollections.ts +4 -1
- components/main/hooks/useInputGeneration.ts +3 -0
- components/main/index.tsx +1 -0
- prisma/schema.prisma +2 -1
- utils/useUser.ts +1 -3
app/api/auth/route.ts
CHANGED
|
@@ -24,7 +24,6 @@ export async function POST(req: Request) {
|
|
| 24 |
.json()
|
| 25 |
.catch(() => ({}));
|
| 26 |
|
| 27 |
-
console.log(response)
|
| 28 |
if (response.error) return Response.json({ status: 401, ok: false, message: response.error_description });
|
| 29 |
|
| 30 |
return Response.json(
|
|
|
|
| 24 |
.json()
|
| 25 |
.catch(() => ({}));
|
| 26 |
|
|
|
|
| 27 |
if (response.error) return Response.json({ status: 401, ok: false, message: response.error_description });
|
| 28 |
|
| 29 |
return Response.json(
|
app/api/collections/route.ts
CHANGED
|
@@ -3,15 +3,15 @@ import { PrismaClient } from '@prisma/client'
|
|
| 3 |
const prisma = new PrismaClient()
|
| 4 |
|
| 5 |
export async function POST(request: Request) {
|
| 6 |
-
const {
|
| 7 |
|
| 8 |
const collections = await prisma.collection.findMany({
|
| 9 |
orderBy: {
|
| 10 |
id: 'desc'
|
| 11 |
},
|
| 12 |
where: {
|
| 13 |
-
|
| 14 |
-
|
| 15 |
}
|
| 16 |
},
|
| 17 |
take: 15,
|
|
|
|
| 3 |
const prisma = new PrismaClient()
|
| 4 |
|
| 5 |
export async function POST(request: Request) {
|
| 6 |
+
const { userId, page } = await request.json()
|
| 7 |
|
| 8 |
const collections = await prisma.collection.findMany({
|
| 9 |
orderBy: {
|
| 10 |
id: 'desc'
|
| 11 |
},
|
| 12 |
where: {
|
| 13 |
+
userId: {
|
| 14 |
+
equals: userId
|
| 15 |
}
|
| 16 |
},
|
| 17 |
take: 15,
|
app/api/route.ts
CHANGED
|
@@ -16,7 +16,7 @@ export async function POST(
|
|
| 16 |
['x-use-cache']: "0"
|
| 17 |
}
|
| 18 |
|
| 19 |
-
const { inputs, style } = await request.json()
|
| 20 |
const findStyle = list_styles.find((item) => item.name === style)
|
| 21 |
|
| 22 |
const textIsNSFW = await isTextNSFW(inputs, global_headers)
|
|
@@ -48,6 +48,7 @@ export async function POST(
|
|
| 48 |
data: {
|
| 49 |
prompt: inputs,
|
| 50 |
file_name: name,
|
|
|
|
| 51 |
},
|
| 52 |
})
|
| 53 |
|
|
|
|
| 16 |
['x-use-cache']: "0"
|
| 17 |
}
|
| 18 |
|
| 19 |
+
const { inputs, style, userId } = await request.json()
|
| 20 |
const findStyle = list_styles.find((item) => item.name === style)
|
| 21 |
|
| 22 |
const textIsNSFW = await isTextNSFW(inputs, global_headers)
|
|
|
|
| 48 |
data: {
|
| 49 |
prompt: inputs,
|
| 50 |
file_name: name,
|
| 51 |
+
userId: userId ?? "",
|
| 52 |
},
|
| 53 |
})
|
| 54 |
|
components/input-generation.tsx
CHANGED
|
@@ -23,7 +23,7 @@ export const InputGeneration: React.FC = () => {
|
|
| 23 |
value={value}
|
| 24 |
type="text"
|
| 25 |
className="h-full text-lg placeholder:text-gray-400 text-gray-900 font-medium w-full outline-none truncate"
|
| 26 |
-
placeholder="A
|
| 27 |
onChange={(e) => setValue(e.target.value)}
|
| 28 |
onBlur={() => setPrompt(value)}
|
| 29 |
onKeyDown={(e) => {
|
|
|
|
| 23 |
value={value}
|
| 24 |
type="text"
|
| 25 |
className="h-full text-lg placeholder:text-gray-400 text-gray-900 font-medium w-full outline-none truncate"
|
| 26 |
+
placeholder="A red car, forest in the background"
|
| 27 |
onChange={(e) => setValue(e.target.value)}
|
| 28 |
onBlur={() => setPrompt(value)}
|
| 29 |
onKeyDown={(e) => {
|
components/login/index.tsx
CHANGED
|
@@ -1,30 +1,53 @@
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
-
import { useMount } from "react-use";
|
| 4 |
import Image from "next/image";
|
| 5 |
import { FaUserAstronaut } from "react-icons/fa";
|
| 6 |
|
| 7 |
import { useUser } from "@/utils/useUser";
|
| 8 |
import HFLogo from "@/assets/images/hf-logo.svg";
|
|
|
|
|
|
|
| 9 |
|
| 10 |
export const Login = ({ code }: { code?: string }) => {
|
| 11 |
-
const { getAuthorization } = useUser();
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
return (
|
| 16 |
<div className="p-6 min-h-screen flex items-center justify-center">
|
| 17 |
<div className="mx-auto bg-white w-full max-w-sm rounded-3xl p-6">
|
| 18 |
-
<p
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
</p>
|
| 21 |
<div className="flex items-center justify-evenly mt-3">
|
| 22 |
<Image src={HFLogo} alt="HF Logo" width={100} height={100} />
|
| 23 |
-
|
| 24 |
-
<
|
| 25 |
-
|
| 26 |
-
<
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
<div className="w-[80px] h-[80px] rounded-full ring-4 ring-indigo-200 border-2 border-white bg-indigo-50 p-3">
|
| 29 |
<FaUserAstronaut className="w-full h-full text-indigo-500" />
|
| 30 |
</div>
|
|
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
+
import { useMount, useUpdateEffect } from "react-use";
|
| 4 |
import Image from "next/image";
|
| 5 |
import { FaUserAstronaut } from "react-icons/fa";
|
| 6 |
|
| 7 |
import { useUser } from "@/utils/useUser";
|
| 8 |
import HFLogo from "@/assets/images/hf-logo.svg";
|
| 9 |
+
import classNames from "classnames";
|
| 10 |
+
import { HiCheckBadge } from "react-icons/hi2";
|
| 11 |
|
| 12 |
export const Login = ({ code }: { code?: string }) => {
|
| 13 |
+
const { getAuthorization, user } = useUser();
|
| 14 |
|
| 15 |
+
useMount(() => getAuthorization(code as string));
|
| 16 |
+
|
| 17 |
+
useUpdateEffect(() => {
|
| 18 |
+
if (user) {
|
| 19 |
+
setTimeout(() => {
|
| 20 |
+
window.location.href = "/";
|
| 21 |
+
}, 2000);
|
| 22 |
+
}
|
| 23 |
+
}, [user]);
|
| 24 |
|
| 25 |
return (
|
| 26 |
<div className="p-6 min-h-screen flex items-center justify-center">
|
| 27 |
<div className="mx-auto bg-white w-full max-w-sm rounded-3xl p-6">
|
| 28 |
+
<p
|
| 29 |
+
className={classNames(
|
| 30 |
+
"text-center text-gray-400 uppercase font-bold text-sm whitespace-pre-line",
|
| 31 |
+
{
|
| 32 |
+
"!text-green-600": user,
|
| 33 |
+
}
|
| 34 |
+
)}
|
| 35 |
+
>
|
| 36 |
+
{user
|
| 37 |
+
? "You have been logged in successfully. \nRedirecting..."
|
| 38 |
+
: "Retrieving your information..."}
|
| 39 |
</p>
|
| 40 |
<div className="flex items-center justify-evenly mt-3">
|
| 41 |
<Image src={HFLogo} alt="HF Logo" width={100} height={100} />
|
| 42 |
+
{user ? (
|
| 43 |
+
<HiCheckBadge className="w-[30px] h-[30px] text-green-500" />
|
| 44 |
+
) : (
|
| 45 |
+
<div className="loading-dots grey translate-y-[5px]">
|
| 46 |
+
<span />
|
| 47 |
+
<span />
|
| 48 |
+
<span />
|
| 49 |
+
</div>
|
| 50 |
+
)}
|
| 51 |
<div className="w-[80px] h-[80px] rounded-full ring-4 ring-indigo-200 border-2 border-white bg-indigo-50 p-3">
|
| 52 |
<FaUserAstronaut className="w-full h-full text-indigo-500" />
|
| 53 |
</div>
|
components/main/hooks/useCollections.ts
CHANGED
|
@@ -3,8 +3,11 @@ import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
| 3 |
import { useLocalStorage, useUpdateEffect } from "react-use";
|
| 4 |
import _ from "lodash";
|
| 5 |
|
|
|
|
|
|
|
| 6 |
export const useCollections = (category: string) => {
|
| 7 |
const [loading, setLoading] = useState(false);
|
|
|
|
| 8 |
const [myGenerationsId] = useLocalStorage<any>('my-own-generations', []);
|
| 9 |
|
| 10 |
const client = useQueryClient();
|
|
@@ -19,7 +22,7 @@ export const useCollections = (category: string) => {
|
|
| 19 |
const response = await fetch("/api/collections", {
|
| 20 |
method: "POST",
|
| 21 |
body: JSON.stringify({
|
| 22 |
-
|
| 23 |
page: 0,
|
| 24 |
}),
|
| 25 |
})
|
|
|
|
| 3 |
import { useLocalStorage, useUpdateEffect } from "react-use";
|
| 4 |
import _ from "lodash";
|
| 5 |
|
| 6 |
+
import { useUser } from "@/utils/useUser";
|
| 7 |
+
|
| 8 |
export const useCollections = (category: string) => {
|
| 9 |
const [loading, setLoading] = useState(false);
|
| 10 |
+
const { user } = useUser();
|
| 11 |
const [myGenerationsId] = useLocalStorage<any>('my-own-generations', []);
|
| 12 |
|
| 13 |
const client = useQueryClient();
|
|
|
|
| 22 |
const response = await fetch("/api/collections", {
|
| 23 |
method: "POST",
|
| 24 |
body: JSON.stringify({
|
| 25 |
+
userId: category === 'my-own' ? user?.sub : undefined,
|
| 26 |
page: 0,
|
| 27 |
}),
|
| 28 |
})
|
components/main/hooks/useInputGeneration.ts
CHANGED
|
@@ -5,9 +5,11 @@ import { useLocalStorage } from 'react-use';
|
|
| 5 |
import { Image } from "@/utils/type"
|
| 6 |
import list_styles from "@/assets/list_styles.json"
|
| 7 |
import { useCollection } from "@/components/modal/useCollection";
|
|
|
|
| 8 |
|
| 9 |
export const useInputGeneration = () => {
|
| 10 |
const { setOpen } = useCollection();
|
|
|
|
| 11 |
const [myGenerationsId, setGenerationsId] = useLocalStorage<any>('my-own-generations', []);
|
| 12 |
|
| 13 |
const { data: prompt } = useQuery(["prompt"], () => {
|
|
@@ -57,6 +59,7 @@ export const useInputGeneration = () => {
|
|
| 57 |
body: JSON.stringify({
|
| 58 |
inputs: prompt,
|
| 59 |
style,
|
|
|
|
| 60 |
}),
|
| 61 |
})
|
| 62 |
const data = await response.json()
|
|
|
|
| 5 |
import { Image } from "@/utils/type"
|
| 6 |
import list_styles from "@/assets/list_styles.json"
|
| 7 |
import { useCollection } from "@/components/modal/useCollection";
|
| 8 |
+
import { useUser } from "@/utils/useUser";
|
| 9 |
|
| 10 |
export const useInputGeneration = () => {
|
| 11 |
const { setOpen } = useCollection();
|
| 12 |
+
const { user } = useUser();
|
| 13 |
const [myGenerationsId, setGenerationsId] = useLocalStorage<any>('my-own-generations', []);
|
| 14 |
|
| 15 |
const { data: prompt } = useQuery(["prompt"], () => {
|
|
|
|
| 59 |
body: JSON.stringify({
|
| 60 |
inputs: prompt,
|
| 61 |
style,
|
| 62 |
+
userId: user?.sub,
|
| 63 |
}),
|
| 64 |
})
|
| 65 |
const data = await response.json()
|
components/main/index.tsx
CHANGED
|
@@ -40,6 +40,7 @@ export const Main = () => {
|
|
| 40 |
{categories.map(({ key, label, icon, isLogged }) =>
|
| 41 |
isLogged && !user ? (
|
| 42 |
<img
|
|
|
|
| 43 |
src="https://huggingface.co/datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl.svg"
|
| 44 |
className="cursor-pointer hover:-translate-y-1 transition-all duration-200"
|
| 45 |
onClick={openWindowLogin}
|
|
|
|
| 40 |
{categories.map(({ key, label, icon, isLogged }) =>
|
| 41 |
isLogged && !user ? (
|
| 42 |
<img
|
| 43 |
+
key={key}
|
| 44 |
src="https://huggingface.co/datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl.svg"
|
| 45 |
className="cursor-pointer hover:-translate-y-1 transition-all duration-200"
|
| 46 |
onClick={openWindowLogin}
|
prisma/schema.prisma
CHANGED
|
@@ -4,7 +4,7 @@ generator client {
|
|
| 4 |
|
| 5 |
datasource db {
|
| 6 |
provider = "sqlite"
|
| 7 |
-
url = "file
|
| 8 |
}
|
| 9 |
|
| 10 |
model Collection {
|
|
@@ -12,4 +12,5 @@ model Collection {
|
|
| 12 |
prompt String
|
| 13 |
file_name String
|
| 14 |
createdAt DateTime @default(now())
|
|
|
|
| 15 |
}
|
|
|
|
| 4 |
|
| 5 |
datasource db {
|
| 6 |
provider = "sqlite"
|
| 7 |
+
url = "file:../data/dev.db"
|
| 8 |
}
|
| 9 |
|
| 10 |
model Collection {
|
|
|
|
| 12 |
prompt String
|
| 13 |
file_name String
|
| 14 |
createdAt DateTime @default(now())
|
| 15 |
+
userId String? @default("")
|
| 16 |
}
|
utils/useUser.ts
CHANGED
|
@@ -29,7 +29,7 @@ export const useUser = () => {
|
|
| 29 |
clear();
|
| 30 |
return null;
|
| 31 |
}
|
| 32 |
-
return res;
|
| 33 |
},
|
| 34 |
{
|
| 35 |
refetchOnWindowFocus: false,
|
|
@@ -52,7 +52,6 @@ export const useUser = () => {
|
|
| 52 |
};
|
| 53 |
|
| 54 |
const getAuthorization = async (code: string) => {
|
| 55 |
-
console.log(code)
|
| 56 |
const request = await fetch("/api/auth", {
|
| 57 |
method: "POST",
|
| 58 |
body: JSON.stringify({
|
|
@@ -64,7 +63,6 @@ export const useUser = () => {
|
|
| 64 |
setValue(res.access_token, {
|
| 65 |
expires: res.experes_in,
|
| 66 |
});
|
| 67 |
-
window.location.href = "/";
|
| 68 |
}
|
| 69 |
|
| 70 |
return {
|
|
|
|
| 29 |
clear();
|
| 30 |
return null;
|
| 31 |
}
|
| 32 |
+
return res?.user;
|
| 33 |
},
|
| 34 |
{
|
| 35 |
refetchOnWindowFocus: false,
|
|
|
|
| 52 |
};
|
| 53 |
|
| 54 |
const getAuthorization = async (code: string) => {
|
|
|
|
| 55 |
const request = await fetch("/api/auth", {
|
| 56 |
method: "POST",
|
| 57 |
body: JSON.stringify({
|
|
|
|
| 63 |
setValue(res.access_token, {
|
| 64 |
expires: res.experes_in,
|
| 65 |
});
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
return {
|