mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +03:00
feat: saving session id of socket in localstorage
This commit is contained in:
parent
e049ce7f71
commit
bfbf0e8064
13
src/App.tsx
13
src/App.tsx
@ -1,4 +1,4 @@
|
|||||||
import React from 'react';
|
import React, { useContext } from 'react';
|
||||||
import { Router, Switch } from 'react-router-dom';
|
import { Router, Switch } from 'react-router-dom';
|
||||||
import { routes } from './routes.config';
|
import { routes } from './routes.config';
|
||||||
|
|
||||||
@ -13,15 +13,26 @@ import { useRootStore } from '@educt/hooks/useRootStore';
|
|||||||
import PrivateRoute from '@educt/components/PrivateRoute';
|
import PrivateRoute from '@educt/components/PrivateRoute';
|
||||||
import PublicRoute from '@educt/components/PublicRoute';
|
import PublicRoute from '@educt/components/PublicRoute';
|
||||||
import { useSocketEvent } from './hooks/useSocketEvent';
|
import { useSocketEvent } from './hooks/useSocketEvent';
|
||||||
|
import { SocketContext } from './contexts';
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const {
|
const {
|
||||||
uiStore: { history },
|
uiStore: { history },
|
||||||
} = useRootStore();
|
} = useRootStore();
|
||||||
|
const { socket } = useContext(SocketContext);
|
||||||
|
|
||||||
useSocketEvent('connect', () => console.info('[WebSocket]: Connected.'));
|
useSocketEvent('connect', () => console.info('[WebSocket]: Connected.'));
|
||||||
useSocketEvent('disconnect', () => console.info('[WebSocket]: Disconnected.'));
|
useSocketEvent('disconnect', () => console.info('[WebSocket]: Disconnected.'));
|
||||||
useSocketEvent('connect_error', err => console.error(err));
|
useSocketEvent('connect_error', err => console.error(err));
|
||||||
|
useSocketEvent('user:session', data => {
|
||||||
|
console.log(data);
|
||||||
|
if (socket) {
|
||||||
|
const { sessionId, userId } = data;
|
||||||
|
socket.auth = { sessionId };
|
||||||
|
window.localStorage.setItem('sessionId', sessionId);
|
||||||
|
socket.userId = userId;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Router history={history}>
|
<Router history={history}>
|
||||||
|
|||||||
@ -50,7 +50,13 @@ const Layout: React.FC<LayoutPropsType> = ({ children, roles }) => {
|
|||||||
*/
|
*/
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!socket?.connected) {
|
if (!socket?.connected) {
|
||||||
socket?.connect();
|
if (socket) {
|
||||||
|
const sessionId = window.localStorage.getItem('sessionId');
|
||||||
|
if (sessionId) {
|
||||||
|
socket.auth = { sessionId };
|
||||||
|
}
|
||||||
|
socket.connect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [socket]);
|
}, [socket]);
|
||||||
|
|
||||||
|
|||||||
@ -24,6 +24,7 @@ const useLogout = () => {
|
|||||||
const result = await authStore.logout();
|
const result = await authStore.logout();
|
||||||
history.push('/auth');
|
history.push('/auth');
|
||||||
toast({ title: 'You are logged out.', isClosable: true, status: 'info' });
|
toast({ title: 'You are logged out.', isClosable: true, status: 'info' });
|
||||||
|
// window.localStorage.removeItem('sessionId');
|
||||||
socket?.disconnect();
|
socket?.disconnect();
|
||||||
return result.data;
|
return result.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@ -41,9 +41,7 @@ const MainPage: React.FC<IPageProps> = () => {
|
|||||||
*/
|
*/
|
||||||
useSocketEvent('user:online', online => setOnline(online));
|
useSocketEvent('user:online', online => setOnline(online));
|
||||||
|
|
||||||
useSocketEvent('user:connected', user => {
|
useSocketEvent('user:connected', user => console.table(user));
|
||||||
console.table(user);
|
|
||||||
});
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (stat) {
|
if (stat) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user