-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
1 parent
31145c5
commit 9c21975
Showing
5 changed files
with
21 additions
and
19 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 |
---|---|---|
@@ -1,26 +1,13 @@ | ||
import { useEffect } from 'react'; | ||
import jwt from 'jwt-decode'; | ||
import { useRecoilState } from 'recoil'; | ||
|
||
import { useGetCurrentUserQuery } from '~/generated/graphql'; | ||
import { AuthTokenPair, useGetCurrentUserQuery } from '~/generated/graphql'; | ||
|
||
import { currentUserState } from '../states/currentUserState'; | ||
import { tokenPairState } from '../states/tokenPairState'; | ||
|
||
export function useFetchCurrentUser() { | ||
const [, setCurrentUser] = useRecoilState(currentUserState); | ||
const [tokenPair] = useRecoilState(tokenPairState); | ||
export function useFetchCurrentUser(tokenPair: AuthTokenPair | null) { | ||
const userId = tokenPair?.accessToken.token | ||
? jwt<{ sub: string }>(tokenPair.accessToken.token).sub | ||
: null; | ||
const { data } = useGetCurrentUserQuery({ | ||
variables: { uuid: userId }, | ||
}); | ||
const user = data?.users?.[0]; | ||
|
||
useEffect(() => { | ||
if (user) { | ||
setCurrentUser(user); | ||
} | ||
}, [user, setCurrentUser]); | ||
return data?.users?.[0]; | ||
} |
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,9 +1,22 @@ | ||
import { useEffect } from 'react'; | ||
import { useRecoilState } from 'recoil'; | ||
|
||
import { useFetchCurrentUser } from '@/auth/hooks/useFetchCurrentUser'; | ||
import { currentUserState } from '@/auth/states/currentUserState'; | ||
import { tokenPairState } from '@/auth/states/tokenPairState'; | ||
|
||
export const UserProvider: React.FC<React.PropsWithChildren> = ({ | ||
children, | ||
}) => { | ||
useFetchCurrentUser(); | ||
const [, setCurrentUser] = useRecoilState(currentUserState); | ||
const [tokenPair] = useRecoilState(tokenPairState); | ||
const user = useFetchCurrentUser(tokenPair); | ||
|
||
useEffect(() => { | ||
if (user) { | ||
setCurrentUser(user); | ||
} | ||
}, [setCurrentUser, user]); | ||
|
||
return <>{children}</>; | ||
}; |
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