Feat: Org scoped JWT's CLI support

This commit is contained in:
Daniel Hougaard
2024-03-17 18:49:30 +01:00
parent 9bbba92768
commit 22b2fb4c98
2 changed files with 19 additions and 24 deletions
+10 -22
View File
@@ -1,9 +1,7 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import axios from "axios";
import { fetchUserDetails } from "@app/hooks/api/users/queries"; import { isLoggedIn } from "@app/reactQuery";
import { getAuthToken, isLoggedIn } from "@app/reactQuery";
import { InitialStep, LDAPStep, MFAStep, SAMLSSOStep } from "./components"; import { InitialStep, LDAPStep, MFAStep, SAMLSSOStep } from "./components";
import { navigateUserToSelectOrg } from "./Login.utils"; import { navigateUserToSelectOrg } from "./Login.utils";
@@ -18,33 +16,23 @@ export const Login = () => {
useEffect(() => { useEffect(() => {
// TODO(akhilmhdh): workspace will be controlled by a workspace context // TODO(akhilmhdh): workspace will be controlled by a workspace context
const redirectToDashboard = async () => { const handleRedirects = async () => {
// TODO(daniel): Move this to select-organization page. // TODO(daniel): Move this to select-organization page.
try { try {
// user details const callbackPort = queryParams?.get("callback_port");
const userDetails = await fetchUserDetails(); // case: a callback port is set, meaning it's a cli login request: redirect to select org with callback port
// send details back to client if (callbackPort) {
navigateUserToSelectOrg(router, callbackPort);
if (queryParams && queryParams.get("callback_port")) { } else {
const callbackPort = queryParams.get("callback_port"); // case: no callback port, meaning it's a regular login request: redirect to select org
navigateUserToSelectOrg(router);
// send post request to cli with details
const cliUrl = `http://127.0.0.1:${callbackPort}/`;
const instance = axios.create();
await instance.post(cliUrl, {
email: userDetails.email,
privateKey: localStorage.getItem("PRIVATE_KEY"),
JTWToken: getAuthToken()
});
} }
navigateUserToSelectOrg(router);
} catch (error) { } catch (error) {
console.log("Error - Not logged in yet"); console.log("Error - Not logged in yet");
} }
}; };
if (isLoggedIn()) { if (isLoggedIn()) {
redirectToDashboard(); handleRedirects();
} }
}, []); }, []);
+9 -2
View File
@@ -27,7 +27,14 @@ export const navigateUserToOrg = async (router: NextRouter, organizationId?: str
} }
}; };
export const navigateUserToSelectOrg = (router: NextRouter) => { export const navigateUserToSelectOrg = (router: NextRouter, cliCallbackPort?: string) => {
queryClient.invalidateQueries(userKeys.getUser); queryClient.invalidateQueries(userKeys.getUser);
router.push("/login/select-organization", undefined, { shallow: true });
let redirectTo = "/login/select-organization";
if (cliCallbackPort) {
redirectTo += `?callback_port=${cliCallbackPort}`;
}
router.push(redirectTo, undefined, { shallow: true });
}; };