Skip to content

Commit

Permalink
feat: 회원가입 시 user cookie 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hamo-o committed Oct 17, 2023
1 parent e56c45c commit 5fcc904
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
15 changes: 13 additions & 2 deletions src/apis/auth/checkSignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ import type { NextRequest } from "next/server";
import { checkSignUp } from "./checkSignUp";

export async function checkSignIn(request: NextRequest) {
const token = request.cookies.get("accessToken")?.value;
const token = request.cookies.has("accessToken");
const url = request.nextUrl.clone();
const { pathname } = request.nextUrl;

if (token) {
return NextResponse.next();
if (!request.nextUrl.pathname.includes("/auth")) {
const userToken = request.cookies.has("user");
if (userToken) {
return NextResponse.next();
}
// 최초진입인 경우 + 회원가입을 하지 않은 경우 + 하다만 경우
else {
return checkSignUp(request);
}
} else {
return NextResponse.next();
}
} else {
url.pathname = "/account";
return NextResponse.redirect(url);
Expand Down
24 changes: 7 additions & 17 deletions src/apis/auth/checkSignUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,16 @@ import { checkSignIn } from "./checkSignIn";
export async function checkSignUp(request: NextRequest) {
const user = await checkUser(request);
const url = request.nextUrl.clone();
const { pathname } = request.nextUrl;

if (pathname.match("/((?!account).*)")) {
return await checkSignIn(request);
}
const response = NextResponse.next();

if (user.name && user.schoolType) {
return NextResponse.next();
response.cookies.set({ name: "user", value: "true", path: "/" });
return response;
} else if (user.name) {
if (
url.pathname !== "/auth/organization" &&
url.pathname !== "/auth/default"
) {
url.pathname = "/auth/organization";
return NextResponse.redirect(url);
} else return NextResponse.next();
url.pathname = "/auth/organization";
return NextResponse.redirect(url);
} else {
if (url.pathname !== "/auth/default") {
url.pathname = "/auth/default";
return NextResponse.redirect(url);
} else return NextResponse.next();
url.pathname = "/auth/default";
return NextResponse.redirect(url);
}
}
7 changes: 2 additions & 5 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { checkSignIn } from "./apis/auth/checkSignIn";
import { checkSignUp } from "./apis/auth/checkSignUp";
import { qFeedAxios } from "./apis/axios";

export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const token = request.cookies.get("accessToken")?.value;

const requestHeaders = new Headers(request.headers);
requestHeaders.set("Authorization", `Bearer ${token}`);

if (pathname.match("/((?!auth).*)")) {
return await checkSignUp(request);
if (!request.nextUrl.pathname.includes("/account")) {
return await checkSignIn(request);
}

return NextResponse.next();
Expand Down
3 changes: 2 additions & 1 deletion src/pages-edit/mypage/SettingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useRouter } from "next/navigation";
import NavigationTopBack from "src/components/navigations/NavigationTopBack";
import Flex from "src/components/common/Flex";
import Text from "src/components/common/Text";
import { deleteCookie } from "src/utils/cookie";
import { deleteCookie, deleteUser } from "src/utils/cookie";
import useDeleteMeMutation from "src/hooks/account/useDeleteMeMutation";

export default function SettingPage() {
Expand All @@ -31,6 +31,7 @@ export default function SettingPage() {
onClick={() => {
deleteMeMutation.mutate();
deleteCookie();
deleteUser();
router.push("/account");
}}
>
Expand Down
6 changes: 6 additions & 0 deletions src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ export const deleteCookie = () => {
path: "/"
});
};

export const deleteUser = () => {
return cookies.remove("user", {
path: "/"
});
};

0 comments on commit 5fcc904

Please sign in to comment.