Skip to content

Commit 5236be6

Browse files
authored
Merge pull request #6 from CodeChefVIT/harshit/users
login done
2 parents 9df2527 + 9722090 commit 5236be6

13 files changed

Lines changed: 398 additions & 87 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@radix-ui/react-slot": "^1.1.1",
2424
"@radix-ui/react-tooltip": "^1.1.7",
2525
"@t3-oss/env-nextjs": "^0.10.1",
26-
"@tanstack/react-query": "^5.53.1",
26+
"@tanstack/react-query": "^5.65.1",
2727
"@tanstack/react-query-devtools": "^5.53.1",
2828
"@tanstack/react-table": "^8.20.6",
2929
"axios": "^1.7.7",

pnpm-lock.yaml

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api/auth.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import axios from "./axiosConfig";
2+
import {data} from "@/store/interfaces"
3+
4+
export const login = async(email: string, password: string) =>{
5+
try {
6+
const response = await axios.post('/auth/login', {email, password})
7+
return response.data as data;
8+
}catch(err){
9+
throw err;
10+
}
11+
}

src/api/axiosConfig.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
import { refreshTK } from "@/store/interfaces";
3+
import axios, {AxiosError, InternalAxiosRequestConfig } from "axios";
4+
import { redirect } from "next/navigation";
5+
import toast from "react-hot-toast";
6+
const instance = axios.create({
7+
baseURL: process.env.NEXT_PUBLIC_BASEURL,
8+
withCredentials: true
9+
})
10+
interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
11+
_retry?: boolean;
12+
}
13+
14+
instance.interceptors.response.use(
15+
(response) =>{
16+
// console.log(response)
17+
return response
18+
19+
},
20+
async (err)=>{
21+
const error = err as AxiosError;
22+
const originalRequest = error.config as CustomAxiosRequestConfig;
23+
24+
// console.log(err.response.status)
25+
if (error.response?.status === 401 && !originalRequest._retry) {
26+
originalRequest._retry = true;
27+
28+
try {
29+
await axios.post<refreshTK>(
30+
`${process.env.NEXT_PUBLIC_BASEURL}/auth/refresh`,
31+
{},
32+
{
33+
withCredentials: true,
34+
}
35+
);
36+
return instance(originalRequest);
37+
} catch {
38+
toast.error("Session expired. Please login again.");
39+
40+
setTimeout(() => {
41+
window.location.href = "/login";
42+
}, 2000);
43+
}
44+
}
45+
46+
return Promise.reject(error);
47+
}
48+
49+
)
50+
export default instance

src/api/fetchUsers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { user } from "@/store/interfaces";
2+
import axios from "./axiosConfig";
3+
4+
5+
export const fetchUsers = async()=>{
6+
try{
7+
const response = await axios.get('admin/users')
8+
console.log(response.data)
9+
return response.data as user[];
10+
}catch(err){
11+
throw err;
12+
}
13+
}
14+
15+

