Implementing babylonjs playground in my own server

Hi,

I’m trying to setup babylonjs playground in my server. As of now I have setup the snippet server as per the documentation (Make your own Snippet Server | Babylon.js Documentation). Now I’m trying to deploy the playground.
I’ve cloned the babylonjs repository and run babylon-server, playground and nme in production mode using webpack.

Now I need a way to implement authentication to access the hosted playground. How can I setup that?

As of now I’m loading the playground in an frontend app I setup using iframe. But I have difficulty in showing the file ids once the file is saved to the user as my hosted babylonjs is embeded in an iframe.

Moreover I’m stuck in thinkin how would I communicate my auth state to my snippet server via my hosted version of babylonjs playground.

Any help on how to setup this correctly would help me a lot.

Thanks in advance

Did you ask chatGPT?
I hope the answers are usefull…

I’m trying to setup babylonjs playground in my server. As of now I have setup the snippet server as per the documentation (Make your own Snippet Server | Babylon.js Documentation 1). Now I’m trying to deploy the playground.
I’ve cloned the babylonjs repository and run babylon-server, playground and nme in production mode using webpack.

GPT:
To implement authentication in your hosted Babylon.js Playground, you can use a variety of authentication methods such as OAuth2, OpenID Connect, or JSON Web Tokens (JWTs). Here are some general steps you can follow:

  1. Choose an authentication method: As mentioned earlier, there are several authentication methods to choose from. Consider the level of security and complexity you need for your application when choosing an authentication method.
  2. Implement authentication in your server-side code: You will need to implement authentication in your server-side code, which should be running the Babylon.js Playground. This may involve configuring a middleware that checks for a valid token or redirecting to a login page.
  3. Integrate authentication in your frontend app: Once the user logs in, you need to send the token to the frontend app. One way to do this is by storing the token in a cookie and accessing it from your frontend app. Another option is to pass the token in the URL parameters.
  4. Use the token to access the Playground API: When making requests to the Babylon.js Playground API, you should include the token in the request headers to authenticate the user. You can use the Authorization header with the Bearer scheme and the token value.
  5. Implement file ID communication: To show the file IDs once the file is saved to the user, you can pass the file ID in the response body when the file is saved. Then, you can communicate this information to your frontend app using a WebSocket or any other real-time communication method.
  6. Communicate auth state to Snippet Server: To communicate the auth state to your Snippet Server, you can pass the token to the server in the request headers when making requests to the server’s API. You may need to modify the server-side code to handle the token and check for its validity.

I hope these steps help you get started with implementing authentication in your hosted Babylon.js Playground. Let me know if you have any further questions!

As of now I’m loading the playground in an frontend app I setup using iframe. But I have difficulty in showing the file ids once the file is saved to the user as my hosted babylonjs is embeded in an iframe.

If you are having difficulty showing the file IDs when the file is saved to the user because the hosted Babylon.js Playground is embedded in an iframe, you can use the postMessage() API to communicate between the iframe and the parent window.

Here are the steps you can follow:

  1. In your Babylon.js Playground code, after the file is saved, send a message to the parent window with the file ID using window.parent.postMessage(). For example:

javascriptCopy code

window.parent.postMessage({ fileId: 1234 }, '*');

This sends a message to the parent window with the file ID 1234.

  1. In your frontend app that is hosting the Babylon.js Playground iframe, listen for the message using the window.addEventListener() method. For example:

javascriptCopy code

window.addEventListener('message', function(event) {
  if (event.data.fileId) {
    // Do something with the file ID, such as display it to the user.
    console.log('File ID:', event.data.fileId);
  }
});

This listens for any messages received from the iframe and checks if it has a fileId property. If it does, it displays the file ID to the user.

By using postMessage(), you can communicate between the iframe and the parent window without having to worry about cross-origin restrictions.

Moreover I’m stuck in thinkin how would I communicate my auth state to my snippet server via my hosted version of babylonjs playground.

To communicate the authentication state from your hosted version of Babylon.js Playground to your Snippet Server, you can send an API request with the token in the request headers.

Here are the steps you can follow:

  1. When a user logs in to your frontend app, store the token in a cookie or local storage.
  2. In your Babylon.js Playground code, before making any API requests to your Snippet Server, retrieve the token from the cookie or local storage and include it in the request headers. You can use the XMLHttpRequest or fetch API to make the API requests. For example:

javascriptCopy code

const token = localStorage.getItem('token');
const headers = new Headers({ 'Authorization': `Bearer ${token}` });
fetch('https://your-snippet-server.com/api/endpoint', { headers })
  .then(response => response.json())
  .then(data => {
    // Handle the response data.
  })
  .catch(error => {
    // Handle the error.
  });

This sends an API request to the Snippet Server with the token in the Authorization header.

  1. In your Snippet Server code, retrieve the token from the request headers and verify its validity. You can use a middleware function to do this for all API requests. For example:

javascriptCopy code

function authenticate(req, res, next) {
  const token = req.headers.authorization.split(' ')[1];
  // Verify the token and set the user ID in the request object.
  // If the token is invalid, return an error response.
  next();
}

This middleware function retrieves the token from the Authorization header, verifies it, and sets the user ID in the request object. You can then use this user ID to perform actions on behalf of the authenticated user.

By including the token in the request headers, you can authenticate API requests to your Snippet Server from your hosted version of Babylon.js Playground.

I tried this. But whatever edits I make to the source file, It is not getting reflected while serving.I used instructions from Babylon.js/buildSystem.md at master · BabylonJS/Babylon.js · GitHub 's run the playground section

I know only one working way to solve problems: Repetition

I go through the steps again and again and gather information, analyse the data I found, then I make a hypothesis, verify my assumptions, and if everything still doesn’t work I start over again.

In the next run, new insights emerge.

Good luck! :slight_smile:

1 Like

Does the terminal show any errors?

1 Like

No errors are shown.

I tried editing this file. ran the build in the babylon.js root folder and served babylon server and plalyground.

Then I have another react app that has an iframe to load the playground.
This is the basic setup.

As in the image I tried to add a return statement to prevent any playground from loading. But still the pg loads. So I think the file I edited has something to do with this!

Ooooh, ok, I see what’s happening! This playground.ts file on the babylonServer package is not the one that renders the Playground, it’s the playground.tsx file on the tools/playground package. I’m not sure why do we have the file on babylonServer, maybe @RaananW knows?

playground.ts is just a simplified version of the playground parser - it takes a snippet and converts it to code, and runs it if possible. It is the code responsible for being able to use the hash-system the playground uses in the babylon server as well.