-
Notifications
You must be signed in to change notification settings - Fork 36
feat(react): add providers & custom hooks #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c11bfd3
57aadea
5b32856
c0bd7e8
d17f0b0
41be6db
8c949e1
d8f21aa
34fad5f
cb1129e
f0e60a6
83cc49b
16346d4
f95920a
92a994f
b620fc0
7280565
0090aa9
23a3785
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,14 @@ | |
* under the License. | ||
*/ | ||
|
||
import {AuthClientConfig} from '@asgardeo/auth-js'; | ||
import merge from 'lodash.merge'; | ||
import {TextObject} from './screens/model'; | ||
import getBrandingPreferenceText from '../api/get-branding-preference-text'; | ||
import {AuthClient} from '../auth-client'; | ||
import AsgardeoUIException from '../exception'; | ||
import {UIAuthConfig} from '../models/auth-config'; | ||
import {BrandingPreferenceTypes} from '../models/branding-api-response'; | ||
import {BrandingPreferenceTextAPIResponse} from '../models/branding-text-api-response'; | ||
import GetLocalizationProps from '../models/get-localization-props'; | ||
|
||
|
@@ -31,20 +34,22 @@ import GetLocalizationProps from '../models/get-localization-props'; | |
* @returns {Promise<Customization>} A promise that resolves with the merged branding properties. | ||
*/ | ||
const getLocalization = async (props: GetLocalizationProps): Promise<TextObject> => { | ||
const {componentCustomization, locale, providerCustomization, screen} = props; | ||
const {componentTextOverrides, locale, providerTextOverrides, screen} = props; | ||
|
||
const module: TextObject = await import(`./screens/${screen}/${locale}.ts`); | ||
|
||
let textFromConsoleBranding: BrandingPreferenceTextAPIResponse; | ||
|
||
const configData: AuthClientConfig<UIAuthConfig> = await AuthClient.getInstance().getDataLayer().getConfigData(); | ||
|
||
try { | ||
if ((await AuthClient.getInstance().getDataLayer().getConfigData()).enableConsoleTextBranding ?? true) { | ||
textFromConsoleBranding = await getBrandingPreferenceText( | ||
if (configData.enableConsoleTextBranding ?? true) { | ||
textFromConsoleBranding = await getBrandingPreferenceText({ | ||
locale, | ||
providerCustomization.name, | ||
name: configData.name ?? 'carbon.super', | ||
screen, | ||
providerCustomization.type, | ||
); | ||
type: configData.type ?? BrandingPreferenceTypes.Org, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's check and nullable checks if required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wil address this in the next PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The config cannot be nullable. Application cannot reach this far, if this is nullable. I think it is expected to break, in case this becomes nullable. |
||
}); | ||
} | ||
} catch (error) { | ||
throw new AsgardeoUIException( | ||
|
@@ -69,11 +74,11 @@ const getLocalization = async (props: GetLocalizationProps): Promise<TextObject> | |
/** | ||
* PRIORITY 02: Text from provider customization | ||
*/ | ||
providerCustomization?.preference?.text?.[locale]?.[screen] ?? {}, | ||
providerTextOverrides?.[locale]?.[screen] ?? {}, | ||
/** | ||
* PRIORITY 01: Text from component customization | ||
*/ | ||
componentCustomization?.preference?.text?.[locale]?.[screen] ?? {}, | ||
componentTextOverrides?.[locale]?.[screen] ?? {}, | ||
); | ||
|
||
return mergedText; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
}, | ||
"devDependencies": { | ||
"@types/node": "^20.12.7", | ||
"@types/randombytes": "^2.0.3", | ||
"@types/react": "^18.2.79", | ||
"@types/react-dom": "^18.2.25", | ||
"@wso2/eslint-plugin": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/eslint-plugin?fa0b844715320a3953d6d055997c0770f8695082", | ||
|
@@ -46,12 +47,17 @@ | |
"sass": "^1.75.0", | ||
"stylelint": "15.1.0", | ||
"tslib": "^2.6.2", | ||
"typescript": "^5.4.5" | ||
"typescript": "5.1.6" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. caret |
||
}, | ||
"dependencies": { | ||
"@asgardeo/js-ui-core": "*", | ||
"@oxygen-ui/react": "^1.11.0", | ||
"clsx": "^2.1.1" | ||
"base64url": "^3.0.1", | ||
"buffer": "^6.0.3", | ||
"clsx": "^2.1.1", | ||
"fast-sha256": "^1.3.0", | ||
"jose": "^5.3.0", | ||
"randombytes": "^2.1.0" | ||
}, | ||
"peerDependencies": { | ||
"react": ">=18.0.0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {Context, createContext} from 'react'; | ||
import AuthContext from '../models/auth-context'; | ||
|
||
const AsgardeoContext: Context<AuthContext> = createContext<AuthContext>(undefined); | ||
|
||
export default AsgardeoContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {BrandingProps} from '@asgardeo/js-ui-core'; | ||
import {Context, createContext} from 'react'; | ||
|
||
const BrandingPreferenceContext: Context<BrandingProps> = createContext<BrandingProps>(undefined); | ||
|
||
export default BrandingPreferenceContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {Context, createContext} from 'react'; | ||
import {I18n} from '../models/i18n'; | ||
|
||
const I18nContext: Context<I18n> = createContext<I18n>(undefined); | ||
|
||
export default I18nContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {signOut as signOutApiCall} from '@asgardeo/js-ui-core'; | ||
import {useContext} from 'react'; | ||
import AsgardeoContext from '../contexts/asgardeo-context'; | ||
import AuthContext from '../models/auth-context'; | ||
import UseAuthentication from '../models/use-authentication'; | ||
|
||
/** | ||
* `useAuthentication` is a custom hook that provides access to the authentication context. | ||
* It returns an object containing the current user, the authentication status, the access token, and a sign out function. | ||
* | ||
* @returns {UseAuthentication} An object containing the current user (`user`), the authentication status (`isAuthenticated`), | ||
* the access token (`accessToken`), and a sign out function (`signOut`). | ||
*/ | ||
const useAuthentication = (): UseAuthentication => { | ||
const contextValue: AuthContext = useContext(AsgardeoContext); | ||
|
||
const {user, isAuthenticated, accessToken} = contextValue; | ||
|
||
const signOut: () => void = () => { | ||
signOutApiCall(); | ||
sessionStorage.clear(); | ||
window.location.reload(); | ||
}; | ||
|
||
return {accessToken, isAuthenticated, signOut, user}; | ||
}; | ||
|
||
export default useAuthentication; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {UIAuthConfig} from '@asgardeo/js-ui-core'; | ||
import {useContext} from 'react'; | ||
import AsgardeoContext from '../contexts/asgardeo-context'; | ||
import UseConfig from '../models/use-config'; | ||
|
||
/** | ||
* Custom hook to access the authentication configuration from the AsgardeoProviderContext. | ||
* @returns An object containing the authentication configuration. | ||
*/ | ||
export const useConfig = (): UseConfig => { | ||
const {config} = useContext(AsgardeoContext) as { | ||
config: UIAuthConfig; | ||
}; | ||
|
||
return {config}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move to a seperate constant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wil address this in the next PR.