Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cookiless Embed example #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions visual-embed/auth/cookieless/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
VITE_THOUGHTSPOT_HOST=https://training.thoughtspot.cloud
VITE_THOUGHT_SPOT_USERNAME=code-sandbox
VITE_THOUGHT_SPOT_PASSWORD="3mbed+#3xplz"

#VITE_SERVER_URL=http://localhost:4000
25 changes: 25 additions & 0 deletions visual-embed/auth/cookieless/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
!.env
41 changes: 41 additions & 0 deletions visual-embed/auth/cookieless/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# cookieless-embed

This example demonstrates how to implement cookieless authentication for embedding ThoughtSpot using the Visual Embed SDK.

## Demo

Open in [Codesandbox](https://githubbox.com/thoughtspot/developer-examples/tree/main/visual-embed/auth/cookieless-embed)

## Documentation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no codesandbox, please follow the template or use the npm run new-example utility

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update

- [Cookieless Authentication Examples](https://developers.thoughtspot.com/docs/trusted-auth-sdk#_cookieless_authentication_examples)

## Run locally

```
$ git clone https://github.com/thoughtspot/developer-examples
$ cd visual-embed/auth/cookieless-embed
```
```
$ npm i
```
```
$ npm run dev
```

## Code Structure

### Source Code
```
src
├── App.tsx -> Contains the embed code using @thoughtspot/visual-embed-sdk
├── services
│ └── token.ts -> Handles fetching auth token from backend
```

## Technology labels

- React
- Typescript
- Web

59 changes: 59 additions & 0 deletions visual-embed/auth/cookieless/api/token-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import express from "express";
import { createConfiguration, ServerConfiguration, ThoughtSpotRestApi } from "@thoughtspot/rest-api-sdk"
import cors from "cors";

const app = express();

const PORT = process.env.VITE_SERVER_PORT || 4000;
const THOUGHTSPOT_HOST = process.env.VITE_THOUGHTSPOT_HOST || 'https://training.thoughtspot.cloud';


let thoughtspotClient: ThoughtSpotRestApi;
const getThoughtClient = () => {

if (!thoughtspotClient) {
const thoughtspotServer = new ServerConfiguration(THOUGHTSPOT_HOST, {});
const basicClientConfig = createConfiguration({
baseServer: thoughtspotServer,
});

thoughtspotClient = new ThoughtSpotRestApi(basicClientConfig);
}
return thoughtspotClient;

}

app.use(express.json());
app.use(cors())

const username = process.env.VITE_THOUGHT_SPOT_USERNAME
const password = process.env.VITE_THOUGHT_SPOT_PASSWORD

if (!username || !password) {
throw new Error('Username and password are required');
}

app.get('/api/token', async (req, res) => {
try {
const thoughtspotClient = getThoughtClient();
const data = await thoughtspotClient.getFullAccessToken({
username,
password,
});

res.status(200).json({ token: data.token });
}
catch (e) {
console.error(e);
res.status(500).json({ error: e.message });
}
});

app.all('/', function (req, res) {
res.json({ message: "Hello, world!" });
});

app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on", `https://localhost:${PORT}`);
});
13 changes: 13 additions & 0 deletions visual-embed/auth/cookieless/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/ts-logo.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ThoughtSpot React + TS Example</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading