Merge pull request #53 from KeyZox71/docker-front

」 feat: added env variable to front
This commit is contained in:
Adam
2025-10-20 14:16:40 +02:00
committed by GitHub
12 changed files with 98 additions and 12 deletions

View File

@ -11,6 +11,7 @@ import { gRegisterCallback } from './gRegisterCallback.js';
import { totpSetup } from './totpSetup.js';
import { totpDelete } from './totpDelete.js';
import { totpVerify } from './totpVerify.js';
import { logout } from './logout.js';
const saltRounds = 10;
export const appName = process.env.APP_NAME || 'knl_meowscendence';
@ -114,4 +115,6 @@ export default async function(fastify, options) {
}
}
}, async (request, reply) => { return register(request, reply, saltRounds, fastify); });
fastify.get('/logout', {}, async (request, reply) => { return logout(reply, fastify); })
}

View File

@ -37,7 +37,7 @@ export async function gLogCallback(request, reply, fastify) {
return reply.code(400).send({ error: "User does not exist" });
}
const token = fastify.jwt.sign(user);
const token = fastify.jwt.sign({ user: user.username});
return reply
.setCookie('token', token, {
@ -45,9 +45,7 @@ export async function gLogCallback(request, reply, fastify) {
path: '/',
secure: env !== 'development',
sameSite: 'lax',
})
.code(200)
.send({ msg: "Login successful" });
}).redirect(process.env.CALLBACK_REDIR);
} catch (error) {
fastify.log.error(error);
reply.code(500).send({ error: 'Internal server error' });

View File

@ -1,6 +1,7 @@
import axios from 'axios'
import authDB from '../../utils/authDB.js';
import { authUserCreate } from '../../utils/authUserCreate.js';
var env = process.env.NODE_ENV || 'development';
@ -46,7 +47,9 @@ export async function gRegisterCallback(request, reply, fastify) {
authDB.addUser(user.username, '');
const token = fastify.jwt.sign(user);
authUserCreate(user.username, fastify)
const token = fastify.jwt.sign({ user: user.username});
return reply
.setCookie('token', token, {
@ -54,9 +57,7 @@ export async function gRegisterCallback(request, reply, fastify) {
path: '/',
secure: env !== 'development',
sameSite: 'lax',
})
.code(200)
.send({ msg: "Register successful" });
}).redirect(process.env.CALLBACK_REDIR);
} catch (error) {
fastify.log.error(error);
reply.code(500).send({ error: 'Internal server error' });

18
src/api/auth/logout.js Normal file
View File

@ -0,0 +1,18 @@
/**
* @async
* @param {import("fastify").FastifyReply} reply
* @param {import("fastify").FastifyInstance} fastify
*
* @returns {import("fastify").FastifyReply}
*/
export async function logout(reply, fastify) {
try {
return reply
.code(200)
.clearCookie("token")
.send({ msg: "Logout successful" });
} catch {
fastify.log.error(err);
return reply.code(500).send({ error: "Internal server error" });
}
}

View File

@ -2,6 +2,7 @@ import bcrypt from 'bcrypt';
import { isValidString } from '../../utils/authUtils.js';
import authDB from '../../utils/authDB.js';
import { authUserCreate } from '../../utils/authUserCreate.js';
var env = process.env.NODE_ENV || 'development';
@ -36,6 +37,8 @@ export async function register(request, reply, saltRounds, fastify) {
const hash = await bcrypt.hash(password, saltRounds);
authDB.addUser(user, hash);
authUserCreate(user, fastify)
const token = fastify.jwt.sign({ user });
return reply

View File

@ -0,0 +1,24 @@
import axios from 'axios';
/**
* @param {string} username
* @param {import('fastify').FastifyInstance} fastify
*/
export async function authUserCreate(username, fastify) {
const payload = {
displayName: username,
};
const cookie = fastify.jwt.sign({ user: "admin" });
const url = process.env.USER_URL || "http://localhost:3002"
await axios.post(
url + "/users/" + username,
payload,
{
headers: {
'Cookie': 'token=' + cookie,
},
}
);
}