Skip to content

Commit

Permalink
changes configuration to implement frontendAPI and backendAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Dopeamin committed Sep 13, 2024
1 parent 38ebad4 commit 817e1ed
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ The Corbado Node.js SDK provides the following services:
To use a specific service, such as `sessions`, invoke it as shown below:

```JavaScript
corbado.sessions().getAndValidateCurrentUser(req);
corbado.sessions().validateToken(req);
```

## :books: Advanced
Expand Down
14 changes: 7 additions & 7 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configuration

The Corbado Node.js SDK uses a configuration object to manage various settings. This object is created using the `Config` class, which takes two parameters: `projectID` and `apiSecret`.
The Corbado Node.js SDK uses a configuration object to manage various settings. This object is created using the `Config` class, which takes 4 parameters: `projectID`, `apiSecret`, `frontendAPI` and `backendAPI`.

## Creating a Configuration Object

Expand All @@ -11,25 +11,25 @@ import { Config } from '@corbado/node-sdk';

const projectID = process.env.CORBADO_PROJECT_ID;
const apiSecret = process.env.CORBADO_API_SECRET;
const frontendAPI = process.env.CORBADO_FRONTEND_API;
const backendAPI = process.env.CORBADO_BACKEND_API;

const config = new Config(projectID, apiSecret);
const config = new Config(projectID, apiSecret, frontendAPI, backendAPI);
```

## Validation in Config Class

The `Config` class validates the `projectID` and `apiSecret` parameters. The `projectID` must start with 'pro-', and the `apiSecret` must start with 'corbado1'. If these conditions are not met, an error is thrown.
The `Config` class validates the `projectID`, `apiSecret`, `frontendAPI` and `backendAPI` parameters. The `projectID` must start with 'pro-', the `apiSecret` must start with 'corbado1', both APIs should be domain names and the pathname should be empty. If these conditions are not met, an error is thrown.

## Config Class Properties

The `Config` class also sets several other properties:

- `BackendAPI`: The base URL for the backend API. By default, this is set to `https://backendapi.corbado.io`.
- `FrontendAPI`: The base URL for the frontend API. This is generated by replacing [projectID] in the default frontend API URL with the provided projectID.
- `ShortSessionCookieName`: The name of the short session cookie. By default, this is set to `cbo_short_session`.
- `CacheMaxAge`: The maximum age for the cache. By default, this is set to 60000 milliseconds (1 minute).
- `JWTIssuer`: The issuer for the JWT. This is generated by appending `/.well-known/jwks` to the FrontendAPI.
These properties are used throughout the SDK to configure various services. For example, the `BackendAPI`, `projectID`, and `apiSecret` are used to create an Axios client in the SDK class.

### nvironment Variables
### Environment Variables

Remember to set the `CORBADO_PROJECT_ID` and `CORBADO_API_SECRET` environment variables in your project. These are used to create the `Config` object.
Remember to set the `CORBADO_PROJECT_ID`, `CORBADO_API_SECRET`, `CORBADO_FRONTEND_API` and `CORBADO_BACKEND_API` environment variables in your project. These are used to create the `Config` object.
23 changes: 6 additions & 17 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ConfigInterface {
}

export const DefaultClient = axios.create();
export const DefaultBackendAPI = 'https://backendapi.cloud.corbado.io/v2';
export const DefaultBackendAPI = 'https://backendapi.cloud.corbado.io';
export const DefaultFrontendAPI = 'https://[projectID].frontendapi.cloud.corbado.io';
export const DefaultShortSessionCookieName = 'cbo_short_session';
export const DefaultCacheMaxAge = 10 * 60 * 1000; // 10 * 60 * 1000 = 60000 milliseconds, which is equivalent to 10 minutes.
Expand All @@ -22,9 +22,7 @@ class Config implements ConfigInterface {

APISecret: string;

FrontendAPI: string;

FrontendAPIWithCName: string;
FrontendAPI: string = DefaultFrontendAPI;

BackendAPI: string = DefaultBackendAPI;

Expand All @@ -34,25 +32,16 @@ class Config implements ConfigInterface {

CacheMaxAge: number = DefaultCacheMaxAge;

constructor(projectID: string, apiSecret: string, cname?: string) {
constructor(projectID: string, apiSecret: string, frontendAPI: string, backendAPI: string) {
this.validateProjectID(projectID);
this.validateAPISecret(apiSecret);
Assert.validURL(frontendAPI, 'frontendAPI');
Assert.validURL(backendAPI, 'backendAPI');

this.ProjectID = projectID;
this.APISecret = apiSecret;
this.Client = DefaultClient;
this.FrontendAPI = DefaultFrontendAPI.replace('[projectID]', projectID);
this.FrontendAPIWithCName = cname ?? this.FrontendAPI;
}

public setFrontendAPI(frontendApi: string): void {
Assert.validURL(frontendApi, 'frontendApi');
this.FrontendAPI = frontendApi;
}

public setBackendAPI(backendAPI: string): void {
Assert.validURL(backendAPI, 'backendAPI');
this.BackendAPI = backendAPI;
this.FrontendAPI = frontendAPI;
}

public setShortSessionCookieName(shortSessionCookieName: string): void {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Assert {
);

validate(
parsedUrl.pathname !== '/' && parsedUrl.pathname !== '/v2',
parsedUrl.pathname !== '/',
`${errorName} URL path assertion failed`,
INVALID_URL.code,
'path needs to be empty',
Expand Down
4 changes: 2 additions & 2 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SDK {

this.session = new Session(
config.ShortSessionCookieName,
config.FrontendAPIWithCName,
config.FrontendAPI,
`${config.FrontendAPI}/.well-known/jwks`,
config.CacheMaxAge,
config.ProjectID,
Expand All @@ -32,7 +32,7 @@ class SDK {

createClient(config: Config): AxiosInstance {
const instance = axios.create({
baseURL: config.BackendAPI,
baseURL: `${config.BackendAPI}/v2`,
auth: {
username: config.ProjectID,
password: config.APISecret,
Expand Down

0 comments on commit 817e1ed

Please sign in to comment.