feat: reset Recoil state on logout (#1675)

Closes #1452
This commit is contained in:
Thaïs
2023-09-20 15:26:11 +02:00
committed by GitHub
parent d8590bb358
commit a20ca95f32
3 changed files with 17 additions and 36 deletions

View File

@ -1,6 +1,10 @@
import { useCallback } from 'react';
import { useApolloClient } from '@apollo/client';
import { useRecoilState } from 'recoil';
import {
snapshot_UNSTABLE,
useGotoRecoilSnapshot,
useRecoilState,
} from 'recoil';
import {
useChallengeMutation,
@ -24,6 +28,8 @@ export const useAuth = () => {
const client = useApolloClient();
const goToRecoilSnapshot = useGotoRecoilSnapshot();
const handleChallenge = useCallback(
async (email: string, password: string) => {
const challengeResult = await challenge({
@ -93,12 +99,13 @@ export const useAuth = () => {
);
const handleSignOut = useCallback(() => {
goToRecoilSnapshot(snapshot_UNSTABLE());
setTokenPair(null);
setCurrentUser(null);
client.clearStore().then(() => {
sessionStorage.clear();
});
}, [setTokenPair, client, setCurrentUser]);
}, [goToRecoilSnapshot, setTokenPair, setCurrentUser, client]);
const handleCredentialsSignUp = useCallback(
async (email: string, password: string, workspaceInviteHash?: string) => {

View File

@ -1,4 +1,4 @@
import { useCallback, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { useNavigate, useParams } from 'react-router-dom';
import { yupResolver } from '@hookform/resolvers/yup';
@ -71,12 +71,17 @@ export const useSignInUp = () => {
mode: 'onChange',
defaultValues: {
exist: false,
email: isSignInPrefilled ? 'tim@apple.dev' : '',
password: isSignInPrefilled ? 'Applecar2025' : '',
},
resolver: yupResolver(validationSchema),
});
useEffect(() => {
if (isSignInPrefilled) {
form.setValue('email', 'tim@apple.dev');
form.setValue('password', 'Applecar2025');
}
}, [form, isSignInPrefilled]);
const {
signInWithCredentials,
signUpWithCredentials,