Skip to content

Commit 635f934

Browse files
committed
Experiment use login api
1 parent 5bf774d commit 635f934

File tree

17 files changed

+206
-260
lines changed

17 files changed

+206
-260
lines changed

config/start.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = webpackBase({
1515
DEV_PROXY: {
1616
'/api/': proxyTarget,
1717
'/assets/': proxyTarget,
18+
'/auth/': proxyTarget,
1819
'/extensions/': proxyTarget,
1920
'/pulp/': proxyTarget,
2021
'/static/rest_framework/': proxyTarget,

src/api/base.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ export class BaseAPI {
88

99
constructor() {
1010
this.http = axios.create({
11-
// adapter + withCredentials ensures no popup on http basic auth fail
1211
adapter: 'fetch',
13-
withCredentials: false,
12+
// Allow session cookie to be used.
13+
withCredentials: true,
14+
// Don't let the browser ask for username/password.
15+
headers: { 'X-Request-With': 'XMLHttpRequest' },
1416

1517
// baseURL gets set in PulpAPI
1618
paramsSerializer: {

src/api/pulp.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ export class PulpAPI extends BaseAPI {
1010
super();
1111

1212
this.http.interceptors.request.use((request) => {
13-
if (!request.auth) {
14-
request.auth = JSON.parse(
15-
window.sessionStorage.credentials ||
16-
window.localStorage.credentials ||
17-
'{}',
18-
);
19-
}
20-
2113
request.baseURL = config.API_BASE_PATH;
2214
request.headers['X-CSRFToken'] = Cookies.get('csrftoken');
2315

src/app-context.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
import { type ReactNode, createContext, useContext, useState } from 'react';
22
import { type AlertType } from 'src/components';
3-
import { useUserContext } from './user-context';
43

4+
export interface IUser {
5+
username?: string;
6+
}
57
export interface IAppContextType {
68
alerts: AlertType[];
79
featureFlags; // deprecated
810
hasPermission: (name: string) => boolean;
911
queueAlert: (alert: AlertType) => void;
1012
setAlerts: (alerts: AlertType[]) => void;
1113
settings; // deprecated
12-
user; // deprecated
14+
user: IUser;
1315
}
1416

1517
export const AppContext = createContext<IAppContextType>(undefined);
1618
export const useAppContext = () => useContext(AppContext);
1719

18-
export const AppContextProvider = ({ children }: { children: ReactNode }) => {
20+
export const AppContextProvider = ({
21+
user,
22+
children,
23+
}: {
24+
user: IUser;
25+
children: ReactNode;
26+
}) => {
1927
const [alerts, setAlerts] = useState<AlertType[]>([]);
20-
const { credentials } = useUserContext();
2128

2229
// hub compat for now
2330
const featureFlags = {
@@ -49,14 +56,7 @@ export const AppContextProvider = ({ children }: { children: ReactNode }) => {
4956
queueAlert,
5057
setAlerts,
5158
settings,
52-
// FIXME: hack
53-
user: credentials
54-
? {
55-
username: credentials.username,
56-
groups: [],
57-
model_permissions: {},
58-
}
59-
: null,
59+
user,
6060
}}
6161
>
6262
{children}

src/app-routes.tsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Trans } from '@lingui/react/macro';
22
import { Banner, Flex, FlexItem } from '@patternfly/react-core';
33
import WrenchIcon from '@patternfly/react-icons/dist/esm/icons/wrench-icon';
44
import { type ElementType } from 'react';
5-
import { Navigate, useLocation } from 'react-router';
5+
import { Navigate, redirect, useLocation } from 'react-router';
66
import { ErrorBoundary, ExternalLink, NotFound } from 'src/components';
77
import {
88
AboutProject,
@@ -31,7 +31,6 @@ import {
3131
ExecutionEnvironmentRegistryList,
3232
GroupDetail,
3333
GroupList,
34-
LoginPage,
3534
MultiSearch,
3635
MyImports,
3736
MyNamespaces,
@@ -50,11 +49,10 @@ import {
5049
UserList,
5150
UserProfile,
5251
} from 'src/containers';
53-
import { StandaloneLayout } from 'src/layout';
5452
import { Paths, formatPath } from 'src/paths';
5553
import { config } from 'src/ui-config';
5654
import { loginURL } from 'src/utilities';
57-
import { useUserContext } from './user-context';
55+
import { useAppContext } from './app-context';
5856

5957
interface IRouteConfig {
6058
beta?: boolean;
@@ -211,12 +209,6 @@ const routes: IRouteConfig[] = [
211209
path: Paths.ansible.namespace.mine,
212210
beta: true,
213211
},
214-
{
215-
component: LoginPage,
216-
path: Paths.meta.login,
217-
noAuth: true,
218-
beta: true,
219-
},
220212
{
221213
component: CollectionDocs,
222214
path: Paths.ansible.collection.docs_page,
@@ -301,10 +293,10 @@ const AuthHandler = ({
301293
noAuth,
302294
path,
303295
}: IRouteConfig) => {
304-
const { credentials } = useUserContext();
296+
const { user } = useAppContext();
305297
const { pathname } = useLocation();
306298

307-
if (!credentials && !noAuth) {
299+
if (!user.username && !noAuth) {
308300
// NOTE: also update LoginLink when changing this
309301
if (config.UI_EXTERNAL_LOGIN_URI) {
310302
window.location.replace(loginURL(pathname));
@@ -358,16 +350,32 @@ const appRoutes = () =>
358350
...rest,
359351
}));
360352

353+
const convert = (m) => {
354+
const {
355+
default: Component,
356+
clientLoader: loader,
357+
clientAction: action,
358+
...rest
359+
} = m;
360+
return { ...rest, loader, action, Component };
361+
};
362+
361363
export const dataRoutes = [
362364
{
363-
element: <StandaloneLayout />,
365+
id: 'root',
366+
lazy: () => import('src/routes/root').then((m) => convert(m)),
364367
children: [
365368
{
366369
errorElement: <ErrorBoundary />,
367370
children: [
368371
{
369372
index: true,
370-
element: <Navigate to={formatPath(Paths.core.status)} />,
373+
loader: () => redirect(formatPath(Paths.core.status)),
374+
},
375+
{
376+
path: 'login',
377+
id: 'login',
378+
lazy: () => import('src/routes/login').then((m) => convert(m)),
371379
},
372380
...appRoutes(),
373381
// "No matching route" is not handled by the error boundary.

src/components/delete-user-modal.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Trans } from '@lingui/react/macro';
33
import { useState } from 'react';
44
import { UserAPI, type UserType } from 'src/api';
55
import { DeleteModal } from 'src/components';
6-
import { useUserContext } from 'src/user-context';
6+
import { useAppContext } from 'src/app-context';
77
import { jsxErrorMessage, mapErrorMessages } from 'src/utilities';
88

99
interface IProps {
@@ -20,7 +20,7 @@ export const DeleteUserModal = ({
2020
user,
2121
}: IProps) => {
2222
const [waiting, setWaiting] = useState(false);
23-
const { credentials } = useUserContext();
23+
const { user: currentUser } = useAppContext();
2424

2525
if (!user || !isOpen) {
2626
return null;
@@ -30,11 +30,11 @@ export const DeleteUserModal = ({
3030
<DeleteModal
3131
cancelAction={() => closeModal(false)}
3232
deleteAction={() => deleteUser()}
33-
isDisabled={waiting || user.username === credentials.username}
33+
isDisabled={waiting || user.username === currentUser.username}
3434
spinner={waiting}
3535
title={t`Delete user?`}
3636
>
37-
{user.username === credentials.username ? (
37+
{user.username === currentUser.username ? (
3838
t`Deleting yourself is not allowed.`
3939
) : (
4040
<Trans>

src/components/error-boundary.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const ErrorBoundary = () => {
99
<h1>
1010
{error.status} {error.statusText}
1111
</h1>
12-
<p>{error.data}</p>
12+
<p>{error.data.toString()}</p>
1313
</>
1414
);
1515
} else if (error instanceof Error) {

src/containers/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export { default as ExecutionEnvironmentManifest } from './execution-environment
2222
export { default as ExecutionEnvironmentRegistryList } from './execution-environment/registry-list';
2323
export { default as GroupDetail } from './group-management/group-detail';
2424
export { default as GroupList } from './group-management/group-list';
25-
export { default as LoginPage } from './login/login';
2625
export { default as MyImports } from './my-imports/my-imports';
2726
export { default as NamespaceDetail } from './namespace-detail/namespace-detail';
2827
export { default as MyNamespaces } from './namespace-list/my-namespaces';

src/containers/login/login.tsx

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/containers/settings/user-profile.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Trans } from '@lingui/react/macro';
33
import { Button } from '@patternfly/react-core';
44
import { useEffect, useState } from 'react';
55
import { UserAPI, type UserType } from 'src/api';
6+
import { useAppContext } from 'src/app-context';
67
import {
78
AlertList,
89
type AlertType,
@@ -11,7 +12,6 @@ import {
1112
UserFormPage,
1213
closeAlert,
1314
} from 'src/components';
14-
import { useUserContext } from 'src/user-context';
1515
import {
1616
type ErrorMessagesType,
1717
type RouteProps,
@@ -28,10 +28,8 @@ function UserProfile(_props: RouteProps) {
2828
const [user, setUser] = useState<UserType>();
2929

3030
const {
31-
credentials: { username },
32-
updateUsername,
33-
updatePassword,
34-
} = useUserContext();
31+
user: { username },
32+
} = useAppContext();
3533

3634
const addAlert = (alert: AlertType) => {
3735
setAlerts([...alerts, alert]);
@@ -66,14 +64,6 @@ function UserProfile(_props: RouteProps) {
6664
variant: 'success',
6765
title: <Trans>Saved changes to user &quot;{username}&quot;.</Trans>,
6866
});
69-
70-
// update saved credentials when password of logged user is changed
71-
if (user.password) {
72-
updatePassword(user.password);
73-
}
74-
if (username !== user.username) {
75-
updateUsername(user.username);
76-
}
7767
})
7868
.catch((err) => setErrorMessages(mapErrorMessages(err)));
7969

0 commit comments

Comments
 (0)