Skip to content

cdn provider #4

Closed
Closed

Description

@bourgeoa

Is there a way to use your code from a cdn provider ?

Activity

  1. uvdsl commented on Apr 26, 2025

    @uvdsl
    Owner

    Hi! 👋
    I have not played around with this yet - but the npm package is bundled (and there is a minified bundle).

    So, you could try:

    For the minified version...

    <script type="module" src="https://unpkg.com/@uvdsl/[email protected]/dist/esm/index.min.js"></script>

    And the regular version...

    <script type="module" src="https://unpkg.com/@uvdsl/[email protected]/dist/esm/index.js"></script>

    And let me know if this works?

  2. uvdsl commented on Apr 30, 2025

    @uvdsl
    Owner

    A quick example to show that usage via a CDN (@bourgeoa) and usage in a multi-page application works (@jg10-mastodon-social). Just serve the two files on a webserver:

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Solid Login Page</title>
        <script type="module" src="https://unpkg.com/@uvdsl/[email protected]/dist/esm/index.min.js"></script>
        <style>
            body {
                font-family: Arial, sans-serif;
                max-width: 600px;
                margin: 0 auto;
                padding: 20px;
                text-align: center;
            }
            button {
                background-color: #4CAF50;
                border: none;
                color: white;
                padding: 15px 32px;
                text-align: center;
                text-decoration: none;
                display: inline-block;
                font-size: 16px;
                margin: 10px;
                cursor: pointer;
                border-radius: 4px;
            }
            .container {
                margin-top: 100px;
                padding: 20px;
                border-radius: 8px;
                box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Solid Login Demo</h1>
            <p>Click the button below to log in with your Solid identity provider</p>
            <button id="loginButton">Login with Solid</button>
            <button id="navigateButton">Go to Welcome Page</button>
            <button id="logoutButton">Logout</button>
        </div>
    
        <script>
            // Initialize the session
            let session;
    
            document.addEventListener('DOMContentLoaded', async () => {
                // Import the Session class from the library
                const module = await import('https://unpkg.com/@uvdsl/[email protected]/dist/esm/index.min.js');
                const Session = module.Session;
                
                // Create a new session
                session = new Session();
                
                // Set up the login button
                document.getElementById('loginButton').addEventListener('click', () => {
                    // Use a default IDP or let user specify one
                    const idp = "https://solidcommunity.net"; // Default IDP - you can change this
                    const redirect_uri = window.location.href;
                    
                    // Redirect to login
                    session.login(idp, redirect_uri);
                });
    
                  // Set up the logout button
                  document.getElementById('logoutButton').addEventListener('click', () => {
                    session.logout();
                    alert("Logout! \nWebID: " + session.webId);
                });
                
                // Set up the navigation button
                document.getElementById('navigateButton').addEventListener('click', () => {
                    // Navigate to the welcome page
                    window.location.href = 'welcome.html';
                });
                
                // Handle redirect after login
                try {
                    await session.handleRedirectFromLogin();
                    if (session.webId) {
                        console.log("Logged in successfully:", session.webId);
                        alert("Login successful! \nWebID: " + session.webId);
                    }
                } catch (error) {
                    console.error("Error handling login redirect:", error);
                }
            });
        </script>
    </body>
    </html>

    welcome.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Welcome Page</title>
        <script type="module" src="https://unpkg.com/@uvdsl/[email protected]/dist/esm/index.min.js"></script>
        <style>
            body {
                font-family: Arial, sans-serif;
                max-width: 600px;
                margin: 0 auto;
                padding: 20px;
                text-align: center;
            }
            .container {
                margin-top: 100px;
                padding: 20px;
                border-radius: 8px;
                box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            }
            .user-info {
                margin-top: 20px;
                padding: 10px;
                background-color: #f8f9fa;
                border-radius: 4px;
                text-align: left;
            }
            button {
                background-color: #4CAF50;
                border: none;
                color: white;
                padding: 10px 20px;
                text-align: center;
                text-decoration: none;
                display: inline-block;
                font-size: 16px;
                margin: 10px;
                cursor: pointer;
                border-radius: 4px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Hello!</h1>
            <div id="welcome-message">Welcome to the application</div>
            <div id="user-info" class="user-info">
                <p>WebID: <span id="webid">Not logged in</span></p>
            </div>
            <button id="back-button">Back to Login Page</button>
        </div>
    
        <script>
            document.addEventListener('DOMContentLoaded', async () => {
                // Import the Session class from the library
                const module = await import('https://unpkg.com/@uvdsl/[email protected]/dist/esm/index.min.js');
                const Session = module.Session;
                
                // Create a new session
                const session = new Session();
                
                // Try to restore the session
                try {
                    await session.handleRedirectFromLogin();
                    
                    // Update the UI
                    if (session.webId) {
                        document.getElementById('webid').textContent = session.webId;
                        document.getElementById('welcome-message').textContent = 
                            `Welcome! You are logged in.`;
                    } else {
                        document.getElementById('welcome-message').textContent = 
                            "You're not logged in. Please return to the login page.";
                    }
                } catch (error) {
                    console.error("Error restoring session:", error);
                    document.getElementById('welcome-message').textContent = 
                        "Error restoring session. Please try logging in again.";
                }
                
                // Set up the back button
                document.getElementById('back-button').addEventListener('click', () => {
                    window.location.href = 'index.html';
                });
            });
        </script>
    </body>
    </html>

    Does this work for you?

  3. uvdsl commented on May 3, 2025

    @uvdsl
    Owner

    @bourgeoa, you said on gitter that this example works for you - can we close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions