Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Oct 25, 2023
1 parent 4b9f7e1 commit e0202fa
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
28 changes: 11 additions & 17 deletions expose/client/src/component/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {getAuthRedirect, unsetAuthRedirect} from "../lib/oauth";
import {UserInfoResponse, AuthEventHandler} from "@alchemy/auth";
import {DashboardMenu} from "@alchemy/react-ps";
import {oauthClient} from "../lib/api-client";
import config from "../lib/config";
Expand All @@ -18,32 +19,25 @@ type Props = {};

export default function App({}: Props) {
const { pushInstruction } = useMatomo();
const [user, setUser] = React.useState<UserInfoResponse | null>(null);
const [user, setUser] = React.useState<UserInfoResponse | undefined>();

const authenticate = React.useCallback(async () => {
if (user) {
return;
}

const res = await oauthClient.authenticate();
setUser(res);
setUser(oauthClient.isAuthenticated() ? oauthClient.getDecodedToken()! : undefined);
}, [user]);

React.useEffect(() => {
pushInstruction('setUserId', user?.user_id || null);
pushInstruction('setUserId', user?.sub || null);
}, [user]);

const onLogin = React.useCallback<AuthEventHandler>(async (event) => {
await authenticate();
}, [authenticate]);
const onLogout = React.useCallback<AuthEventHandler>(async (event) => {
setUser(null);
setUser(undefined);
}, []);

React.useEffect(() => {
if (oauthClient.getAccessToken()) {
authenticate();
}
authenticate();
oauthClient.registerListener('login', onLogin);
oauthClient.registerListener('logout', onLogout);

Expand All @@ -53,7 +47,7 @@ export default function App({}: Props) {
}
}, [onLogin, onLogout, authenticate]);

const css = config.get('globalCSS');
const css = config.globalCSS;

return <Router>
<AnalyticsRouterProvider>
Expand All @@ -71,11 +65,11 @@ export default function App({}: Props) {
/>}/>
<Route path="/:publication" exact render={props => <PublicationRoute
{...props}
username={this.state.username}
username={user?.preferred_username}
/>}/>
<Route path="/:publication/:asset" exact render={props => <AssetRoute
{...props}
username={this.state.username}
username={user?.preferred_username}
/>}/>
<Route path="/" exact render={() => <ErrorPage
title={'Not found'}
Expand All @@ -86,9 +80,9 @@ export default function App({}: Props) {
</Router>
}

const OAuthR = props => {
const OAuthR = (props: {}) => {
return <OAuthRedirect
{...props}
{...props as any}
oauthClient={oauthClient}
successHandler={(history) => {
const redirectUri = getAuthRedirect() || '/';
Expand Down
2 changes: 1 addition & 1 deletion lib/js/auth/src/client/OAuthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ValidationError {
error_description: string;
}

type UserInfoResponse = {
export type UserInfoResponse = {
preferred_username: string;
groups: string[];
roles: string[];
Expand Down
2 changes: 2 additions & 0 deletions lib/js/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import OAuthClient, {
configureClientAuthentication,
configureClientCredentialsGrantType,
RefreshTokenEvent,
UserInfoResponse,
AuthEventHandler,
LoginEvent,
AuthEvent,
Expand Down Expand Up @@ -36,6 +37,7 @@ export {
RefreshTokenEvent,
AuthEventHandler,
LoginEvent,
UserInfoResponse,
AuthEvent,
LogoutEvent,
MemoryStorage,
Expand Down
10 changes: 0 additions & 10 deletions lib/js/react-ps/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,3 @@ export {
DashboardMenu,
useEffectOnce,
};

export type {
TokenResponse,
UserInfoResponse,
AuthEvent,
LoginEvent,
AuthenticationEvent,
LogoutEvent,
AuthEventHandler,
} from "./lib/oauth-client";

0 comments on commit e0202fa

Please sign in to comment.