-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: auth failure middleware redirection
- Loading branch information
1 parent
5bbd04f
commit 291dc05
Showing
7 changed files
with
75 additions
and
12 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
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
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]); | ||
} |
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
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 | ||
} | ||
}; |