Skip to content

Commit

Permalink
updated Corbado frontend and backend API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlosp committed Jan 26, 2025
1 parent 5448c9e commit 09c9ec2
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions server/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,60 @@
import { Request, Response, NextFunction } from "express";
import { SDK, Config } from '@corbado/node-sdk';
import { SDK, Config } from "@corbado/node-sdk";

if (!process.env.CORBADO_PROJECT_ID || !process.env.CORBADO_API_SECRET) {
throw new Error('CORBADO_PROJECT_ID and CORBADO_API_SECRET environment variables are required');
throw new Error(
"CORBADO_PROJECT_ID and CORBADO_API_SECRET environment variables are required",
);
}

const config = new Config(
process.env.CORBADO_PROJECT_ID,
process.env.CORBADO_API_SECRET,
'https://api.corbado.com', // Frontend API
'https://api.corbado.com' // Backend API
"https://login.aegeanblue.shop", // Frontend API
"https://backendapi.cloud.corbado.io", // Backend API
);
const sdk = new SDK(config);

export async function authenticateUser(req: Request, res: Response, next: NextFunction) {
export async function authenticateUser(
req: Request,
res: Response,
next: NextFunction,
) {
try {
// Get the Authorization header
const authHeader = req.headers.authorization;
console.log('Auth header present:', !!authHeader);
console.log("Auth header present:", !!authHeader);

if (!authHeader?.startsWith('Bearer ')) {
console.log('No Bearer token provided');
return res.status(401).json({ error: 'No token provided' });
if (!authHeader?.startsWith("Bearer ")) {
console.log("No Bearer token provided");
return res.status(401).json({ error: "No token provided" });
}

// Extract the token
const token = authHeader.split(' ')[1];
console.log('Token extracted from header');
const token = authHeader.split(" ")[1];
console.log("Token extracted from header");

try {
// Validate the token using Corbado SDK
const validation = await sdk.sessions().validateToken(token);
console.log('Token validation result:', validation);
console.log("Token validation result:", validation);

if (!validation.userId) {
console.log('Token validation failed: no userId in response');
throw new Error('Invalid token');
console.log("Token validation failed: no userId in response");
throw new Error("Invalid token");
}

// Add the validated user ID to the request
req.userId = validation.userId;
console.log('User authenticated:', req.userId);
console.log("User authenticated:", req.userId);
next();
} catch (error) {
console.error('Token validation failed:', error);
return res.status(401).json({ error: 'Invalid token' });
console.error("Token validation failed:", error);
return res.status(401).json({ error: "Invalid token" });
}
} catch (error) {
console.error('Auth error:', error);
res.status(401).json({ error: 'Authentication failed' });
console.error("Auth error:", error);
res.status(401).json({ error: "Authentication failed" });
}
}

Expand All @@ -59,4 +65,4 @@ declare global {
userId?: string;
}
}
}
}

0 comments on commit 09c9ec2

Please sign in to comment.