Skip to content

Commit

Permalink
feature: auth failure middleware redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
soomin9106 committed Aug 10, 2024
1 parent 5bbd04f commit 291dc05
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app/auth/validation/complete/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function ValidationCompletePage() {
const router = useRouter();

const auth_token = searchParams.get("auth_token");
useAuth(auth_token? auth_token : "");
// useAuth(auth_token? auth_token : "");

return (
<div className="flex h-auto flex-col items-center">
Expand Down
6 changes: 4 additions & 2 deletions src/auth/components/EmailForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use client";

import { Button } from "@shared/components/ui/button";
import {
Form,
Expand All @@ -16,13 +15,16 @@ import {
SUBSCRIBE_ANNOUCE,
} from "@subscription/constants/subscribe";

import { LOGIN_OR_SIGNUP } from "@auth/constants/auth";
import { LOGIN_OR_SIGNUP, SIGNUP_FAILED } from "@auth/constants/auth";
import { useEmailForm } from "@auth/hooks/useEmailForm";
import { useLoginFailToast } from "@auth/hooks/useLoginFailToast";

export default function EmailForm() {
const { form, onSubmit, goToPendingPage } = useEmailForm();
const { handleSubmit, control, formState } = form;

useLoginFailToast();

return (
<Form {...form}>
<form onSubmit={form.handleSubmit(goToPendingPage)} className="space-y-8">
Expand Down
2 changes: 2 additions & 0 deletions src/auth/hooks/useAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const useAuth = (auth_token: string) => {
{ auth_token },
{
onSuccess: (response: ApiResponse<tokenResponse>) => {
console.log('res ', response);

if (response?.data?.data) {
const { accessToken, refreshToken } = response.data.data;

Expand Down
24 changes: 24 additions & 0 deletions src/auth/hooks/useLoginFailToast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useEffect } from "react";

import { deleteCookie,getCookie } from "cookies-next";

import { useToast } from "@shared/components/ui/use-toast";
import { ISLOGIN } from "@shared/constants/token";

import { SIGNUP_FAILED } from "@auth/constants/auth";

export function useLoginFailToast() {
const { toast } = useToast();
const isLoginFailed = getCookie(ISLOGIN);

useEffect(() => {
if (isLoginFailed === "false") {
toast({
title: SIGNUP_FAILED,
});

deleteCookie(ISLOGIN);

}
}, [isLoginFailed, toast]);
}
11 changes: 4 additions & 7 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NextResponse } from "next/server";

import { COOKIES } from "@shared/constants/token";
import { articleMiddleware } from "@shared/middlewares/article";
import { AuthMiddleware } from "@shared/middlewares/auth";
import { problemMiddleware } from "@shared/middlewares/problem";
import { unsubscriptionMiddleware } from "@shared/middlewares/subscription";
import { workbookMiddleware } from "@shared/middlewares/workbook";
Expand All @@ -15,10 +16,6 @@ const withOutAuth = async (req: NextRequest) => {
const nextUrl = req.nextUrl.clone();
const { pathname, searchParams } = nextUrl;

// if (pathname === "/") {
// return MainMiddleware();
// }

if (pathname === "/workbook") {
return workbookMiddleware({ nextUrl });
}
Expand All @@ -35,9 +32,9 @@ const withOutAuth = async (req: NextRequest) => {
return problemMiddleware({ req, nextUrl });
}

// if (pathname.includes("/auth/validation/complete")) {
// return AuthMiddleware();
// }
if (pathname.includes("/auth/validation/complete")) {
return AuthMiddleware({ req, nextUrl });
}
};

// NOTE : 인증기반 접근할 수 있는 페이지에 대한 middleware
Expand Down
4 changes: 4 additions & 0 deletions src/shared/constants/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export const COOKIES = {
ACCESS_TOKEN: "accessToken",
REFRESH_TOKEN: "refreshToken",
};

export const ISLOGIN = 'isLogin'

export const AUTH_TOKEN = 'auth_token'
38 changes: 36 additions & 2 deletions src/shared/middlewares/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
export const AuthMiddleware = () => {
return true;
import { NextURL } from "next/dist/server/web/next-url";
import { NextRequest, NextResponse } from "next/server";

import { ApiResponse, fewFetch } from "@api/fewFetch";

import { API_ROUTE } from "@auth/remotes/api";
import { tokenResponse } from "@auth/types/auth";

type authMiddlewareProps = {
req: NextRequest;
nextUrl: NextURL;
};

export const AuthMiddleware = async ({ req, nextUrl }: authMiddlewareProps) => {
try {
const { searchParams } = nextUrl;
const auth_token = searchParams.get('auth_token')
if (auth_token) {
const response: ApiResponse<tokenResponse> = await fewFetch().post(API_ROUTE.TOKEN(auth_token))

if (response.data?.message === "알 수 없는 오류가 발생했어요." || !response.data?.data?.isLogin) {
nextUrl.pathname = `/auth`
nextUrl.searchParams.delete('auth_token')

const response = NextResponse.redirect(nextUrl);
response.cookies.set('isLogin', 'false');
return response;
} else {
if (response.data?.data?.isLogin) {
return NextResponse.redirect(nextUrl);
}
}
}
} catch {
return undefined
}
};

0 comments on commit 291dc05

Please sign in to comment.