0.3.0 • Published 1 year ago

@wral/studio-mod_auth v0.3.0

Weekly downloads
-
License
UNLICENSED
Repository
bitbucket
Last release
1 year ago

Authentication Mod for Studio App

This is a mod for Studio App.

Installation

Include in your Studio App HTML implementation:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Your Studio App</title>
    <script type="module" src="https://cdn.wral.studio/studio-app.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <studio-app>

        <!-- Hello, World! Studio Mod -->
        <studio-mod src="https://cdn.wral.studio/mods/auth/release/latest/auth.es.js"></studio-mod>
    
    </studio-app>
</body>
</html>

Usage

The auth mod subscribes to the following:

  • studio.wral::mod-auth::token-request, payload: { callback: (token, error) => {} }

The auth mod provides bearer tokens to other mods by request. Other mods can request a token by publishing a token-request event and providing a callback.

/**
 * @param {Studio} studio
 * @return {Promise<string>} bearer token
 */
async function getToken(studio) {
    return new Promise((resolve, reject) => {
        studio.pub('studio.wral::mod-auth', 'token-request', {
            callback: (token, error) => {
                if (error) {
                    reject(error);
                } else {
                    resolve(token);
                }
            },
        });
    });
}