Skip to content

Commit

Permalink
feat: display user list on table
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Jul 16, 2024
1 parent d39e9da commit 56741a7
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 20 deletions.
26 changes: 26 additions & 0 deletions client/hooks/useCognitoClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider';
import type { DefaultConfigs } from 'common/types/api';
import { atom, useAtom, useAtomValue } from 'jotai';
import { NEXT_PUBLIC_API_ORIGIN } from 'utils/envValues';

const defaultsAtom = atom<DefaultConfigs | { [Key in keyof DefaultConfigs]?: undefined }>({});
const clientAtom = atom((get) => {
const defaults = get(defaultsAtom);

return new CognitoIdentityProviderClient(
defaults.accessKey
? {
endpoint: NEXT_PUBLIC_API_ORIGIN,
region: defaults.region,
credentials: { accessKeyId: defaults.accessKey, secretAccessKey: defaults.secretKey },
}
: {},
);
});

export const useCognitoClient = () => {
const [defaults, setDefaults] = useAtom(defaultsAtom);
const cognitoClient = useAtomValue(clientAtom);

return { defaults, setDefaults, cognitoClient };
};
1 change: 0 additions & 1 deletion client/layouts/BasicHeader/BasicHeader.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
height: 100%;
padding: 0 16px;
margin: 0 auto;
user-select: none;
}

.userBtn {
Expand Down
28 changes: 19 additions & 9 deletions client/pages/_app.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import '@aws-amplify/ui-react/styles.css';
import { Amplify } from 'aws-amplify';
import { I18n } from 'aws-amplify/utils';
import { AuthLoader } from 'components/Auth/AuthLoader';
import { useCognitoClient } from 'hooks/useCognitoClient';
import type { AppProps } from 'next/app';
import dynamic from 'next/dynamic';
import { useEffect, useState } from 'react';
import { useEffect, useMemo } from 'react';
import { apiClient } from 'utils/apiClient';
import { catchApiErr } from 'utils/catchApiErr';
import { NEXT_PUBLIC_API_ORIGIN } from 'utils/envValues';
import '../styles/globals.css';

Expand All @@ -15,21 +17,29 @@ I18n.setLanguage('ja');

function MyApp({ Component, pageProps }: AppProps) {
const SafeHydrate = dynamic(() => import('../components/SafeHydrate'), { ssr: false });
const [configured, setConfigured] = useState(false);
const { defaults, setDefaults } = useCognitoClient();

useEffect(() => {
apiClient.public.defaults.$get().then((defaults) => {
Amplify.configure({
Auth: { Cognito: { ...defaults, userPoolEndpoint: NEXT_PUBLIC_API_ORIGIN } },
});
useMemo(() => {
if (defaults.userPoolId === undefined) return;

setConfigured(true);
Amplify.configure({
Auth: {
Cognito: {
userPoolId: defaults.userPoolId,
userPoolClientId: defaults.userPoolClientId,
userPoolEndpoint: NEXT_PUBLIC_API_ORIGIN,
},
},
});
}, [defaults]);

useEffect(() => {
apiClient.public.defaults.$get().then(setDefaults).catch(catchApiErr);
}, []);

return (
<SafeHydrate>
{configured && (
{defaults.userPoolId && (
<Authenticator.Provider>
<AuthLoader />
<Component {...pageProps} />
Expand Down
39 changes: 38 additions & 1 deletion client/pages/console/index.page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
import type { ListUsersResponse } from '@aws-sdk/client-cognito-identity-provider';
import { ListUsersCommand } from '@aws-sdk/client-cognito-identity-provider';
import type { UserEntity } from 'common/types/user';
import { useCognitoClient } from 'hooks/useCognitoClient';
import { Layout } from 'layouts/Layout';
import { useEffect, useState } from 'react';
import { catchApiErr } from 'utils/catchApiErr';
import styles from './index.module.css';

const Main = (_: { user: UserEntity }) => {
const { defaults, cognitoClient } = useCognitoClient();
const [users, setUsers] = useState<ListUsersResponse['Users']>();

useEffect(() => {
cognitoClient
.send(new ListUsersCommand({ UserPoolId: defaults.userPoolId }))
.then((res) => setUsers(res.Users))
.catch(catchApiErr);
}, []);

return (
<div className={styles.container}>
<div className={styles.main}>
<div className={styles.card}></div>
<div className={styles.card}>
<table>
<caption>User List</caption>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{users?.map((user) => (
<tr key={user.Username}>
<td>{user.Attributes?.find((attr) => attr.Name === 'sub')?.Value}</td>
<td>{user.Username}</td>
<td>{user.Attributes?.find((attr) => attr.Name === 'email')?.Value}</td>
<td>{user.UserStatus}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
Expand Down
20 changes: 15 additions & 5 deletions server/api/public/defaults/controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { DEFAULT_USER_POOL_CLIENT_ID, DEFAULT_USER_POOL_ID } from 'service/envValues';
import { returnSuccess } from 'service/returnStatus';
import {
ACCESS_KEY,
DEFAULT_USER_POOL_CLIENT_ID,
DEFAULT_USER_POOL_ID,
REGION,
SECRET_KEY,
} from 'service/envValues';
import { defineController } from './$relay';

export default defineController(() => ({
get: () =>
returnSuccess({
get: () => ({
status: 200,
body: {
userPoolId: DEFAULT_USER_POOL_ID,
userPoolClientId: DEFAULT_USER_POOL_CLIENT_ID,
}),
region: REGION,
accessKey: ACCESS_KEY,
secretKey: SECRET_KEY,
},
}),
}));
6 changes: 2 additions & 4 deletions server/api/public/defaults/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { DefineMethods } from 'aspida';
import type { EntityId } from 'common/types/brandedId';
import type { DefaultConfigs } from 'common/types/api';

export type Methods = DefineMethods<{
get: {
resBody: { userPoolId: EntityId['userPool']; userPoolClientId: EntityId['userPoolClient'] };
};
get: { resBody: DefaultConfigs };
}>;
9 changes: 9 additions & 0 deletions server/common/types/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { EntityId } from './brandedId';

export type DefaultConfigs = {
userPoolId: EntityId['userPool'];
userPoolClientId: EntityId['userPoolClient'];
region: string;
accessKey: string;
secretKey: string;
};

0 comments on commit 56741a7

Please sign in to comment.