Skip to content

Commit

Permalink
Feature: Basic wizard example (#35)
Browse files Browse the repository at this point in the history
* adds basic example

* adds basic example

* resolves comments
  • Loading branch information
Dopeamin authored Sep 17, 2024
1 parent 163e6ea commit a8d6e0d
Show file tree
Hide file tree
Showing 11 changed files with 2,494 additions and 2,843 deletions.
96 changes: 96 additions & 0 deletions examples/basic/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import express from 'express';

const app = express();
app.use(express.urlencoded({ extended: false }));

//////////////////////////////////////////////////////////////////////////////////////////////
// Basic example which serves as basis for code snippets for integration guides //
//////////////////////////////////////////////////////////////////////////////////////////////

import { Config, SDK } from '@corbado/node-sdk';
import { ValidationError } from '@corbado/node-sdk/errors';

//////////////////////////////////////////////////////////////////////////////////////////////
// Instantiate SDK //
//////////////////////////////////////////////////////////////////////////////////////////////

// Configuration
const projectID = '<Your Project ID>';
const apiSecret = '<Your API secret>';
const frontendAPI = '<Frontend API URL>';
const backendAPI = '<Backend API URL>';

// Create SDK instance
const config = new Config(projectID, apiSecret, frontendAPI, backendAPI);
const sdk = new SDK(config);

app.get('/', async (_, res) => {
//////////////////////////////////////////////////////////////////////////////////////////////
// Protecting routes //
//////////////////////////////////////////////////////////////////////////////////////////////

// Retrieve the short-term session value from the Cookie (e.g. req.cookies.cbo_short_session)
const shortTermSessionValue = '<Your short-term session value>';

if (!shortTermSessionValue) {
// If the short-term session value is empty (e.g. the cookie is not set or
// expired), the user is not authenticated. e.g. redirect to login page.

console.log('User not authenticated');
res.redirect('/login');
}

let user;

try {
user = await sdk.sessions().validateToken(shortTermSessionValue);

console.log(`User with ID ${user.userId} is authenticated!`);
} catch (err) {
if (err instanceof ValidationError) {
// Handle the user not being authenticated, e.g. redirect to login page

console.log(err.message, err.statusCode);
res.redirect('/login');
return;
}

console.error(err);
res.status(500).send(err.message);
return;
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Getting user data from short-term session (represented as JWT) //
//////////////////////////////////////////////////////////////////////////////////////////////

user = await sdk.sessions().validateToken(shortTermSessionValue);

console.log('UserID', user.userId);
console.log('Full Name', user.fullName);

//////////////////////////////////////////////////////////////////////////////////////////////
// Getting user data from Corbado Backend API //
//////////////////////////////////////////////////////////////////////////////////////////////

user = await sdk.sessions().validateToken(shortTermSessionValue);

const fullUser = await sdk.users().get(user.userId);

const fullName = fullUser.fullName;
const userStatus = fullUser.status;

console.log('User full name', fullName);
console.log('User status', userStatus);

// To get the email we use the IdentifierService
const identifiersList = await sdk.identifiers().listByUserIdAndType(user.userId, 'email');

console.log('Email', identifiersList.identifiers[0].value);

res.status(200).send('Success');
});

app.listen(8000, () => {
console.log('Server running on port 8000');
});
Loading

0 comments on commit a8d6e0d

Please sign in to comment.