diff --git a/lib/database/migrations/1_create_servers_table.sql b/lib/database/migrations/1_create_servers_table.sql index fe3f357..6386fc5 100644 --- a/lib/database/migrations/1_create_servers_table.sql +++ b/lib/database/migrations/1_create_servers_table.sql @@ -1,7 +1,7 @@ CREATE SEQUENCE servers_id_seq; CREATE TABLE servers ( id bigint NOT NULL DEFAULT nextval('servers_id_seq') PRIMARY KEY, - owner_cairo_id bigint, + owner_cairo_id varchar(63), host varchar(255) DEFAULT NULL, name varchar(255) DEFAULT NULL, version varchar(63) DEFAULT 'latest', diff --git a/lib/routes/auth-route.js b/lib/routes/auth-route.js index 8409975..638e163 100644 --- a/lib/routes/auth-route.js +++ b/lib/routes/auth-route.js @@ -2,11 +2,14 @@ import { Router } from "express"; import cairoAuthMiddleware from "./middlewares/auth-middleware.js"; const router = Router(); +const cairoProjectId = process.env.MCL_CAIRO_PROJECT; +if(!cairoProjectId) throw Error("Cairo Project Required!"); + const ok = (_r, res) => res.sendStatus(200); function cairoRedirect(req, res) { res.redirect( - `${process.env.MCL_CAIRO_URL}/cairo/auth?redirectUri=${req.query.redirectUri}`, + `${process.env.MCL_CAIRO_URL}/cairo/authenticate?redirectUri=${req.query.redirectUri}&projectId=${cairoProjectId}`, ); } diff --git a/lib/routes/middlewares/auth-middleware.js b/lib/routes/middlewares/auth-middleware.js index 37fe39c..5758cb4 100644 --- a/lib/routes/middlewares/auth-middleware.js +++ b/lib/routes/middlewares/auth-middleware.js @@ -4,12 +4,12 @@ import bearerTokenMiddleware from "express-bearer-token"; import { ERR, VERB } from "../../util/logging.js"; // Constants -const { MCL_CAIRO_URL } = process.env; +const { MCL_CAIRO_URL, MCL_CAIRO_PROJECT } = process.env; const cairoAuthMiddleware = Router(); const cairoAuthenticate = async (token) => { const config = { headers: { Authorization: `Bearer ${token}` } }; - return fetch(`${MCL_CAIRO_URL}/api/user/info`, config).then(async (res) => { + return fetch(`${MCL_CAIRO_URL}/api/${MCL_CAIRO_PROJECT}/auth/credentials`, config).then(async (res) => { if (res.status >= 300) { const errorMessage = await res .json() @@ -30,9 +30,9 @@ const cairoAuthHandler = (req, res, next) => { cairoAuthenticate(req.token) .then((authData) => { console.log(authData); - if (!authData.id) - throw Error(`Cairo didn't return the expected data! ${authData.id}`); - req.cairoId = authData.id; + if (!authData?.user?.id) + throw Error(`Cairo didn't return the expected data! ${authData?.user?.id}`); + req.cairoId = authData?.user?.id; }) .then(() => next()) .catch((err) => { diff --git a/src/util/auth.js b/src/util/auth.js index f25a6bb..69b9d8c 100644 --- a/src/util/auth.js +++ b/src/util/auth.js @@ -1,8 +1,8 @@ import { useState, useEffect } from "react"; import { useSearchParams } from "react-router-dom"; -const tokenStorageName = "cairoAuthToken"; -const tokenQuery = "cairoAuthToken"; +const tokenStorageName = "cairoUserToken"; +const tokenQuery = "cairoUserToken"; const verifyAuth = (authToken) => fetch("/api/auth/verify", {