Skip to content

Commit

Permalink
Fixed cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
HohShenYien committed Jul 27, 2023
1 parent c8fbdd1 commit 1aceda9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
NEXT_PUBLIC_API_URL=localhost:3000/api
NEXT_PUBLIC_DOMAIN=localhost
NEXT_PUBLIC_API_URL=localhost:3000/api
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ You can start editing the page by modifying `app/page.tsx`. The page auto-update

### Env

The .env file has two variables that you might want to update
The .env file has one variable that you might want to update

```env
# The base API URL to make any request
NEXT_PUBLIC_API_URL=/api
# For cookie
NEXT_PUBLIC_DOMAIN=localhost
```

### Authentication
Expand Down
8 changes: 2 additions & 6 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { apiClient } from "@/features/Auth/AxiosProvider";
import { deleteCookie } from "cookies-next";
import { useMutation, useQuery } from "@tanstack/react-query";
import { User } from "@/features/Auth/types";
import { AxiosError } from "axios";
import useAuthToken from "@/features/Auth/hooks/useAuthToken";
import { RegisterType } from "@/features/Auth/modals/RegisterModal";
import { AuthType } from "@/features/Auth/modals/LoginModal";
import { login } from "@/features/Auth/utils";
import { deleteCookie, login } from "@/features/Auth/utils";
import { modals } from "@mantine/modals";
import { useRouter } from "next/router";
import { authRedirectPath, loginAfterRegister } from "@/configs/auth.config";
Expand Down Expand Up @@ -71,10 +70,7 @@ export const useUserQuery = () => {
} catch (exception) {
if (exception instanceof AxiosError) {
if (exception.response?.status === 401) {
deleteCookie("authToken", {
path: "/",
domain: process.env.NEXT_PUBLIC_DOMAIN,
});
deleteCookie();
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/features/Auth/hooks/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const useSession = () => {
const authToken = useAuthToken();

const session: Session = useMemo(() => {
console.log("AUTH TOKEN Changed");
console.log(authToken);
if (!authToken) {
return { status: "unauthenticated", user: undefined };
}
Expand Down
28 changes: 18 additions & 10 deletions src/features/Auth/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { notifications } from "@mantine/notifications";
import { deleteCookie, setCookie } from "cookies-next";
import { setCookie } from "cookies-next";
import { useRouter } from "next/router";

export const login = (jwt: string, message?: string, expiryDate?: Date) => {
Expand All @@ -8,6 +8,7 @@ export const login = (jwt: string, message?: string, expiryDate?: Date) => {
expiryDate.setFullYear(expiryDate.getFullYear() + 1);
}
setCookie("authToken", jwt, { expires: expiryDate });
cookieChanged();
notifications.show({
message: message ?? "Login successfully, redirecting...",
color: "green",
Expand All @@ -18,20 +19,27 @@ export const useLogout = () => {
const router = useRouter();
const signout = () => {
router.push("/");
deleteCookie("authToken", {
path: "/",
domain: process.env.NEXT_PUBLIC_DOMAIN,
});
cookieChanged();
notifications.show({
message: "Logged out successfully",
color: "green",
});
// Logging out after redirected
setTimeout(() => {
deleteCookie();
notifications.show({
message: "Logged out successfully",
color: "green",
});
}, 300);
};

return signout;
};

export const deleteCookie = () => {
// Deleting cookie by setting it to a long ago time
const expired = new Date();
expired.setFullYear(1970);
setCookie("authToken", "", { expires: expired });
cookieChanged();
};

export const cookieChanged = () => {
const event = new Event("CookieChanged");
document.dispatchEvent(event);
Expand Down
1 change: 0 additions & 1 deletion src/pages/api/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const authSchema = z.object({
const handler = (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === "POST") {
const data = authSchema.parse(JSON.parse(req.body));
console.log(data);
if (data) {
if (data.email === "[email protected]" && data.password === "asdf") {
res.status(200).json({ auth_token: "This is a JWT auth token" });
Expand Down

1 comment on commit 1aceda9

@vercel
Copy link

@vercel vercel bot commented on 1aceda9 Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.