」 feat: added 2fa checker

This commit is contained in:
2025-10-23 17:56:28 +02:00
parent 9ea154492c
commit d0862a5098
2 changed files with 30 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import fastifyJWT from '@fastify/jwt';
import fastifyCookie from '@fastify/cookie'; import fastifyCookie from '@fastify/cookie';
import cors from '@fastify/cors'; import cors from '@fastify/cors';
import { totpCheck } from './totpCheck.js';
import { register } from './register.js'; import { register } from './register.js';
import { login } from './login.js'; import { login } from './login.js';
import { gRedir } from './gRedir.js'; import { gRedir } from './gRedir.js';
@ -28,7 +29,7 @@ export default async function(fastify, options) {
fastify.register(cors, { fastify.register(cors, {
origin: process.env.CORS_ORIGIN || 'http://localhost:5173', origin: process.env.CORS_ORIGIN || 'http://localhost:5173',
credentials: true, credentials: true,
methods: [ "GET", "POST", "PATCH", "DELETE", "OPTIONS" ] methods: ["GET", "POST", "PATCH", "DELETE", "OPTIONS"]
}); });
fastify.register(fastifyJWT, { fastify.register(fastifyJWT, {
@ -54,6 +55,9 @@ export default async function(fastify, options) {
fastify.get('/me', { preHandler: [fastify.authenticate] }, async (request, reply) => { fastify.get('/me', { preHandler: [fastify.authenticate] }, async (request, reply) => {
return { user: request.user }; return { user: request.user };
}); });
fastify.get('/2fa', { preHandler: [fastify.authenticate] }, async (request, reply) => {
return totpCheck(request, reply);
});
// GOOGLE sign in // GOOGLE sign in
fastify.get('/login/google', async (request, reply) => { fastify.get('/login/google', async (request, reply) => {
@ -119,5 +123,5 @@ export default async function(fastify, options) {
fastify.get('/logout', {}, async (request, reply) => { return logout(reply, fastify); }) fastify.get('/logout', {}, async (request, reply) => { return logout(reply, fastify); })
fastify.delete('/', { preHandler: fastify.authenticate }, async (request, reply) => { return remove(request, reply, fastify)}) fastify.delete('/', { preHandler: fastify.authenticate }, async (request, reply) => { return remove(request, reply, fastify) })
} }

24
src/api/auth/totpCheck.js Normal file
View File

@ -0,0 +1,24 @@
import authDB from '../../utils/authDB.js';
/**
* @param {import('fastify').FastifyRequest} request
* @param {import('fastify').FastifyReply} reply
*/
export async function totpCheck(request, reply) {
try {
const user = request.user;
if (authDB.checkUser(user) === false) {
return reply.code(400).send({ error: "User does not exist" });
}
return reply
.code(200)
.send({
totp: authDB.isTOTPEnabled(user)
});
} catch (err) {
fastify.log.error(err);
return reply.code(500).send({ error: "Internal server error" });
}
}