diff --git a/Justfile b/Justfile index 4966626..b6b56a0 100644 --- a/Justfile +++ b/Justfile @@ -4,3 +4,9 @@ export FASTIFY_LOG_LEVEL := "info" auth: fastify start src/api/auth/default.js + +user: + fastify start src/api/user/default.js + +apis: + node dev.js diff --git a/dev.js b/dev.js new file mode 100644 index 0000000..7e2ef10 --- /dev/null +++ b/dev.js @@ -0,0 +1,32 @@ +import Fastify from 'fastify'; +import authApi from './src/api/auth/default.js'; +import userApi from './src/api/user/default.js'; + +const loggerOption = { + transport: { + target: 'pino-pretty', + options: { + colorize: true, + translateTime: 'HH:MM:ss', + ignore: 'pid,hostname' + } + } +}; + +async function start() { + const auth = Fastify({ + logger: loggerOption + }); + auth.register(authApi); + await auth.listen({ port: 3001 }); + console.log('Auth API listening on http://localhost:3001'); + + const user = Fastify({ + logger: loggerOption + }); + user.register(userApi); + await user.listen({ port: 3002 }); + console.log('User data API listening on http://localhost:3002'); +} + +start().catch(console.error); diff --git a/src/api/auth/default.js b/src/api/auth/default.js index eb29262..17eeb0e 100644 --- a/src/api/auth/default.js +++ b/src/api/auth/default.js @@ -47,7 +47,7 @@ function isValidString(value) { */ export default async function(fastify, options) { fastify.register(fastifyJWT, { - secret: '12345', + secret: '123456789101112131415161718192021', cookie: { cookieName: 'token', }, @@ -57,20 +57,10 @@ export default async function(fastify, options) { }); fastify.register(fastifyCookie); - fastify.decorate("authenticate", async function(request, reply) { - try { - fastify.log.info(request.headers.cookie); - await request.jwtVerify(); - } catch (err) { - reply.code(401).send({ error: 'Unauthorized' }); - } - }); - fastify.post('/login', async (request, reply) => { try { /** @type {{ user: string, password: string }} */ const { user, password } = request.body; - request.headers.cookie if (!checkUser(user)) { return reply.code(400).send({ error: "User does not exist" }); @@ -80,7 +70,7 @@ export default async function(fastify, options) { const hash = query?.passwordHash; if (!hash) { - return reply.code(500).send({ error: "Password hash not found" }); + return reply.code(500).send({ error: "No password was found" }); } const compare = await bcrypt.compare(password, hash); @@ -129,8 +119,4 @@ export default async function(fastify, options) { return reply.code(500).send({ error: "Internal server error" }); } }); - - fastify.get('/check', { preHandler: [fastify.authenticate] }, async (request, reply) => { - return reply.code(200).send({ msg: "workinggg" }); - }); } diff --git a/src/api/user/default.js b/src/api/user/default.js new file mode 100644 index 0000000..25a3e1a --- /dev/null +++ b/src/api/user/default.js @@ -0,0 +1,43 @@ +import fastifyJWT from '@fastify/jwt'; +import fastifyCookie from '@fastify/cookie'; +import Database from 'better-sqlite3'; + +const database = new Database(":memory:"); + +function prepareDB() { + database.exec(` + CREATE TABLE credentials ( + username TEXT PRIMARY KEY, + passwordHash TEXT + ) STRICT + `); +} + +/** + * @param {import('fastify').FastifyInstance} fastify + * @param {import('fastify').FastifyPluginOptions} options + */ +export default async function(fastify, options) { + fastify.register(fastifyJWT, { + secret: '123456789101112131415161718192021', + cookie: { + cookieName: 'token', + }, + }); + + fastify.decorate("authenticate", async function(request, reply) { + try { + // fastify.log.info(request.headers.cookie); + const jwt = await request.jwtVerify(); + request.user = jwt.user; + } catch (err) { + reply.code(401).send({ error: 'Unauthorized' }); + } + }); + + fastify.register(fastifyCookie); + + fastify.get('/check', { preHandler: [fastify.authenticate] }, async (request, reply) => { + return reply.code(200).send({ msg: "workinggg", user: request.user }); + }); +}