src/app/layout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GeistSans } from "geist/font/sans";
55
import { type Metadata } from "next";
66
import { Toaster } from "react-hot-toast";
77
import { ThemeProvider } from "@/components/theme-provider";
8+
89
import {
910
Sheet,
1011
SheetContent,

src/app/login/page.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { LoginForm } from "@/components/login-form"
22

33
export default function Page() {
4+
45
return (
5-
<div className=" flex min-h-svh w-full items-center justify-center p-6 md:p-10">
6-
<div className="w-full max-w-sm">
6+
<div className=" flex h-full w-full items-center justify-center p-6 md:p-10">
7+
<div className=" w-full max-w-sm">
78
<LoginForm />
89
</div>
910
</div>

src/app/page.tsx

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,80 @@ import Link from "next/link";
33
import logo from "../../public/cc-logo.svg";
44

55
export default function HomePage() {
6+
67
return (
7-
<main className="flex min-h-screen flex-col items-center justify-center p-8">
8-
<div className="w-full max-w-xl rounded-lg bg-white p-8 text-center text-black shadow-lg">
9-
<Image
10-
src={logo as HTMLImageElement}
11-
alt="CodeChef-VIT Logo"
12-
width={150}
13-
height={150}
14-
className="mx-auto mb-4"
15-
/>
16-
<h1 className="mb-4 text-6xl font-bold">CodeChef-VIT</h1>
17-
<p className="mb-6 text-2xl">Portal Template Repository</p>
18-
<p className="mb-2 text-lg font-semibold text-gray-700">
19-
TanStack Query is Initialised in this Repository
20-
</p>
21-
<div className="flex flex-row gap-2">
22-
<Link
23-
href="/todo"
24-
className="mx-auto w-fit rounded-md bg-gray-100 p-3 text-lg font-medium text-indigo-600 transition-all duration-200 hover:bg-gray-200 hover:text-indigo-800 active:scale-95"
25-
>
26-
Sample GET Request
27-
</Link>
28-
<Link
29-
href="/post"
30-
className="mx-auto w-fit rounded-md bg-gray-100 p-3 text-lg font-medium text-indigo-600 transition-all duration-200 hover:bg-gray-200 hover:text-indigo-800 active:scale-95"
31-
>
32-
Sample POST Request
33-
</Link>
34-
</div>
35-
36-
<div className="mt-2">
37-
<p className="text-lg font-semibold text-gray-700">
38-
Shadcn is Initialised in this Repository
8+
<>
9+
<main className="flex min-h-screen flex-col items-center justify-center p-8">
10+
<div className="w-full max-w-xl rounded-lg bg-white p-8 text-center text-black shadow-lg">
11+
<Image
12+
src={logo as HTMLImageElement}
13+
alt="CodeChef-VIT Logo"
14+
width={150}
15+
height={150}
16+
className="mx-auto mb-4"
17+
/>
18+
<h1 className="mb-4 text-6xl font-bold">CodeChef-VIT</h1>
19+
<p className="mb-6 text-2xl">Portal Template Repository</p>
20+
<p className="mb-2 text-lg font-semibold text-gray-700">
21+
TanStack Query is Initialised in this Repository
3922
</p>
40-
<p className="mt-4 text-gray-700">
41-
Custom toast hook is created. You can use it by importing{" "}
42-
<code className="rounded bg-gray-100 p-1">useToast</code> from{" "}
43-
<code className="rounded bg-gray-100 p-1">
44-
&quot;@/lib/toast.tsx&quot;
45-
</code>{" "}
46-
and then calling the create method with message and type as
47-
arguments.
48-
</p>
49-
<div className="mt-4 text-gray-700">
50-
<p>Example: </p>
51-
<div className="rounded-md bg-gray-100 p-2 text-left">
52-
<p>
53-
<span className="text-blue-600">const</span>{" "}
54-
<span className="text-green-600">toast</span> ={" "}
55-
<span className="text-blue-600">useToast</span>();
56-
</p>
57-
<p>
58-
<span className="text-green-600">toast</span>.
59-
<span className="text-blue-600">create</span>(
60-
<span className="text-red-600">&quot;Hello World&quot;</span>,{" "}
61-
<span className="text-red-600">&quot;success&quot;</span>);
62-
</p>
23+
<div className="flex flex-row gap-2">
24+
<Link
25+
href="/todo"
26+
className="mx-auto w-fit rounded-md bg-gray-100 p-3 text-lg font-medium text-indigo-600 transition-all duration-200 hover:bg-gray-200 hover:text-indigo-800 active:scale-95"
27+
>
28+
Sample GET Request
29+
</Link>
30+
<Link
31+
href="/post"
32+
className="mx-auto w-fit rounded-md bg-gray-100 p-3 text-lg font-medium text-indigo-600 transition-all duration-200 hover:bg-gray-200 hover:text-indigo-800 active:scale-95"
33+
>
34+
Sample POST Request
35+
</Link>
36+
</div>
37+
38+
<div className="mt-2">
39+
<p className="text-lg font-semibold text-gray-700">
40+
Shadcn is Initialised in this Repository
41+
</p>
42+
<p className="mt-4 text-gray-700">
43+
Custom toast hook is created. You can use it by importing{" "}
44+
<code className="rounded bg-gray-100 p-1">useToast</code> from{" "}
45+
<code className="rounded bg-gray-100 p-1">
46+
&quot;@/lib/toast.tsx&quot;
47+
</code>{" "}
48+
and then calling the create method with message and type as
49+
arguments.
50+
</p>
51+
<div className="mt-4 text-gray-700">
52+
<p>Example: </p>
53+
<div className="rounded-md bg-gray-100 p-2 text-left">
54+
<p>
55+
<span className="text-blue-600">const</span>{" "}
56+
<span className="text-green-600">toast</span> ={" "}
57+
<span className="text-blue-600">useToast</span>();
58+
</p>
59+
<p>
60+
<span className="text-green-600">toast</span>.
61+
<span className="text-blue-600">create</span>(
62+
<span className="text-red-600">&quot;Hello World&quot;</span>,{" "}
63+
<span className="text-red-600">&quot;success&quot;</span>);
64+
</p>
65+
</div>
6366
</div>
67+
<p className="mt-4 text-gray-700">
68+
There are 3 variants configured:{" "}
69+
<span className="font-bold">success</span>,{" "}
70+
<span className="font-bold">error</span>, and{" "}
71+
<span className="font-bold">info</span>.
72+
</p>
73+
<p className="mt-4 text-gray-700">
74+
You can add more in{" "}
75+
<code className="rounded bg-gray-100 p-1">toast.tsx</code> file
76+
</p>
6477
</div>
65-
<p className="mt-4 text-gray-700">
66-
There are 3 variants configured:{" "}
67-
<span className="font-bold">success</span>,{" "}
68-
<span className="font-bold">error</span>, and{" "}
69-
<span className="font-bold">info</span>.
70-
</p>
71-
<p className="mt-4 text-gray-700">
72-
You can add more in{" "}
73-
<code className="rounded bg-gray-100 p-1">toast.tsx</code> file
74-
</p>
7578
</div>
76-
</div>
77-
</main>
79+
</main>
80+
</>
7881
);
7982
}

src/app/users/page.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
"use client"
12
import { DataTable } from "@/components/table/data-table";
23
import columns from "@/components/columns";
34
import { tasks } from "@/data/data";
5+
import { useQuery } from "@tanstack/react-query";
6+
import { fetchUsers } from "@/api/fetchUsers"
7+
import oosers from '@/components/dumUser.json'
8+
export default function Users(){
9+
const {data: userList, isLoading, isError} = useQuery({
10+
queryKey: ['users'],
11+
queryFn: fetchUsers
12+
})
413

5-
export default function users(){
14+
if(isLoading){
15+
<>loading...</>
16+
}
17+
if (isError){
18+
<>skill issue</>
19+
}
620

721
return (<div className="">
822
<DataTable data={tasks} columns={columns} />

0 commit comments

Comments
 (0)