Skip to content

Commit 5478a3e

Browse files
committed
feat(react): ✨ add useOn custom hook for onSignIn and onSignOut functions
1 parent a8a8ead commit 5478a3e

File tree

9 files changed

+134
-11
lines changed

9 files changed

+134
-11
lines changed

packages/react/src/components/SignIn/fragments/BasicAuth.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ const BasicAuth = ({
7272

7373
return (
7474
<UISignIn.Paper className="asgardeo-basic-auth-paper">
75-
<UISignIn.Typography title>{t(keys.login.login.heading)}</UISignIn.Typography>
75+
<UISignIn.Typography title className="basic-auth-title">
76+
{t(keys.login.login.heading)}
77+
</UISignIn.Typography>
7678

7779
{alert && (
7880
<UISignIn.Alert className="asgardeo-basic-auth-alert" {...alert?.alertType}>

packages/react/src/components/SignIn/fragments/LoginOptionsBox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {SignIn as UISignIn} from '../../../oxygen-ui-react-auth-components';
2929

3030
const images: {[key: string]: string} = {
3131
'Email OTP': emailSolid,
32-
Facebook: facebook,
32+
facebook: facebook,
3333
Github: github,
3434
Google: google,
3535
Microsoft: microsoft,

packages/react/src/hooks/use-authentication.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const useAuthentication = (): UseAuthentication => {
3737
const signOut: () => void = () => {
3838
signOutApiCall().then(() => {
3939
sessionStorage.clear();
40-
window.location.reload();
40+
if (contextValue.onSignOutRef.current) {
41+
contextValue.onSignOutRef.current();
42+
}
4143
});
4244
};
4345

packages/react/src/hooks/use-on.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
import {useContext, useEffect} from 'react';
20+
import AsgardeoContext from '../contexts/asgardeo-context';
21+
import AuthContext from '../models/auth-context';
22+
import {Hooks, UseOnProps} from '../models/use-on';
23+
import {AsgardeoUIException} from '@asgardeo/js-ui-core';
24+
25+
const useOn = (props: UseOnProps) => {
26+
const {callback, event} = props;
27+
28+
const contextValue: AuthContext = useContext(AsgardeoContext);
29+
30+
useEffect(() => {
31+
switch (event) {
32+
case Hooks.SignIn:
33+
contextValue.setOnSignIn(callback);
34+
break;
35+
case Hooks.SignOut:
36+
contextValue.setOnSignOut(callback);
37+
break;
38+
default:
39+
throw new AsgardeoUIException('REACT-USE_ON-UO-IV-01', 'Invalid event type provided.');
40+
}
41+
}, []);
42+
};
43+
44+
export default useOn;

packages/react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export * from './components/public-components';
2020
export * from './models/public-models';
2121
export {default as AsgardeoProvider} from './providers/AsgardeoProvider';
2222
export {default as useAuthentication} from './hooks/use-authentication';
23+
export {default as useOn} from './hooks/use-on';

packages/react/src/models/auth-context.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ interface AuthContext {
2626
isBrandingLoading: boolean;
2727
isGlobalLoading: boolean;
2828
isTextLoading: boolean;
29+
onSignOutRef: React.MutableRefObject<Function>;
2930
setAuthentication: () => void;
3031
setIsAuthLoading: (value: boolean) => void;
3132
setIsBrandingLoading: (value: boolean) => void;
3233
setIsTextLoading: (value: boolean) => void;
34+
setOnSignIn: (response?: any) => void | Promise<void>;
35+
setOnSignOut: (response?: any) => void | Promise<void>;
3336
user: MeAPIResponse;
3437
}
3538

packages/react/src/models/public-models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
*/
1818

1919
export type {UIAuthConfig} from '@asgardeo/js-ui-core';
20+
export {Hooks} from './use-on';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
export enum Hooks {
20+
SignIn,
21+
SignOut,
22+
}
23+
24+
export interface UseOnProps {
25+
/**
26+
* The callback to be executed when the event is triggered.
27+
*/
28+
callback: (response?: any) => void | Promise<void>;
29+
/**
30+
* The event to listen to.
31+
*/
32+
event: Hooks;
33+
}

packages/react/src/providers/AsgardeoProvider.tsx

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import {AuthClient, CryptoUtils, MeAPIResponse, Store, UIAuthClient, getProfileInformation} from '@asgardeo/js-ui-core';
20-
import {FC, PropsWithChildren, useCallback, useEffect, useMemo, useState} from 'react';
20+
import {FC, PropsWithChildren, useCallback, useEffect, useMemo, useRef, useState} from 'react';
2121
import BrandingPreferenceProvider from './BrandingPreferenceProvider';
2222
import I18nProvider from './I18nProvider';
2323
import AsgardeoContext from '../contexts/asgardeo-context';
@@ -52,6 +52,23 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = (
5252
const [isTextLoading, setIsTextLoading] = useState<boolean>(true);
5353
const [isAuthLoading, setIsAuthLoading] = useState<boolean>(false);
5454

55+
const onSignInRef: React.MutableRefObject<Function> = useRef<Function>();
56+
const onSignOutRef: React.MutableRefObject<Function> = useRef<Function>();
57+
58+
const setOnSignIn: (newOnSignIn: Function) => void = useCallback(
59+
(newOnSignIn: Function): void => {
60+
onSignInRef.current = newOnSignIn;
61+
},
62+
[], // Add any dependencies here...
63+
);
64+
65+
const setOnSignOut: (newOnSignOut: Function) => void = useCallback(
66+
(newOnSignOut: Function): void => {
67+
onSignOutRef.current = newOnSignOut;
68+
},
69+
[], // Add any dependencies here...
70+
);
71+
5572
const storeInstance: Store = store || new SessionStore();
5673

5774
const spaUtils: CryptoUtils = new SPACryptoUtils();
@@ -64,14 +81,20 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = (
6481
const setAuthentication: () => void = useCallback((): void => {
6582
authClient.isAuthenticated().then((isAuth: boolean) => {
6683
setIsAuthenticated(isAuth);
67-
});
6884

69-
authClient.getAccessToken().then((accessTokenFromClient: string) => {
70-
if (accessTokenFromClient) {
71-
setAccessToken(accessTokenFromClient);
85+
if (isAuth) {
86+
authClient.getAccessToken().then((accessTokenFromClient: string) => {
87+
if (accessTokenFromClient) {
88+
setAccessToken(accessTokenFromClient);
7289

73-
getProfileInformation().then((response: MeAPIResponse) => {
74-
setUser(response);
90+
getProfileInformation().then((response: MeAPIResponse) => {
91+
setUser(response);
92+
});
93+
94+
if (onSignInRef.current) {
95+
onSignInRef.current();
96+
}
97+
}
7598
});
7699
}
77100
});
@@ -105,13 +128,27 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = (
105128
isBrandingLoading,
106129
isGlobalLoading: isBrandingLoading || isTextLoading || isAuthLoading,
107130
isTextLoading,
131+
onSignOutRef,
108132
setAuthentication,
109133
setIsAuthLoading,
110134
setIsBrandingLoading,
111135
setIsTextLoading,
136+
setOnSignIn,
137+
setOnSignOut,
112138
user,
113139
}),
114-
[accessToken, config, isAuthLoading, isAuthenticated, isBrandingLoading, isTextLoading, setAuthentication, user],
140+
[
141+
accessToken,
142+
config,
143+
isAuthLoading,
144+
isAuthenticated,
145+
isBrandingLoading,
146+
isTextLoading,
147+
setAuthentication,
148+
setOnSignIn,
149+
setOnSignOut,
150+
user,
151+
],
115152
);
116153

117154
return (

0 commit comments

Comments
 (0)