mirror of
https://github.com/KeyZox71/knl_meowscendence.git
synced 2025-08-14 04:22:54 +02:00
「🎉」 init: started user data api
This commit is contained in:
6
Justfile
6
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
|
||||
|
32
dev.js
Normal file
32
dev.js
Normal file
@ -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);
|
@ -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" });
|
||||
});
|
||||
}
|
||||
|
43
src/api/user/default.js
Normal file
43
src/api/user/default.js
Normal file
@ -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 });
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user