mirror of
https://github.com/portainer/portainer.git
synced 2025-08-05 05:45:22 +02:00
feat(auth): save jwt in cookie [EE-5864] (#10527)
This commit is contained in:
parent
ecce501cf3
commit
436da01bce
51 changed files with 679 additions and 312 deletions
|
@ -1,4 +1,3 @@
|
|||
import jwtDecode from 'jwt-decode';
|
||||
import { useCurrentStateAndParams } from '@uirouter/react';
|
||||
import {
|
||||
createContext,
|
||||
|
@ -11,9 +10,7 @@ import {
|
|||
import { isAdmin } from '@/portainer/users/user.helpers';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import { User } from '@/portainer/users/types';
|
||||
import { useUser as useLoadUser } from '@/portainer/users/queries/useUser';
|
||||
|
||||
import { useLocalStorage } from './useLocalStorage';
|
||||
import { useLoadCurrentUser } from '@/portainer/users/queries/useLoadCurrentUser';
|
||||
|
||||
interface State {
|
||||
user?: User;
|
||||
|
@ -163,20 +160,14 @@ interface UserProviderProps {
|
|||
}
|
||||
|
||||
export function UserProvider({ children }: UserProviderProps) {
|
||||
const [jwt] = useLocalStorage('JWT', '');
|
||||
|
||||
const tokenPayload = useMemo(() => jwtDecode(jwt) as { id: number }, [jwt]);
|
||||
|
||||
const userQuery = useLoadUser(tokenPayload.id, {
|
||||
staleTime: Infinity, // should reload te user details only on page load
|
||||
});
|
||||
const userQuery = useLoadCurrentUser();
|
||||
|
||||
const providerState = useMemo(
|
||||
() => ({ user: userQuery.data }),
|
||||
[userQuery.data]
|
||||
);
|
||||
|
||||
if (jwt === '' || !providerState.user) {
|
||||
if (!providerState.user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ import { useCurrentStateAndParams } from '@uirouter/react';
|
|||
import { Terminal as TerminalIcon } from 'lucide-react';
|
||||
import { Terminal } from 'xterm';
|
||||
|
||||
import { get } from '@/react/hooks/useLocalStorage';
|
||||
import { baseHref } from '@/portainer/helpers/pathHelper';
|
||||
import { notifyError } from '@/portainer/services/notifications';
|
||||
|
||||
|
@ -169,10 +168,7 @@ export function ConsoleView() {
|
|||
);
|
||||
|
||||
function connectConsole() {
|
||||
const jwtToken = get('JWT', '');
|
||||
|
||||
const params: StringDictionary = {
|
||||
token: jwtToken,
|
||||
endpointId: environmentId,
|
||||
namespace,
|
||||
podName: podID,
|
||||
|
|
|
@ -11,7 +11,6 @@ import {
|
|||
} from '@/portainer/services/terminal-window';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import { error as notifyError } from '@/portainer/services/notifications';
|
||||
import { useLocalStorage } from '@/react/hooks/useLocalStorage';
|
||||
|
||||
import { Icon } from '@@/Icon';
|
||||
import { Button } from '@@/buttons';
|
||||
|
@ -40,8 +39,6 @@ export function KubeCtlShell({ environmentId, onClose }: Props) {
|
|||
|
||||
const terminalElem = useRef(null);
|
||||
|
||||
const [jwt] = useLocalStorage('JWT', '');
|
||||
|
||||
const handleClose = useCallback(() => {
|
||||
terminalClose(); // only css trick
|
||||
socket?.close();
|
||||
|
@ -103,7 +100,7 @@ export function KubeCtlShell({ environmentId, onClose }: Props) {
|
|||
|
||||
// on component load/destroy
|
||||
useEffect(() => {
|
||||
const socket = new WebSocket(buildUrl(jwt, environmentId));
|
||||
const socket = new WebSocket(buildUrl(environmentId));
|
||||
setShell((shell) => ({ ...shell, socket }));
|
||||
|
||||
terminal.onData((data) => socket.send(data));
|
||||
|
@ -122,7 +119,7 @@ export function KubeCtlShell({ environmentId, onClose }: Props) {
|
|||
}
|
||||
|
||||
return close;
|
||||
}, [environmentId, jwt, terminal]);
|
||||
}, [environmentId, terminal]);
|
||||
|
||||
return (
|
||||
<div className={clsx(styles.root, { [styles.minimized]: shell.minimized })}>
|
||||
|
@ -182,9 +179,8 @@ export function KubeCtlShell({ environmentId, onClose }: Props) {
|
|||
}
|
||||
}
|
||||
|
||||
function buildUrl(jwt: string, environmentId: EnvironmentId) {
|
||||
function buildUrl(environmentId: EnvironmentId) {
|
||||
const params = {
|
||||
token: jwt,
|
||||
endpointId: environmentId,
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue