From d15161dba4dc676a449282cfbcb49ca5baf89ab8 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 13 Oct 2025 15:10:54 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20now=20cre?= =?UTF-8?q?ating=20user=20on=20the=20user=20api=20when=20registering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 3 +++ src/api/auth/gRegisterCallback.js | 3 +++ src/api/auth/register.js | 3 +++ src/utils/authUserCreate.js | 24 ++++++++++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 src/utils/authUserCreate.js diff --git a/.env.example b/.env.example index 8ecbc62..72ae740 100644 --- a/.env.example +++ b/.env.example @@ -24,3 +24,6 @@ SMTP_FROM= SMTP_AUTH_USERNAME= SMTP_AUTH_PASSWORD= EMAIL_TO= + +USER_URL= +AUTH_URL= diff --git a/src/api/auth/gRegisterCallback.js b/src/api/auth/gRegisterCallback.js index f79542f..40d8111 100644 --- a/src/api/auth/gRegisterCallback.js +++ b/src/api/auth/gRegisterCallback.js @@ -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,6 +47,8 @@ export async function gRegisterCallback(request, reply, fastify) { authDB.addUser(user.username, ''); + authUserCreate(user.username, fastify) + const token = fastify.jwt.sign(user); return reply diff --git a/src/api/auth/register.js b/src/api/auth/register.js index 7463452..5e10ff1 100644 --- a/src/api/auth/register.js +++ b/src/api/auth/register.js @@ -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 diff --git a/src/utils/authUserCreate.js b/src/utils/authUserCreate.js new file mode 100644 index 0000000..d453285 --- /dev/null +++ b/src/utils/authUserCreate.js @@ -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( + "http://localhost:3002/users/" + username, + payload, + { + headers: { + 'Cookie': 'token=' + cookie, + }, + } + ); +}