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

Clear sessions in other tabs on log out in one tab #196

Merged
merged 1 commit into from
Jan 13, 2021
Merged
Changes from all commits
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
42 changes: 27 additions & 15 deletions ui/src/App/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import React from 'react';
import React, { useEffect } from 'react';
import { Router } from 'react-router-dom';
import history from 'services/history';
import { AuthProvider, UserManager } from 'oidc-react';
import { AuthProvider, UserManager, useAuth as useOidc } from 'oidc-react';
import Oidc from 'oidc-client';
import { ApolloProvider } from '@apollo/client';
import apolloClient from 'services/apolloClient';
import { useOrgSlug } from 'services/location';
import AuthRoutesContainer from './components/AuthRoutesContainer';

const ApolloSetup = () => {
const orgSlug = useOrgSlug();

return (
<ApolloProvider client={apolloClient(orgSlug)}>
<AuthRoutesContainer />
</ApolloProvider>
);
};

const redirectUri = process.env.REACT_APP_KEYCLOAK_CALLBACK_URL;

// we create our own userManager to store user data in localStorage instead
Expand All @@ -40,10 +30,32 @@ const userManager = new UserManager({
automaticSilentRenew: true,
});

const ApolloSetup = () => {
const orgSlug = useOrgSlug();
const oidc = useOidc();

// if the user has e.g. logged out in another tab
useEffect(() => {
const signedOutHandler = () => {
console.log('user signed out event');
oidc.signOut();
};
userManager.events.addUserSignedOut(signedOutHandler);
return () => userManager.events.removeUserSignedOut(signedOutHandler);
}, [oidc]);

return (
<ApolloProvider client={apolloClient(orgSlug)}>
<AuthRoutesContainer />
</ApolloProvider>
);
};

// To show the user as logged in when they open a new tab and they're already
// logged in. Though there are some bugs around this
// https://github.com/Edgeryders-Participio/realities/issues/191
userManager.signinSilent();
// logged in
userManager.signinSilent().catch((err) => {
console.log('silent sign in failed:', err);
});

userManager.events.addSilentRenewError((err) => {
console.log('silent renew error', err);
Expand Down