feat: align auth api with front convention (#370)
* feat: align auth api with front convention * fix: email password auth * fix: proper file naming * Fix login --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -3,11 +3,12 @@ import { useRecoilState } from 'recoil';
|
||||
|
||||
import { useChallengeMutation, useVerifyMutation } from '~/generated/graphql';
|
||||
|
||||
import { tokenService } from '../services/TokenService';
|
||||
import { currentUserState } from '../states/currentUserState';
|
||||
import { isAuthenticatingState } from '../states/isAuthenticatingState';
|
||||
import { tokenPairState } from '../states/tokenPairState';
|
||||
|
||||
export function useAuth() {
|
||||
const [, setTokenPair] = useRecoilState(tokenPairState);
|
||||
const [, setCurrentUser] = useRecoilState(currentUserState);
|
||||
const [, setIsAuthenticating] = useRecoilState(isAuthenticatingState);
|
||||
|
||||
@ -50,14 +51,14 @@ export function useAuth() {
|
||||
throw new Error('No verify result');
|
||||
}
|
||||
|
||||
tokenService.setTokenPair(verifyResult.data?.verify.tokens);
|
||||
setTokenPair(verifyResult.data?.verify.tokens);
|
||||
|
||||
setIsAuthenticating(false);
|
||||
setCurrentUser(verifyResult.data?.verify.user);
|
||||
|
||||
return verifyResult.data?.verify;
|
||||
},
|
||||
[setCurrentUser, setIsAuthenticating, verify],
|
||||
[setCurrentUser, setIsAuthenticating, setTokenPair, verify],
|
||||
);
|
||||
|
||||
const handleLogin = useCallback(
|
||||
@ -70,8 +71,8 @@ export function useAuth() {
|
||||
);
|
||||
|
||||
const handleLogout = useCallback(() => {
|
||||
tokenService.removeTokenPair();
|
||||
}, []);
|
||||
setTokenPair(null);
|
||||
}, [setTokenPair]);
|
||||
|
||||
return {
|
||||
challenge: handleChallenge,
|
||||
|
||||
26
front/src/modules/auth/hooks/useFetchCurrentUser.ts
Normal file
26
front/src/modules/auth/hooks/useFetchCurrentUser.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { useEffect } from 'react';
|
||||
import jwt from 'jwt-decode';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { 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);
|
||||
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]);
|
||||
}
|
||||
@ -1,21 +1,9 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { cookieStorage } from '@/utils/cookie-storage';
|
||||
import { tokenPairState } from '../states/tokenPairState';
|
||||
|
||||
export function useIsLogged(): boolean {
|
||||
const [value, setValue] = useState<string | undefined>(
|
||||
cookieStorage.getItem('accessToken'),
|
||||
);
|
||||
const [tokenPair] = useRecoilState(tokenPairState);
|
||||
|
||||
useEffect(() => {
|
||||
const updateValue = (newValue: string | undefined) => setValue(newValue);
|
||||
|
||||
cookieStorage.addEventListener('accessToken', updateValue);
|
||||
|
||||
return () => {
|
||||
cookieStorage.removeEventListener('accessToken', updateValue);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return !!value;
|
||||
return !!tokenPair;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user