Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app/auth): logout user when tokens expire #182

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const web3userDataSchema = z.object({
userId: z.number(),
address: z.string(),
reputation_network: z.string(),
exp: z.number(),
});

type Web3UserData = z.infer<typeof web3userDataSchema>;
Expand Down Expand Up @@ -38,7 +39,6 @@ export function Web3AuthProvider({ children }: { children: React.ReactNode }) {
status: AuthStatus;
}>({ user: null, status: 'loading' });

// TODO update SignInSuccessResponse according to new endpoint web3/auth/signin
const handleSignIn = () => {
try {
const accessToken = browserAuthProvider.getAccessToken();
Expand Down
1 change: 1 addition & 0 deletions packages/apps/human-app/frontend/src/auth/auth-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const userDataSchema = z
userId: z.number(),
reputation_network: z.string(),
email_notifications: z.boolean().optional(), // TODO that should be verified when email notifications feature is done
exp: z.number(),
})
.merge(extendableUserDataSchema);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type React from 'react';
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { useGetAccessTokenMutation } from '@/api/servieces/common/get-access-token';
import { useWeb3Auth } from '@/auth-web3/use-web3-auth';
import { useAuth } from '@/auth/use-auth';

export function JWTExpirationCheck({
children,
}: {
children: React.ReactElement;
}) {
const web3Auth = useWeb3Auth();
const web2Auth = useAuth();
const location = useLocation();
const { mutate: getAccessTokenMutation } = useGetAccessTokenMutation();

useEffect(() => {
if (web3Auth.user?.exp && web3Auth.user.exp < Date.now() / 1000) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create variable

getAccessTokenMutation('web3');
}

if (web2Auth.user?.exp && web2Auth.user.exp < Date.now() / 1000) {
getAccessTokenMutation('web2');
}
}, [
location,
web3Auth.user?.exp,
web2Auth.user?.exp,
getAccessTokenMutation,
]);

return children;
}
5 changes: 4 additions & 1 deletion packages/apps/human-app/frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import '@fontsource/inter/600.css';
import '@fontsource/inter/800.css';
import { WalletConnectProvider } from '@/contexts/wallet-connect';
import { Web3AuthProvider } from '@/auth-web3/web3-auth-context';
import { JWTExpirationCheck } from '@/contexts/jwt-expiration-check';

const root = document.getElementById('root');
if (!root) throw Error('root element is undefined');
Expand All @@ -39,7 +40,9 @@ createRoot(root).render(
<WalletConnectProvider>
<Web3AuthProvider>
<AuthProvider>
<Router />
<JWTExpirationCheck>
<Router />
</JWTExpirationCheck>
</AuthProvider>
</Web3AuthProvider>
<ReactQueryDevtools client={queryClient} initialIsOpen={false} />
Expand Down
Loading