Files
freeCodeCamp/api/index.ts
Mrugesh Mohapatra 7199f033fb feat(next-api): basic authentication setup (#49378)
* feat(next-api): add fastify-auth0-verify plugin

* feat(next-api): add fastify-jwt-authz plugin

* feat(next-api): accept privacy endpoint with scopes support

* fix(next-api): ignore eslint and ts errors

They will be fixed in a future PR when the package with errors has been updated

Co-authored-by: Niraj Nandish <nirajnandish@icloud.com>
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2023-02-24 17:26:40 +05:30

50 lines
1.3 KiB
TypeScript

import { config } from 'dotenv';
config({ path: '../.env' });
import fastifyAuth0 from 'fastify-auth0-verify';
import Fastify from 'fastify';
import middie from '@fastify/middie';
import jwtAuthz from './plugins/fastify-jwt-authz';
import { testRoutes } from './routes/test';
import { dbConnector } from './db';
import { auth0Verify, testMiddleware } from './middleware';
const fastify = Fastify({
logger: { level: process.env.NODE_ENV === 'development' ? 'debug' : 'fatal' }
});
fastify.get('/', async (_request, _reply) => {
return { hello: 'world' };
});
const start = async () => {
// NOTE: Awaited to ensure `.use` is registered on `fastify`
await fastify.register(middie);
// Auth0 plugin
void fastify.register(fastifyAuth0, {
domain: process.env.AUTH0_DOMAIN,
audience: process.env.AUTH0_AUDIENCE
});
void fastify.register(jwtAuthz);
void fastify.use('/test', testMiddleware);
// Hooks
void fastify.addHook('preValidation', auth0Verify);
void fastify.register(dbConnector);
void fastify.register(testRoutes);
try {
const port = Number(process.env.PORT) || 3000;
fastify.log.info(`Starting server on port ${port}`);
await fastify.listen({ port });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
void start();