-
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
22 changed files
with
216 additions
and
170 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import {} from 'twin.macro'; | ||
|
||
type TProps = { | ||
text: string; | ||
onClick: React.MouseEventHandler<HTMLButtonElement> | undefined; | ||
}; | ||
|
||
const Button = ({ text, onClick }: TProps) => { | ||
return ( | ||
<button tw="bg-primary2 py-2 px-4 text-primary7 rounded-md hover:brightness-110" onClick={onClick}> | ||
{text} | ||
</button> | ||
); | ||
}; | ||
|
||
export default Button; |
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,32 +1,35 @@ | ||
// import { useUser } from 'queries/hooks/auth/useUser'; | ||
import { ComponentType, useEffect } from 'react'; | ||
import { ComponentType, PropsWithChildren } from 'react'; | ||
import { useRouter } from 'next/router'; | ||
import { useAuthContext } from 'context/authContext'; | ||
|
||
const WithAuth = (WrappedComponent: ComponentType) => { | ||
// FIXME: useRouter 못쓰는건가? | ||
// const router = useRouter(); | ||
|
||
const HOC = () => { | ||
// 유저가 없으면 로그인 페이지로 이동 로그인이 필요한 페이지만 적용 ex) 상품 구매? 페이지? | ||
|
||
// FIXME: 서버 페이지 연결하고 주석 풀기 | ||
// useEffect(() => { | ||
// const { user } = useUser(); | ||
// if (!user) { | ||
// // FIXME: Alert 대신 Toast 띄어주기! | ||
// alert('로그인이 필요합니다!'); | ||
// router.push('/login'); | ||
// } else { | ||
// // FIXME: Alert 대신 Toast 띄어주기! | ||
// alert('로그인이 되어있습니다!'); | ||
// router.push('/'); | ||
// } | ||
// }, []); | ||
|
||
return <WrappedComponent />; | ||
import {} from 'twin.macro'; | ||
|
||
const WithAuth = (WrappedComponent: ComponentType<PropsWithChildren>, requireAdmin?: boolean) => { | ||
const AuthenticatedComponent = (props: PropsWithChildren) => { | ||
const router = useRouter(); | ||
const { loading, user } = useAuthContext(); | ||
|
||
console.log(user, 'withAuth 안 user'); | ||
|
||
if (loading) { | ||
return ( | ||
<div tw="flex justify-center items-center mt-32 "> | ||
<p tw="te">Loading...</p> | ||
</div> | ||
); | ||
} | ||
|
||
if ((requireAdmin && !user?.isAdmin) || !user) { | ||
if (typeof window !== 'undefined') { | ||
router.push('/'); | ||
} | ||
return null; | ||
} | ||
|
||
return <WrappedComponent {...props} />; | ||
}; | ||
|
||
return HOC; | ||
return AuthenticatedComponent; | ||
}; | ||
|
||
export default WithAuth; |
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 @@ | ||
|
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
File renamed without changes.
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
File renamed without changes.
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,56 @@ | ||
import { Dispatch, ReactNode, SetStateAction, createContext, useContext, useEffect, useState } from 'react'; | ||
import { User, getAuth, onAuthStateChanged } from 'firebase/auth'; | ||
import { adminUser, login, logout, onUserStateChange } from '@services/firebase'; | ||
|
||
interface IUser extends User { | ||
isAdmin?: boolean; | ||
} | ||
|
||
type AuthContextType = { | ||
user: IUser | null; | ||
loading: boolean; | ||
// setUserInfo: Dispatch<SetStateAction<IUser | undefined>>; | ||
login: typeof login; | ||
logout: typeof logout; | ||
}; | ||
|
||
const initialAuthContext: Partial<AuthContextType> = { | ||
login: login, | ||
logout: logout, | ||
}; | ||
|
||
type AuthContextProviderProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
const AuthContext = createContext<typeof initialAuthContext>(initialAuthContext); | ||
|
||
export function AuthContextProvider({ children }: AuthContextProviderProps) { | ||
// const [userInfo, setUserInfo] = useState<IUser>(); | ||
const [authState, setAuthState] = useState<{ | ||
user: IUser | null; | ||
loading: boolean; | ||
}>({ user: null, loading: true }); | ||
|
||
const { user, loading } = authState; | ||
|
||
useEffect(() => { | ||
const stopListen = onAuthStateChanged(getAuth(), (user) => { | ||
if (user) { | ||
adminUser(user).then((user) => setAuthState({ user, loading: false })); | ||
} else { | ||
setAuthState({ user: null, loading: false }); | ||
} | ||
}); | ||
|
||
return () => stopListen(); | ||
}, [adminUser, getAuth]); | ||
|
||
return <AuthContext.Provider value={{ user, login, logout, loading }}>{children}</AuthContext.Provider>; | ||
} | ||
|
||
export function useAuthContext() { | ||
return useContext(AuthContext); | ||
} | ||
|
||
export default AuthContext; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.