## Vanilla.js embedded login quickstart This guide walks you through integrating Frontegg’s login box into your JavaScript app, with built-in support for Sign In, Sign Up, SSO, and automatic redirects—all in just a few lines of code. br Check out the sample package on GitHub br Getting your Frontegg subdomain and `clientId` Frontegg creates a unique subdomain and client id for every environment created on the account. In order to retrieve the clientId `subdomain` that will act as the `baseUrl` in the integration, navigate to your workspace `Keys & domains` menu, and copy the Frontegg domain and `clientId`. br ### Step 1: Adding Frontegg to your app Frontegg can be added to your app via npm / script on your application head. ``` npm install @frontegg/js ``` ``` yarn add @frontegg/js ``` ```html ... ``` ### Step 2: Configure Frontegg works with Context options and needs to be initialized with this context. In order to initialize Frontegg use the following code snippet #### Add the following script to initialize Frontegg on your application and to Interact with the login page See [`contextOptions`](/ciam/sdks/components/fronteggappoptions#contextoptions) for more information. ```typescript import {initialize} from "@frontegg/js" const style = document.createElement('style'); style.setAttribute('type', 'text/css'); style.innerHTML = ''; document.getElementsByTagName('head')[0].appendChild(style); const app = initialize({ contextOptions: { baseUrl: "https://[YOUR_SUBDOMAIN].frontegg.com", //set your Frontegg environment domain and client ID here appId: '[YOUR-APP-ID]' }, authOptions: { keepSessionAlive: true // Keeps the session alive by refreshing the JWT in the background }, hostedLoginBox: false }) ``` See [`keepSessionAlive`](/ciam/sdks/components/fronteggstoreoptions#keepsessionalive) and [`hostedLoginBox`](/ciam/sdks/components/fronteggappoptions#hostedloginbox) for more information. br Add the representative HTML button that will redirect the UI to the login page. Clicking on the button should take you to the login dialog. ```html Title ``` ### Step 3: Get the user context The user context is available from the Frontegg app. Upon initialization, you can subscribe to state notifications which will include whether the user is authenticated or not and what is the JWT of the user. In order to subscribe to user state notifications add the following code snippet: ```typescript import {initialize} from "@frontegg/js" const style = document.createElement('style'); style.setAttribute('type', 'text/css'); style.innerHTML = ''; document.getElementsByTagName('head')[0].appendChild(style); const app = initialize({ contextOptions: { baseUrl: "https://[YOUR_SUBDOMAIN].frontegg.com", //set your Frontegg environment domain and client ID here }, hostedLoginBox: false }) app.store.subscribe(() => { const state = app.store.getState(); if (state.auth.user) { document.getElementById('user-container').innerText = state.auth.user.email; } else { document.getElementById('user-container').innerText = 'Not Authenticated' } document.getElementById('app-root').style.display = state.auth.isLoading ? 'hidden' : 'block' let styleHtml = '' if (state.auth.isAuthenticated) { styleHtml += '[fe-state="isAuthenticated"] { }'; styleHtml += '[fe-state="!isAuthenticated"] { display: none; }'; } else { styleHtml += '[fe-state="isAuthenticated"] { display: none; }'; styleHtml += '[fe-state="!isAuthenticated"] { }'; } style.innerHTML = styleHtml; }) ``` br Great, Frontegg is now integrated with your app!