Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Fix: Email signup and switching organization

Daniel Hougaard4d229ec7457f
diff --git a/backend/src/server/routes/v3/signup-router.ts b/backend/src/server/routes/v3/signup-router.ts
index 17787be843..ac43df36de 100644
--- a/backend/src/server/routes/v3/signup-router.ts
+++ b/backend/src/server/routes/v3/signup-router.ts
@@ -108,7 +108,8 @@ export const registerSignupRouter = async (server: FastifyZodProvider) => {
200: z.object({
message: z.string(),
user: UsersSchema,
- token: z.string()
+ token: z.string(),
+ organizationId: z.string().nullish()
})
}
},
@@ -124,12 +125,13 @@ export const registerSignupRouter = async (server: FastifyZodProvider) => {
});
}
- const { user, accessToken, refreshToken } = await server.services.signup.completeEmailAccountSignup({
- ...req.body,
- ip: req.realIp,
- userAgent,
- authorization: req.headers.authorization as string
- });
+ const { user, accessToken, refreshToken, organizationId } =
+ await server.services.signup.completeEmailAccountSignup({
+ ...req.body,
+ ip: req.realIp,
+ userAgent,
+ authorization: req.headers.authorization as string
+ });
if (user.email) {
void server.services.telemetry.sendLoopsEvent(user.email, user.firstName || "", user.lastName || "");
@@ -152,7 +154,7 @@ export const registerSignupRouter = async (server: FastifyZodProvider) => {
secure: appCfg.HTTPS_ENABLED
});
- return { message: "Successfully set up account", user, token: accessToken };
+ return { message: "Successfully set up account", user, token: accessToken, organizationId };
}
});
diff --git a/backend/src/services/auth/auth-signup-service.ts b/backend/src/services/auth/auth-signup-service.ts
index 4c14cfb816..3db935769f 100644
--- a/backend/src/services/auth/auth-signup-service.ts
+++ b/backend/src/services/auth/auth-signup-service.ts
@@ -150,11 +150,15 @@ export const authSignupServiceFactory = ({
});
if (!organizationId) {
- await orgService.createOrganization({
+ const newOrganization = await orgService.createOrganization({
userId: user.id,
userEmail: user.email ?? user.username,
orgName: organizationName
});
+
+ if (!newOrganization) throw new Error("Failed to create organization");
+
+ organizationId = newOrganization.id;
}
const updatedMembersips = await orgDAL.updateMembership(
@@ -187,6 +191,7 @@ export const authSignupServiceFactory = ({
const refreshToken = jwt.sign(
{
+ authMethod: AuthMethod.EMAIL,
authTokenType: AuthTokenType.REFRESH_TOKEN,
userId: updateduser.info.id,
tokenVersionId: tokenSession.id,
@@ -197,7 +202,7 @@ export const authSignupServiceFactory = ({
{ expiresIn: appCfg.JWT_REFRESH_LIFETIME }
);
- return { user: updateduser.info, accessToken, refreshToken };
+ return { user: updateduser.info, accessToken, refreshToken, organizationId };
};
/*
@@ -290,6 +295,7 @@ export const authSignupServiceFactory = ({
const refreshToken = jwt.sign(
{
+ authMethod: AuthMethod.EMAIL,
authTokenType: AuthTokenType.REFRESH_TOKEN,
userId: updateduser.info.id,
tokenVersionId: tokenSession.id,
diff --git a/frontend/src/components/signup/UserInfoStep.tsx b/frontend/src/components/signup/UserInfoStep.tsx
index 43c4817632..a7d11d10c4 100644
--- a/frontend/src/components/signup/UserInfoStep.tsx
+++ b/frontend/src/components/signup/UserInfoStep.tsx
@@ -8,7 +8,7 @@ import jsrp from "jsrp";
import nacl from "tweetnacl";
import { encodeBase64 } from "tweetnacl-util";
-import { completeAccountSignup } from "@app/hooks/api/auth/queries";
+import { completeAccountSignup, useSelectOrganization } from "@app/hooks/api/auth/queries";
import { fetchOrganizations } from "@app/hooks/api/organization/queries";
import ProjectService from "@app/services/ProjectService";
@@ -79,6 +79,7 @@ export default function UserInfoStep({
const [errors, setErrors] = useState<Errors>({});
+ const { mutateAsync: selectOrganization } = useSelectOrganization();
const [isLoading, setIsLoading] = useState(false);
const { t } = useTranslation();
@@ -181,6 +182,10 @@ export default function UserInfoStep({
SecurityClient.setToken(response.token);
SecurityClient.setProviderAuthToken("");
+ if (response.organizationId) {
+ await selectOrganization({ organizationId: response.organizationId });
+ }
+
saveTokenToLocalStorage({
publicKey,
encryptedPrivateKey,
diff --git a/frontend/src/layouts/AppLayout/AppLayout.tsx b/frontend/src/layouts/AppLayout/AppLayout.tsx
index 2785d82a1e..e78913f6ec 100644
--- a/frontend/src/layouts/AppLayout/AppLayout.tsx
+++ b/frontend/src/layouts/AppLayout/AppLayout.tsx
@@ -68,8 +68,10 @@ import {
useGetSecretApprovalRequestCount,
useGetUserAction,
useLogoutUser,
- useRegisterUserAction
+ useRegisterUserAction,
+ useSelectOrganization
} from "@app/hooks/api";
+import { navigateUserToOrg } from "@app/views/Login/Login.utils";
import { CreateOrgModal } from "@app/views/Org/components";
interface LayoutProps {
@@ -100,7 +102,12 @@ const supportOptions = [
];
const formSchema = yup.object({
- name: yup.string().required().label("Project Name").trim().max(64, "Too long, maximum length is 64 characters"),
+ name: yup
+ .string()
+ .required()
+ .label("Project Name")
+ .trim()
+ .max(64, "Too long, maximum length is 64 characters"),
addMembers: yup.bool().required().label("Add Members")
});
@@ -147,6 +154,7 @@ export const AppLayout = ({ children }: LayoutProps) => {
const { t } = useTranslation();
const registerUserAction = useRegisterUserAction();
+ const { mutateAsync: selectOrganization } = useSelectOrganization();
const closeUpdate = async () => {
await registerUserAction.mutateAsync("december_update_closed");
@@ -164,8 +172,11 @@ export const AppLayout = ({ children }: LayoutProps) => {
};
const changeOrg = async (orgId: string) => {
- localStorage.setItem("orgData.id", orgId);
- router.push(`/org/${orgId}/overview`);
+ await selectOrganization({
+ organizationId: orgId
+ });
+
+ await navigateUserToOrg(router, orgId);
};
// TODO(akhilmhdh): This entire logic will be rechecked and will try to avoid
@@ -425,7 +436,7 @@ export const AppLayout = ({ children }: LayoutProps) => {
<Select
defaultValue={currentWorkspace?.id}
value={currentWorkspace?.id}
- className="w-full [&>*:first-child]:truncate bg-mineshaft-600 py-2.5 font-medium"
+ className="w-full bg-mineshaft-600 py-2.5 font-medium [&>*:first-child]:truncate"
onValueChange={(value) => {
router.push(`/project/${value}/secrets/overview`);
localStorage.setItem("projectData.id", value);