Files
webstudio/apps/builder/app/shared/entri/entri-api.server.ts
T
Ivan StarkovandOleg Isonen c534eb6965 Entri (#1738)
## Description

Implements entri service support

## Steps for reproduction

Wait for SaaS release. Or locally add following env variables to the
`.env` file
```
ENTRI_APPLICATION_ID=webstudio
ENTRI_SECRET=$(bwget WS_ENTRI_SECRET)
```

<img width="361" alt="image"
src="https://github.com/webstudio-is/webstudio-builder/assets/5077042/eafcc7ec-1bc7-448f-be78-50ae89909d34">


<img width="724" alt="image"
src="https://github.com/webstudio-is/webstudio-builder/assets/5077042/6d97a08b-1763-4ca4-970f-96836b4d5cd2">



## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio-builder/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env.example`
and the `builder/env-check.js` if mandatory

---------

Co-authored-by: Oleg Isonen <oleg008@gmail.com>
2023-06-12 16:10:27 +04:00

48 lines
1.1 KiB
TypeScript

import { z } from "zod";
import env from "~/env/env.server";
const EntryResponse = z.object({
auth_token: z.string(),
});
/**
* Short lived JWT token for Entri API
* https://developers.entri.com/docs/install#3-fetch-the-json-web-token-jwt
*/
const getEntriToken = async () => {
const { ENTRI_APPLICATION_ID, ENTRI_SECRET } = env;
if (ENTRI_SECRET === undefined) {
throw new Error(`ENTRI_SECRET is not defined`);
}
const entryResponse = await fetch("https://api.goentri.com/token", {
method: "POST",
body: JSON.stringify({
// These values come from the Entri dashboard
applicationId: ENTRI_APPLICATION_ID,
secret: ENTRI_SECRET,
}),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
if (entryResponse.ok === false) {
throw new Error(`Entri API error: ${await entryResponse.text()}`);
}
const entryJson = await entryResponse.json();
const responseData = EntryResponse.parse(entryJson);
return {
token: responseData.auth_token,
applicationId: ENTRI_APPLICATION_ID,
} as const;
};
export const entryApi = {
getEntriToken,
};