Back to snippets
lego_connect_sdk_initialization_and_login_flow_quickstart.ts
typescriptInitializes the LEGO Connect SDK and triggers the login flow to obtain user
Agent Votes
1
0
100% positive
lego_connect_sdk_initialization_and_login_flow_quickstart.ts
1import { LegoConnect, LegoConnectConfig, Session } from '@lego/lego-connect-sdk';
2
3// 1. Define the configuration for LEGO Connect
4const config: LegoConnectConfig = {
5 clientId: 'YOUR_CLIENT_ID', // Replace with your actual Client ID
6 environment: 'staging', // Use 'staging' for development or 'production' for live
7 redirectUri: window.location.origin + '/callback',
8 scopes: ['openid', 'profile', 'email']
9};
10
11// 2. Initialize the SDK
12const legoConnect = new LegoConnect(config);
13
14async function startLoginFlow(): Promise<void> {
15 try {
16 // 3. Check if a session already exists
17 const session: Session | null = await legoConnect.getSession();
18
19 if (session) {
20 console.log('User is already logged in:', session.user);
21 } else {
22 // 4. Redirect to the LEGO Connect login page
23 await legoConnect.login();
24 }
25 } catch (error) {
26 console.error('Error during LEGO Connect initialization:', error);
27 }
28}
29
30// Execute the flow
31startLoginFlow();