NodeJS
Feathers Cloud Auth requests can be verified in NodeJS with any framework.
HTTP
Below is a NodeJS server with a basic development CORS (necessary to allow requests from any client framework) configuration that uses the built in node:http
module
js
import { createServer } from 'node:http';
import { createVerifier } from '@featherscloud/auth';
const appId = '<your-app-id>';
const verifier = createVerifier({ appId });
const defaultHeaders = {
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Origin': '*'
};
const server = createServer(async (req, res) => {
if (req.method === 'OPTIONS') {
res.writeHead(200, defaultHeaders);
return res.end();
}
try {
const header = req.headers.authorization;
// Verify the Authorization header and get the user information
const { user } = await verifier.verifyHeader(header);
const response = {
message: `Hello ${user.email} from node:http!`
};
res.writeHead(200, defaultHeaders);
res.end(JSON.stringify(response));
} catch (error) {
res.writeHead(400, defaultHeaders);
res.end(JSON.stringify({ error: error.message }));
}
});
// Listen locally on port 3333
server.listen(3030, '127.0.0.1', () => {
console.log(`NodeJS application ${appId} listening on http://localhost:3030`);
});
The full NodeJS example server can be found here.
Express
The Express framework can handle request in a similar way.
sh
npm install express
npm install cors
js
import express from 'express';
import cors from 'cors';
import { createVerifier } from '@featherscloud/auth';
const appId = '<your-app-id>';
const verifier = createVerifier({ appId });
const app = express();
app.use(cors());
app.get('/', async (req, res) => {
try {
const header = req.get('Authorization');
// Verify the Authorization header and get the user information
const { user } = await verifier.verifyHeader(header);
const response = {
message: `Hello ${user.email} from Express!`
};
res.status(200).json(response);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.listen(3030, '127.0.0.1', () => {
console.log(
`Express application ${appId} listening on http://localhost:3030`
);
});
The full Express example server can be found here.