Skip to content

Commit

Permalink
Remove iTwin Auth (#4)
Browse files Browse the repository at this point in the history
* update READMEs

* en: remove itwin auth requirement

* update readme
  • Loading branch information
wsm95 authored Dec 19, 2024
1 parent 50edd0d commit 8893ef7
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 42 deletions.
12 changes: 6 additions & 6 deletions common/src/itwin/cesiumCuratedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

export async function getCuratedCesiumContent(iTwinId: string, imsPrefix: string, accessToken: string) {
export async function getCuratedCesiumContent(imsPrefix: string, accessToken: string) {
const headers = {
"Authorization": accessToken,
"Accept": "application/vnd.bentley.itwin-platform.v1+json",
"Content-Type": "application/json"
};

let url = `https://${imsPrefix}api.bentley.com/curated-content/cesium/?iTwinId=${iTwinId}`;
let url = `https://${imsPrefix}api.bentley.com/curated-content/cesium/`;

const response = await fetch(url, { headers });
const responseJson = await response.json();

return responseJson;
}

export async function getCesiumCuratedContentTiles(contentId: string, iTwinId: string, imsPrefix: string, accessToken: string) {
export async function getCesiumCuratedContentTiles(contentId: string, imsPrefix: string, accessToken: string) {
const headers = {
"Authorization": accessToken,
"Accept": "application/vnd.bentley.itwin-platform.v1+json",
"Content-Type": "application/json"
};

let url = `https://${imsPrefix}api.bentley.com/curated-content/cesium/${contentId}/tiles?iTwinId=${iTwinId}`;
let url = `https://${imsPrefix}api.bentley.com/curated-content/cesium/${contentId}/tiles`;

const response = await fetch(url, { headers });
const responseJson = await response.json();

return { url: responseJson.url, accessToken: responseJson.accessToken, attributions: responseJson.attributions };
}

export async function getCesiumMoonTerrianTiles(iTwinId: string, imsPrefix: string, accessToken: string) {
export async function getCesiumMoonTerrianTiles(imsPrefix: string, accessToken: string) {
const headers = {
"Authorization": accessToken,
"Accept": "application/vnd.bentley.itwin-platform.v1+json",
"Content-Type": "application/json"
};

let url = `https://${imsPrefix}api.bentley.com/curated-content/cesium/2684829/tiles?iTwinId=${iTwinId}`;
let url = `https://${imsPrefix}api.bentley.com/curated-content/cesium/2684829/tiles`;

const response = await fetch(url, { headers });
const responseJson = await response.json();
Expand Down
1 change: 0 additions & 1 deletion r3f-sample/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
VITE_CLIENT_ID="**YOUR CLIENT ID**"
VITE_IMS_PREFIX =""
VITE_ITWIN_ID="**OPTIONAL ITWIN ID**"
3 changes: 1 addition & 2 deletions r3f-sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ In a .env file in the `r3f-sample` directory:

- `VITE_CLIENT_ID` - SPA Application Client ID needed to sign in with Bentley IMS (required)
- `VITE_IMS_PREFIX` - Bentley IMS authority prefix (optional)
- `VITE_ITWIN_ID` - iTwin Id of an iTwin. You will need to have permission to read this iTwin (optional, if no Id is provided the user's primary account iTwin will be used)

For an example of the `.env` file, an one has been provided [here](./.env.example)
For an example of the `.env` file, one has been provided [here](./.env.example)

## Authentication

Expand Down
4 changes: 2 additions & 2 deletions r3f-sample/src/globe.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { BentleyCesiumCuratedContentPlugin } from './plugins/BentleyCesiumCurate
import { Canvas } from '@react-three/fiber';
import { Environment } from '@react-three/drei';

export function Globe ({ accessToken, itwinId, imsPrefix }) {
export function Globe ({ accessToken, imsPrefix }) {
return (
<Canvas
frameloop='demand'
Expand All @@ -35,7 +35,7 @@ export function Globe ({ accessToken, itwinId, imsPrefix }) {
<color attach="background" args={["#241100"]} />

<TilesRenderer key={accessToken} group={ { rotation: [ - Math.PI / 2, 0, 0 ] } }>
<TilesPlugin plugin={ BentleyCesiumCuratedContentPlugin } args={ {accessToken, itwinId, imsPrefix} } />
<TilesPlugin plugin={ BentleyCesiumCuratedContentPlugin } args={ {accessToken, imsPrefix} } />
<GlobeControls enableDamping={ true } />
<TilesAttributionOverlay />
</TilesRenderer>
Expand Down
19 changes: 2 additions & 17 deletions r3f-sample/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import { createRoot } from 'react-dom/client';

import { Globe } from './globe';
import { getBentleyAuthClient } from "common";
import { ITwinsAccessClient } from "@itwin/itwins-client";

const imsPrefix = import.meta.env.VITE_IMS_PREFIX ?? "";
const clientId = import.meta.env.VITE_CLIENT_ID;

function App() {
const [accessToken, setAccessToken] = useState('');
const [itwinId, setItwinId] = useState(import.meta.env.VITE_ITWIN_ID);

useEffect( () => {
const fetchAccessToken = async () => {
Expand All @@ -28,24 +26,11 @@ function App() {
fetchAccessToken();
}, [] );

useEffect( () => {
const fetchiTwinId = async () => {
if(accessToken && !itwinId) {
const iTwinsAccessClient = new ITwinsAccessClient(`https://${imsPrefix}api.bentley.com/itwins`);
const iTwinsResponse = await iTwinsAccessClient.getPrimaryAccountAsync(accessToken);

setItwinId(iTwinsResponse.data?.id);
}
}

fetchiTwinId();
}, [accessToken] );

if (!accessToken || !itwinId) {
if (!accessToken) {
return <div>Loggin' in...</div>;
}

return <Globe accessToken={accessToken} itwinId={itwinId} imsPrefix={imsPrefix}/>;
return <Globe accessToken={accessToken} imsPrefix={imsPrefix}/>;
}

createRoot( document.getElementById( 'root' ) ).render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class BentleyCesiumCuratedContentPlugin {
}

init(tiles) {
tiles.rootURL = `https://${this.imsPrefix}api.bentley.com/curated-content/cesium/96188/tiles?iTwinId=${this.itwinId}`;
tiles.rootURL = `https://${this.imsPrefix}api.bentley.com/curated-content/cesium/96188/tiles`;

this.tiles = tiles;
this.endpointURL = tiles.rootURL;
Expand Down
1 change: 0 additions & 1 deletion threejs-sample/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
VITE_CLIENT_ID="**YOUR CLIENT ID**"
VITE_IMS_PREFIX =""
VITE_ITWIN_ID="**OPTIONAL ITWIN ID**"
3 changes: 1 addition & 2 deletions threejs-sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ In a .env file in the `threejs-sample` directory:

- `VITE_CLIENT_ID` - SPA Application Client ID needed to sign in with Bentley IMS (required)
- `VITE_IMS_PREFIX` - Bentley IMS authority prefix (optional)
- `VITE_ITWIN_ID` - iTwin Id of an iTwin. You will need to have permission to read this iTwin (optional, if no Id is provided the user's primary account iTwin will be used)

For an example of the `.env` file, an one has been provided [here](./.env.example)
For an example of the `.env` file, one has been provided [here](./.env.example)

## Authentication

Expand Down
11 changes: 1 addition & 10 deletions threejs-sample/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as THREE from "three";
import { getBentleyAuthClient, getCesiumMoonTerrianTiles } from "common";
import { GUI } from "lil-gui";
import { TilesRenderer, LUNAR_ELLIPSOID, GlobeControls } from "3d-tiles-renderer";
import { ITwinsAccessClient, ITwin, ITwinsAPIResponse} from "@itwin/itwins-client";

const imsPrefix = import.meta.env.VITE_IMS_PREFIX ?? "";
const clientId = import.meta.env.VITE_CLIENT_ID;
Expand All @@ -20,15 +19,7 @@ if (!clientId) {
const authClient = await getBentleyAuthClient(clientId, imsPrefix);
const accessToken = await authClient.getAccessToken();

let iTwinId = import.meta.env.VITE_ITWIN_ID;
if (!iTwinId) {
const iTwinsAccessClient: ITwinsAccessClient = new ITwinsAccessClient(`https://${imsPrefix}api.bentley.com/itwins`);
const iTwinsResponse: ITwinsAPIResponse<ITwin> = await iTwinsAccessClient.getPrimaryAccountAsync(accessToken);

iTwinId = iTwinsResponse.data?.id;
}

const cesiumMoonTerrianTiles = await getCesiumMoonTerrianTiles(iTwinId, imsPrefix, accessToken);
const cesiumMoonTerrianTiles = await getCesiumMoonTerrianTiles(imsPrefix, accessToken);

var camera = {} as THREE.PerspectiveCamera;
var controls = {} as GlobeControls;
Expand Down

0 comments on commit 8893ef7

Please sign in to comment.