1.0.104 • Published 5 months ago

@corbado/webcomponent v1.0.104

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

@corbado/webcomponent

The @corbado/webcomponent JavaScript library empowers developers to build seamless passkey-first authentication into their applications. It facilitates user sign-up, login, and session management operations directly from your JavaScript (Frontend).

Installation

You can install @corbado/webcomponent either as an ES module via npm, or load it as a script in your HTML. Both methods are discussed below.

Install as an ES module

Install the package with npm

npm install @corbado/webcomponent

After installing, import Corbado into your JavaScript files and initialize it with your Frontend API.

Install as a script

To load Corbado directly in your HTML using a <script/> tag, add the following scripts with your Project ID:

<script src="https://<Project ID>.frontendapi.corbado.io/auth.js"></script>
<!-- Only required for Vanilla HTML / JS, see below-->
<script src="https://<Project ID>.frontendapi.corbado.io/utility.js"></script>

Usage

The usage of @corbado/webcomponent differs slightly depending on your choice of JavaScript or a frontend framework. Examples are provided below for:

React

Use @corbado/webcomponent in your React components:

// ... your other imports
import Corbado from '@corbado/webcomponent';
import '@corbado/webcomponent/pkg/auth_cui.css';

const corbado = new Corbado.Session('<project ID>');

function App() {
    const session = new Corbado.Session("<Project ID>");
    let [currentUser, setCurrentUser] = useState(null);

    useEffect(() => {
        session.refresh(user => {
            setCurrentUser(user);
        });
    }, [session]);

    const handleLogout = async () => {
       await session.logout();
    }

    return (
        <div>
            <corbado-auth project-id="<Project ID>" conditional="yes">
                <input id="corbado-username" autoComplete="webauthn" name="username" required/>
            </corbado-auth>
            <button onClick={handleLogout}>Logout</button>
        </div>
    )
}

Vue.js

Use corbado-auth-provider and corbado-auth tags in your Vue templates:

<template>
  <corbado-auth-provider project-id="<Project ID>">
    <corbado-auth project-id="<Project ID>" conditional="yes">
      <input id="corbado-username" autocomplete="webauthn" name="username" required/>
    </corbado-auth>
  </corbado-auth-provider>
</template>

<script>
import "@corbado/webcomponent"
import "@corbado/webcomponent/pkg/auth_cui.css"

export default {
  name: 'App',
  setup() {
    return {
      session: new Corbado.Session('<Project ID>'),
    }
  },
  mounted() {
    // Register a callback for session refresh
    // in order to receive an authentication event
    this.session.refresh(user => {
      // Here, you can define what happens when the session is initialized
      // if user is null, then the user is not logged in
    });
  },
  methods: {
    handleLogout() {
      this.session.logout()
          .then(() => {
            // Here, you can define what happens after the user is logged out
          })
          .catch(err => {
            console.error(err);
          });
    }
  }
}
</script>

Vue.js 3 setup:

export default defineConfig(({command, mode, ssrBuild}) => {
    return {
        build: {
            sourcemap: command === 'build',
        },
        plugins: [
            vue({
                template: {
                    transformAssetUrls,
                    compilerOptions: {
                        isCustomElement: (tag) => tag.startsWith('corbado-')
                    },
                },
            }),
            // https://github.com/vuetifyjs/vuetify-loader/tree/next/packages/vite-plugin
            vuetify({
                autoImport: true,

            }),
        ],
        define: {'process.env': {}},
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            },
            extensions: [
                '.js',
                '.json',
                '.jsx',
                '.mjs',
                '.ts',
                '.tsx',
                '.vue',
            ],
        },
        server: {
            port: 3000,
        },
    }
})

Vue.js 2 setup:

import {i18n} from '@/plugins/i18n'
import '@/plugins/vue-composition-api'
import '@/styles/styles.scss'
import Vue from 'vue'
import App from './App.vue'
import './plugins/acl'
import vuetify from './plugins/vuetify'
import router from './router'
import store from './store'

Vue.config.productionTip = false
Vue.config.ignoredElements = [
    'corbado-auth',
]

new Vue({
    router,
    store,
    i18n,
    vuetify,
    render: h => h(App),
}).$mount('#app')

HTML custom elements

Include corbado-auth-provider and corbado-auth tags in your HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Corbado</title>
</head>
<body>
<script src="https://<Project ID>.frontendapi.corbado.io/auth.js"></script>
<!-- Use the corbado-auth-provider custom element in your HTML. -->
<corbado-auth-provider project-id="<Project ID>">
    <!-- Only displayed if the user is not logged in -->
    <div slot="unauthed">
        <corbado-auth project-id="<%Project ID>" conditional="yes">
            <input name="username" id="corbado-username"
                   data-input="username" required
                   autocomplete="webauthn"/>
        </corbado-auth>
    </div>

    <!-- Only displayed if the user is logged in -->
    <div slot="authed">
        You're logged in.
        <corbado-logout-handler project-id="<Project ID>"
                                redirect-url="https://www.acme.com">
            <button>Logout</button>
        </corbado-logout-handler>
    </div>

    <!-- Always displayed -->
    <div>
        You're logged in.
        <corbado-logout-handler project-id="<Project ID>"
                                redirect-url="https://www.acme.com">
            <button>Logout</button>
        </corbado-logout-handler>
    </div>
