Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/core/src/api/branding-preference-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {BrandingPreferenceTextAPIResponse} from 'src/models/branding-text-api-re
* @returns A Promise that resolves to the response from the server.
* @throws {AsgardeoUIException} If the API call fails or when the response is not successful.
*/
export const getBrandingPreferenceText = async (
const getBrandingPreferenceText = async (
locale: string,
name: string,
screen: string,
Expand Down Expand Up @@ -71,3 +71,5 @@ export const getBrandingPreferenceText = async (
'Failed to receive a successful response from the branding text endpoint',
);
};

export default getBrandingPreferenceText;
74 changes: 74 additions & 0 deletions packages/core/src/api/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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 {AuthClient} from 'src/auth-client';
import AsgardeoUIException from 'src/exception';
import {MeAPIResponse} from 'src/models/me-api-response';

/**
* Fetch the profile information of the authenticated user.
*
* This function uses the `AuthClient` instance to get the base URL and access token,
* and then makes a GET request to the `/scim2/Me` endpoint to fetch the user's profile information.
*
* @returns {Promise<MeAPIResponse>} A promise that resolves to an object containing the user's profile information.
* @throws {AsgardeoUIException} Throws an exception if there's an error getting the base URL and access token, or if the fetch request fails.
*/
const getProfileInformation = async (): Promise<MeAPIResponse> => {
let baseUrl: string;
let accessToken: string;
let response: Response;

try {
baseUrl = (await AuthClient.getInstance().getDataLayer().getConfigData()).baseUrl;
accessToken = await AuthClient.getInstance().getAccessToken();
} catch (error) {
throw new AsgardeoUIException(
'JS_UI_CORE-ME-GPI-NF',
'Failed in getting the base URL and access token.',
error.stack,
);
}

if (!accessToken) {
throw new AsgardeoUIException('JS_UI_CORE-ME-GPI-IV', 'Access token is null.');
}

const headers: Headers = new Headers();
headers.append('Authorization', `Bearer ${accessToken}`);
headers.append('Content-Type', 'application/json');

const requestOptions: RequestInit = {
headers,
method: 'GET',
};

try {
response = await fetch(new Request(`${baseUrl}/scim2/Me`, requestOptions));
} catch (error) {
throw new AsgardeoUIException('JS_UI_CORE-ME-GPI-NE', 'Me API call failed.', error.stack);
}

if (response.ok) {
return (await response.json()) as MeAPIResponse;
}

throw new AsgardeoUIException('JS_UI_CORE-ME-GPI-HE', 'Failed to receive a successful response from the Me API.');
};

export default getProfileInformation;
23 changes: 23 additions & 0 deletions packages/core/src/api/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.
*/

export {default as authorize} from './authorize';
export {default as authenticate} from './authenticate';
export {default as getBrandingPreference} from './branding-preference';
export {default as getProfileInformation} from './profile';
export {default as getBrandingPreferenceText} from './branding-preference-text';
2 changes: 2 additions & 0 deletions packages/core/src/branding/branding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ export const getBranding = async (props: GetBranding): Promise<Customization> =>

return mergedBranding;
};

export default getBranding;
2 changes: 1 addition & 1 deletion packages/core/src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import merge from 'lodash.merge';
import {getBrandingPreferenceText} from 'src/api/branding-preference-text';
import getBrandingPreferenceText from 'src/api/branding-preference-text';
import {AuthClient} from 'src/auth-client';
import {BrandingPreferenceTextAPIResponse} from 'src/models/branding-text-api-response';
import {GetLocalization, TextObject} from './screens/model';
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/i18n/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@

export * from './screens/model';
export * from './screens/keys';
export {default as getLocalization} from './i18n';
23 changes: 23 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.
*/

export * from './api/public-api';
export {default as getBranding} from './branding/branding';
export * from './i18n/public';
export * from './auth-client';
export * from './models/public-models';
63 changes: 63 additions & 0 deletions packages/core/src/models/me-api-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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.
*/

/**
* The interface for the response from the "me" API endpoint.
*/
export interface MeAPIResponse {
/**
* The user's email addresses.
*/
emails: string[];
/**
* The user's id.
*/
id: string;
/**
* The user's names.
*/
name: Name;
/**
* When signed in using a social login, the photos field will be populated instead of profile URL.
*/
photos?: Photos[];
/**
* The user's profile URL.
*/
profileUrl: string;
/**
* The user's username.
*/
userName: string;
}

/**
* The interface for the name field in the "me" API response.
*/
export interface Name {
familyName?: string;
givenName?: string;
}

/**
* The interface for the photos field in the "me" API response.
*/
export interface Photos {
type: string;
value: string;
}
25 changes: 25 additions & 0 deletions packages/core/src/models/public-models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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.
*/

export * from './auth-api-request';
export * from './auth-api-response';
export * from './auth-config';
export * from './branding-api-response';
export * from './customization';
export * from './me-api-response';
export * from './screen-type';