Set up Your App by adding App Logo and App Name, and Add At least 1 callback URL.
Regenerate your AppSecret, then copy appId and appSecret For Later Use.
When you create a new app, your app will be in Sandbox Mode, meaning only the developer or test users, if any, can log in with their email. To make your app accessible to the public, remember to request App Review after completing your app integration.
Replace {your-app-id} with your appId and {your-callback-url} with your callback URL From Create Your First App Step.
When users click the login button on your website, they are redirected to the hosted login page URL. After completing the login process, they will be redirected back to your website via the callback URL, which includes a sessionId query parameter for the user session to capture.For Example:
If your appId is 4y83w4f And your callback URL is https://mywebsite.com/login/callback Then the hosted login URL will be https://bawabah.app/login?appId=4y83w4f&callbackUrl=https://mywebsite.com/login/callback&fullscreen=true
If you followed the last step (Setup Hosted Login) no need to set up a login form. Alternatively, if you wish to host the login form on your own website, you can follow the guide on Form Login.
After installing the npm Package, we can start using it immediately:
Copy
// Import Bawabah Libraryimport Bawabah from 'bawabah';// Initialize Bawabah with your credentialsconst bw = new Bawabah();bw.init({ appId: "your-app-id", appSecret: "your-app-secret"});
Replace {your-app-id} with your appId and {your-app-secret}with your appSecret From Create Your First App Step.
Now we will create the callback route to capture the user session. In this example, we will use Express.js to create the route, then capture the user session with the capture() function:But before that, we need to install Express.js using this command:
Copy
npm i express
Copy
// Import Express and Bawabah Libraryimport express from 'express';import Bawabah from 'bawabah';// Initialize Bawabah with your credentialsconst bw = new Bawabah();bw.init({ appId: "your-app-id", appSecret: "your-app-secret"});// Create Express appconst app = express();const PORT = process.env.PORT || 3000;// Callback route for Bawabah redirectapp.get('/login/callback', async (req, res) => { const sessionId = req.query.sessionId as string; // Get Session Id from Query /login/callback?sessionId=abc123 // Capture user session from Bawabah const result = await bw.capture(sessionId); console.log(result); // Log User Data -- for Debugging -- res.json({ loggedIn: true });});// Start the Express serverapp.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`);});
Finally, after testing the implementation, we can add our Login/Signup logic and wrap the code in a try/catch block to catch any errors:
Copy
// Import Express and Bawabah Libraryimport express from 'express';import Bawabah from 'bawabah';// Initialize Bawabah with your credentialsconst bw = new Bawabah();bw.init({ appId: "your-app-id", appSecret: "your-app-secret"});// Create Express appconst app = express();const PORT = process.env.PORT || 3000;// Callback route for Bawabah redirectapp.get('/login/callback', async (req, res) => { const sessionId = req.query.sessionId as string; // Get Session Id from Query /login/callback?sessionId=abc123 if (!sessionId) { // If sessionId is missing, return a 400 error return res.status(400).send('Missing sessionId in query'); } try { // Capture user session from Bawabah const result = await bw.capture(sessionId); // --- Perform Your User Login/Signup Logic here --- res.status(200).json({ loggedIn: true }); } catch (err) { // If Bawabah API Respond with an Error, return a 500 error to the user with details res.status(500).json({ error: 'Failed to capture session', details: err }); }});// Start the Express serverapp.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`);});