Spaces:
Running
Running
Update lib/auth.ts
Browse files- lib/auth.ts +13 -65
lib/auth.ts
CHANGED
|
@@ -1,72 +1,20 @@
|
|
| 1 |
import { User } from "@/types";
|
| 2 |
import { NextResponse } from "next/server";
|
| 3 |
-
import { cookies, headers } from "next/headers";
|
| 4 |
-
import MY_TOKEN_KEY from "./get-cookie-name";
|
| 5 |
|
| 6 |
-
//
|
| 7 |
type UserResponse = User & { token: string };
|
| 8 |
|
| 9 |
-
export const isAuthenticated = async ():
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
const
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return NextResponse.json(
|
| 19 |
-
{
|
| 20 |
-
ok: false,
|
| 21 |
-
message: "Wrong castle fam :(",
|
| 22 |
-
},
|
| 23 |
-
{
|
| 24 |
-
status: 401,
|
| 25 |
-
headers: {
|
| 26 |
-
"Content-Type": "application/json",
|
| 27 |
-
},
|
| 28 |
-
}
|
| 29 |
-
);
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
const user = await fetch("https://huggingface.co/api/whoami-v2", {
|
| 33 |
-
headers: {
|
| 34 |
-
Authorization: token,
|
| 35 |
-
},
|
| 36 |
-
method: "GET",
|
| 37 |
-
})
|
| 38 |
-
.then((res) => res.json())
|
| 39 |
-
.catch(() => {
|
| 40 |
-
return NextResponse.json(
|
| 41 |
-
{
|
| 42 |
-
ok: false,
|
| 43 |
-
message: "Invalid token",
|
| 44 |
-
},
|
| 45 |
-
{
|
| 46 |
-
status: 401,
|
| 47 |
-
headers: {
|
| 48 |
-
"Content-Type": "application/json",
|
| 49 |
-
},
|
| 50 |
-
}
|
| 51 |
-
);
|
| 52 |
-
});
|
| 53 |
-
if (!user || !user.id) {
|
| 54 |
-
return NextResponse.json(
|
| 55 |
-
{
|
| 56 |
-
ok: false,
|
| 57 |
-
message: "Invalid token",
|
| 58 |
-
},
|
| 59 |
-
{
|
| 60 |
-
status: 401,
|
| 61 |
-
headers: {
|
| 62 |
-
"Content-Type": "application/json",
|
| 63 |
-
},
|
| 64 |
-
}
|
| 65 |
-
);
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
-
return {
|
| 69 |
-
...user,
|
| 70 |
-
token: token.replace("Bearer ", ""),
|
| 71 |
};
|
|
|
|
|
|
|
|
|
|
| 72 |
};
|
|
|
|
| 1 |
import { User } from "@/types";
|
| 2 |
import { NextResponse } from "next/server";
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
// Define the structure of our mock user
|
| 5 |
type UserResponse = User & { token: string };
|
| 6 |
|
| 7 |
+
export const isAuthenticated = async (): Promise<UserResponse | NextResponse<unknown> | undefined> => {
|
| 8 |
+
// Create a mock user object that will be returned for every request.
|
| 9 |
+
// This bypasses the need for actual authentication.
|
| 10 |
+
const mockUser: UserResponse = {
|
| 11 |
+
id: "mock_user_123", // A fake user ID
|
| 12 |
+
name: "RaBU-User", // A default username
|
| 13 |
+
fullname: "RaBU-2 User", // A default full name
|
| 14 |
+
avatarUrl: "/default-avatar.png", // A path to a default avatar if you have one
|
| 15 |
+
token: "mock_token_abcdef123456" // A fake token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
};
|
| 17 |
+
|
| 18 |
+
// Always return the mock user, effectively making every request "authenticated".
|
| 19 |
+
return Promise.resolve(mockUser);
|
| 20 |
};
|