</corbado-auth-provider>
</body>
</html>

Vanilla HTML / JS

Include corbado-auth-provider and corbado-auth tags in your HTML:

<!DOCTYPE html>
<html>
<head>
<title>Corbado</title>
<!-- Import Corbado SDK -->
</head>
<body>
<script src="https://<Project ID>.frontendapi.corbado.io/auth.js"></script>
<script src="https://<Project ID>.frontendapi.corbado.io/utility.js"></script>
    <!-- Logout button -->
    <button id="logoutButton">Logout</button>

    <script>
        // Create a new session
        const session = new Corbado.Session('<Project ID>');

        // Register a callback for session refresh
        // in order to receive an authentication event
        session.refresh(user => {
            // Here, you can define what happens when the session is initialized
            // if user is null, then the user is not logged in
        });

        // Get logout button
        const logoutButton = document.getElementById('logoutButton');

        // Add event listener to logout button
        logoutButton.addEventListener('click', function() {
            session.logout()
                .then(() => {
                    // Here, you can define what happens after the user is logged out
                })
                .catch(err => {
                    console.error(err);
                });
        });
    </script>
</body>
</html>

Getting help

Have questions or need help? Here's how you can reach us:

Security

@corbado/webcomponent follows good practices of security, but 100% security cannot be assured.

@corbado/webcomponent is provided "as is" without any warranty. Use at your own risk.

1.0.105-beta

5 months ago

1.0.103

5 months ago

1.0.104

5 months ago

1.0.81-beta

6 months ago

1.0.62

8 months ago

1.0.61

8 months ago

1.0.60

8 months ago

1.0.66

7 months ago

1.0.65

7 months ago

1.0.64

8 months ago

1.0.63

8 months ago

1.0.84-beta

6 months ago

1.0.69

7 months ago

1.0.68

7 months ago

1.0.87-beta

6 months ago

1.0.67

7 months ago

1.0.56-beta

8 months ago

1.0.100-beta

6 months ago

1.0.78-beta

6 months ago

1.0.73

7 months ago

1.0.72

7 months ago

1.0.71

7 months ago

1.0.70

7 months ago

1.0.77

6 months ago

1.0.76

6 months ago

1.0.74

6 months ago

1.0.55-beta

8 months ago

1.0.39

10 months ago

1.0.90-beta

6 months ago

1.0.38

10 months ago

1.0.58-beta

8 months ago

1.0.88-beta

6 months ago

1.0.40

9 months ago

1.0.44

9 months ago

1.0.43

9 months ago

1.0.42

9 months ago

1.0.41

9 months ago

1.0.48

8 months ago

1.0.47

8 months ago

1.0.46

9 months ago

1.0.45-beta

9 months ago

1.0.45

9 months ago

1.0.49

8 months ago

1.0.75-beta

6 months ago

1.0.89-beta

6 months ago

1.0.51

8 months ago

1.0.50

8 months ago

1.0.91-beta

6 months ago

1.0.59

8 months ago

1.0.101

5 months ago

1.0.102

5 months ago

1.0.54-beta

8 months ago

1.0.104-beta

5 months ago

1.0.57-beta

8 months ago

1.0.79-beta

6 months ago

1.0.80-beta

6 months ago

1.0.86-beta

6 months ago

1.0.37

10 months ago

1.0.36

10 months ago

1.0.35

10 months ago

1.0.85-beta

6 months ago

1.0.99-beta

6 months ago

1.0.83

6 months ago

1.0.82

6 months ago

1.0.98-beta

6 months ago

1.0.103-beta

5 months ago

1.0.53-beta

8 months ago

1.0.91

6 months ago

1.0.52-beta

8 months ago

1.0.95

6 months ago

1.0.94

6 months ago

1.0.93

6 months ago

1.0.92

6 months ago

1.0.83-beta

6 months ago

1.0.97

6 months ago

1.0.96

6 months ago

1.0.33

10 months ago

1.0.34

10 months ago

1.0.29

11 months ago

1.0.28

11 months ago

1.0.27

11 months ago

1.0.32

11 months ago

1.0.31

11 months ago

1.0.30

11 months ago

1.0.25

11 months ago

1.0.24

11 months ago

1.0.23-beta

11 months ago

1.0.22-beta

11 months ago

1.0.20

11 months ago

1.0.19

11 months ago

1.0.18

11 months ago

1.0.17-debug

11 months ago

1.0.17

11 months ago

1.0.16

11 months ago

1.0.15

11 months ago

1.0.14

11 months ago

1.0.13

11 months ago

1.0.12

11 months ago

1.0.11

12 months ago

1.0.10

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago