Skip to content

Commit 972582b

Browse files
authored
Merge pull request #15 from pooulad/improve-loading-ui
Debug auth callback data and improve ui🎯
2 parents 1101526 + 15eedd2 commit 972582b

File tree

11 files changed

+88
-34
lines changed

11 files changed

+88
-34
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ in back-end:
1919
in front-end:
2020
- React hook form
2121
- Axios
22-
- React-loading
2322
- React-toastify
2423
- Tailwindcss
2524
- Typescript

frontend/app/auth/signin/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export const metadata: Metadata = {
1212
export default async function SignIn() {
1313
const user = await getCurrentUser();
1414

15-
if (user) {
15+
console.log("user",user);
16+
17+
18+
if (user?.token) {
1619
redirect("/dashboard");
1720
}
1821

frontend/app/dashboard/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default async function DashboardLayout({
1212
}: DashboardLayoutProps) {
1313
const user = await getCurrentUser();
1414

15-
if (!user) {
15+
if (!user?.token) {
1616
redirect(authOptions?.pages?.signIn || "/auth/signin");
1717
}
1818
return (

frontend/app/dashboard/page.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React from "react";
22
import { getCurrentUser } from "../../lib/session";
33
import { redirect } from "next/navigation";
4-
import { authOptions } from "../../lib/auth";
4+
import { authOptions, getSession } from "../../lib/auth";
55
import Link from "next/link";
66
import { siteConfig } from "../../config/site";
7-
import { signOut } from "next-auth/react";
87
import SignOutButton from "../../components/signout-button/page";
98

109
export const metadata = {
@@ -13,11 +12,17 @@ export const metadata = {
1312
};
1413

1514
async function getUsers(): Promise<any | null> {
15+
const session = await getSession();
16+
console.log("session", session);
17+
1618
try {
1719
const response = await fetch(`${siteConfig.localApi}/users`, {
1820
next: {
1921
revalidate: 60,
2022
},
23+
headers: {
24+
Authorization: `Bearer ${session?.user?.token}`,
25+
},
2126
});
2227

2328
// if (!response?.ok) {
@@ -34,18 +39,17 @@ async function getUsers(): Promise<any | null> {
3439
}
3540

3641
export default async function DashboardPage() {
37-
const users = await getUsers();
3842
const user = await getCurrentUser();
43+
const users = await getUsers();
3944

40-
if (!user) {
41-
redirect(authOptions?.pages?.signIn || "/auth/signin");
42-
}
4345
return (
4446
<main className={`flex min-h-screen flex-col items-center p-4`}>
4547
<header className="container z-40 bg-background">
4648
<div className="flex h-20 items-center justify-between py-6">
4749
<nav>
48-
<SignOutButton className={`absolute left-5 top-5 bg-teal-500 hover:bg-teal-700 text-white font-bold rounded-sm min-w-[70px] text-center`} />
50+
<SignOutButton
51+
className={`absolute left-5 top-5 bg-teal-500 hover:bg-teal-700 text-white font-bold rounded-sm min-w-[70px] text-center`}
52+
/>
4953
</nav>
5054
</div>
5155
</header>

frontend/components/signin-form/page.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Link from "next/link";
55
import { useSearchParams } from "next/navigation";
66
import { useState } from "react";
77
import { SubmitHandler, useForm } from "react-hook-form";
8-
import ReactLoading from "react-loading";
98

109
export default function SignInForm() {
1110
const [loading, setLoading] = useState(false);
@@ -33,18 +32,32 @@ export default function SignInForm() {
3332
callbackUrl: searchParams?.get("from") || "/dashboard",
3433
});
3534
setLoading(false);
35+
console.log("res",signInResult);
36+
3637
};
3738
return (
3839
<main className={`flex min-h-screen flex-col items-center p-4`}>
3940
{loading ? (
4041
<>
41-
<ReactLoading
42-
type={"bars"}
43-
color={"red"}
44-
height={200}
45-
width={200}
46-
className="flex flex-row min-h-screen justify-center items-center"
47-
/>
42+
<div role="status">
43+
<svg
44+
aria-hidden="true"
45+
className="inline w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-green-500"
46+
viewBox="0 0 100 101"
47+
fill="none"
48+
xmlns="http://www.w3.org/2000/svg"
49+
>
50+
<path
51+
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
52+
fill="currentColor"
53+
/>
54+
<path
55+
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
56+
fill="currentFill"
57+
/>
58+
</svg>
59+
<span className="sr-only">Loading...</span>
60+
</div>
4861
</>
4962
) : (
5063
<>

frontend/components/signup-form/page.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
ToastSuccessMessage,
1010
} from "../../utils/ToastGenerator";
1111
import { useRouter } from "next/navigation";
12-
import ReactLoading from "react-loading";
1312
import Link from "next/link";
1413

1514
export default function SignUpForm() {
@@ -57,13 +56,25 @@ export default function SignUpForm() {
5756
<main className={`flex min-h-screen flex-col items-center p-4`}>
5857
{loading ? (
5958
<>
60-
<ReactLoading
61-
type={"bars"}
62-
color={"red"}
63-
height={200}
64-
width={200}
65-
className="flex flex-row min-h-screen justify-center items-center"
66-
/>
59+
<div role="status">
60+
<svg
61+
aria-hidden="true"
62+
className="inline w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-green-500"
63+
viewBox="0 0 100 101"
64+
fill="none"
65+
xmlns="http://www.w3.org/2000/svg"
66+
>
67+
<path
68+
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
69+
fill="currentColor"
70+
/>
71+
<path
72+
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
73+
fill="currentFill"
74+
/>
75+
</svg>
76+
<span className="sr-only">Loading...</span>
77+
</div>
6778
</>
6879
) : (
6980
<>

frontend/lib/auth.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const authOptions: NextAuthOptions = {
2929
}),
3030
});
3131
const user = await res.json();
32-
console.log(user);
32+
console.log("this is user",user);
33+
console.log("this is req",req);
3334

3435

3536
if (user) {
@@ -40,6 +41,17 @@ const authOptions: NextAuthOptions = {
4041
},
4142
}),
4243
],
44+
callbacks: {
45+
async jwt({ token, user }) {
46+
return { ...token, ...user };
47+
},
48+
async session({ session, token, user }) {
49+
console.log("sd",user);
50+
51+
session.user = token as any;
52+
return session;
53+
},
54+
},
4355
pages: {
4456
signIn: "/auth/signin",
4557
// signOut: "/auth/signout",

frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"react-bootstrap": "^2.9.1",
2424
"react-dom": "^19.0.0",
2525
"react-hook-form": "^7.47.0",
26-
"react-loading": "^2.0.3",
2726
"react-toastify": "^9.1.3",
2827
"tailwindcss": "3.3.3",
2928
"typescript": "5.1.6"

frontend/types/next-auth.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { User } from "next-auth";
2+
3+
// type UserId = string
4+
5+
// declare module "next-auth/jwt" {
6+
// interface JWT {
7+
// id: UserId
8+
// }
9+
// }
10+
11+
declare module "next-auth" {
12+
interface Session {
13+
user: User & {
14+
token: string;
15+
message: string;
16+
};
17+
}
18+
}

frontend/yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,11 +2573,6 @@ react-lifecycles-compat@^3.0.4:
25732573
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362"
25742574
integrity sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==
25752575

2576-
react-loading@^2.0.3:
2577-
version "2.0.3"
2578-
resolved "https://registry.yarnpkg.com/react-loading/-/react-loading-2.0.3.tgz#e8138fb0c3e4674e481b320802ac7048ae14ffb9"
2579-
integrity sha512-Vdqy79zq+bpeWJqC+xjltUjuGApyoItPgL0vgVfcJHhqwU7bAMKzysfGW/ADu6i0z0JiOCRJjo+IkFNkRNbA3A==
2580-
25812576
react-toastify@^9.1.3:
25822577
version "9.1.3"
25832578
resolved "https://registry.yarnpkg.com/react-toastify/-/react-toastify-9.1.3.tgz#1e798d260d606f50e0fab5ee31daaae1d628c5ff"

0 commit comments

Comments
 (0)