-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
216 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ yarn-error.log* | |
|
||
# local env files | ||
.env*.local | ||
|
||
.env | ||
# vercel | ||
.vercel | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import connect from "@/database/database"; | ||
import User from "@/models/User"; | ||
import { NextResponse, NextRequest } from "next/server"; | ||
|
||
connect(); | ||
|
||
export async function GET( | ||
request: NextRequest, | ||
{ params }: { params: { id: string } } | ||
) { | ||
const id = params.id; | ||
|
||
const user = await User.findById(id); | ||
if (!user) { | ||
return NextResponse.json({ error: "User Not Found" }); | ||
} | ||
return NextResponse.json({ user }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NextResponse } from "next/server"; | ||
import connect from "@/database/database"; | ||
|
||
connect(); | ||
|
||
export async function GET() { | ||
try { | ||
const response = NextResponse.json({ | ||
message: "Logout successfully", | ||
success: true, | ||
}); | ||
response.cookies.set("token", "", { httpOnly: true, expires: new Date(0) }); | ||
return response; | ||
} catch (error: any) { | ||
return NextResponse.json({ error: error.message }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import connect from "@/database/database"; | ||
import User from "@/models/User"; | ||
import { getDataFromToken } from "@/helpers/GetDataFromToken"; | ||
|
||
connect(); | ||
|
||
export async function GET(request: NextRequest) { | ||
try { | ||
const userId = await getDataFromToken(request); | ||
const user = await User.findOne({ _id: userId }).select("-password"); | ||
return NextResponse.json({ | ||
message: "User found", | ||
data: user, | ||
}); | ||
} catch (error: any) { | ||
return NextResponse.json({ error: error.message }, { status: 400 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
"use client"; | ||
import FirstBtn from "@/components/btn/FirstBtn"; | ||
import Image from "next/image"; | ||
import { useDispatch } from "react-redux"; | ||
import { useAppSelector } from "@/store/store"; | ||
import { logOut } from "@/store/UserSlice"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
export default function Home() { | ||
return <div className="text-3xl font-bold">hello</div>; | ||
const { push } = useRouter(); | ||
const dispatch = useDispatch<any>(); | ||
return ( | ||
<div | ||
onClick={() => { | ||
dispatch(logOut()); | ||
push("/signin"); | ||
}} | ||
className="text-3xl font-bold" | ||
> | ||
<div> | ||
<FirstBtn customClasses="mt-8 py-2 px-8" text="Logout" /> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { NextRequest } from "next/server"; | ||
import jwt from "jsonwebtoken"; | ||
|
||
export const getDataFromToken = (request: NextRequest) => { | ||
try { | ||
const token = request.cookies.get("token")?.value || ""; | ||
const decodedToken: any = jwt.verify(token, process.env.JWT_SECRET_KEY!); | ||
return decodedToken.id; | ||
} catch (error: any) { | ||
throw new Error(error.message); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,29 @@ | ||
import { NextResponse } from "next/server"; | ||
import type { NextRequest } from "next/server"; | ||
import { NextResponse, NextRequest } from "next/server"; | ||
|
||
// This function can be marked `async` if using `await` inside | ||
export function middleware(request: NextRequest) { | ||
return NextResponse.redirect(new URL("/home", request.url)); | ||
const path = request.nextUrl.pathname; | ||
const isPublicPath = path === "/signin" || path === "/signup" || path === "/welcome"; | ||
|
||
const token = request.cookies.get("token")?.value || ""; | ||
|
||
if (isPublicPath && token) { | ||
return NextResponse.redirect(new URL("/", request.nextUrl)); | ||
} | ||
|
||
if (!isPublicPath && !token) { | ||
return NextResponse.redirect(new URL("/welcome", request.nextUrl)); | ||
} | ||
} | ||
|
||
// See "Matching Paths" below to learn more | ||
export const config = { | ||
matcher: "/about/:path*", | ||
matcher: [ | ||
/* | ||
* Match all request paths except for the ones starting with: | ||
* - api (API routes) | ||
* - _next/static (static files) | ||
* - _next/image (image optimization files) | ||
* - favicon.ico (favicon file) | ||
*/ | ||
"/((?!api|_next/static|_next/image|favicon.ico).*)", | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters