fix: stale session after logging into CLI

This commit is contained in:
Daniel Hougaard
2024-09-09 23:15:58 +04:00
parent 2b39d9e6c4
commit e6c0bbb25b
5 changed files with 19 additions and 14 deletions
+1 -1
View File
@@ -468,7 +468,7 @@ export const registerRoutes = async (
projectMembershipDAL projectMembershipDAL
}); });
const loginService = authLoginServiceFactory({ userDAL, smtpService, tokenService, orgDAL, tokenDAL: authTokenDAL }); const loginService = authLoginServiceFactory({ userDAL, smtpService, tokenService, orgDAL });
const passwordService = authPaswordServiceFactory({ const passwordService = authPaswordServiceFactory({
tokenService, tokenService,
smtpService, smtpService,
+3 -2
View File
@@ -42,7 +42,8 @@ export const registerLoginRouter = async (server: FastifyZodProvider) => {
}, },
schema: { schema: {
body: z.object({ body: z.object({
organizationId: z.string().trim() organizationId: z.string().trim(),
customUserAgent: z.enum(["cli"]).optional()
}), }),
response: { response: {
200: z.object({ 200: z.object({
@@ -53,7 +54,7 @@ export const registerLoginRouter = async (server: FastifyZodProvider) => {
handler: async (req, res) => { handler: async (req, res) => {
const cfg = getConfig(); const cfg = getConfig();
const tokens = await server.services.login.selectOrganization({ const tokens = await server.services.login.selectOrganization({
userAgent: req.headers["user-agent"], userAgent: req.body.customUserAgent ?? req.headers["user-agent"],
authJwtToken: req.headers.authorization, authJwtToken: req.headers.authorization,
organizationId: req.body.organizationId, organizationId: req.body.organizationId,
ipAddress: req.realIp ipAddress: req.realIp
@@ -12,7 +12,6 @@ import { BadRequestError, DatabaseError, UnauthorizedError } from "@app/lib/erro
import { logger } from "@app/lib/logger"; import { logger } from "@app/lib/logger";
import { getServerCfg } from "@app/services/super-admin/super-admin-service"; import { getServerCfg } from "@app/services/super-admin/super-admin-service";
import { TTokenDALFactory } from "../auth-token/auth-token-dal";
import { TAuthTokenServiceFactory } from "../auth-token/auth-token-service"; import { TAuthTokenServiceFactory } from "../auth-token/auth-token-service";
import { TokenType } from "../auth-token/auth-token-types"; import { TokenType } from "../auth-token/auth-token-types";
import { TOrgDALFactory } from "../org/org-dal"; import { TOrgDALFactory } from "../org/org-dal";
@@ -34,7 +33,6 @@ type TAuthLoginServiceFactoryDep = {
orgDAL: TOrgDALFactory; orgDAL: TOrgDALFactory;
tokenService: TAuthTokenServiceFactory; tokenService: TAuthTokenServiceFactory;
smtpService: TSmtpService; smtpService: TSmtpService;
tokenDAL: TTokenDALFactory;
}; };
export type TAuthLoginFactory = ReturnType<typeof authLoginServiceFactory>; export type TAuthLoginFactory = ReturnType<typeof authLoginServiceFactory>;
@@ -42,8 +40,7 @@ export const authLoginServiceFactory = ({
userDAL, userDAL,
tokenService, tokenService,
smtpService, smtpService,
orgDAL, orgDAL
tokenDAL
}: TAuthLoginServiceFactoryDep) => { }: TAuthLoginServiceFactoryDep) => {
/* /*
* Private * Private
@@ -376,8 +373,6 @@ export const authLoginServiceFactory = ({
}); });
} }
await tokenDAL.incrementTokenSessionVersion(user.id, decodedToken.tokenVersionId);
const tokens = await generateUserTokens({ const tokens = await generateUserTokens({
authMethod: decodedToken.authMethod, authMethod: decodedToken.authMethod,
user, user,
+8 -2
View File
@@ -60,7 +60,10 @@ export const useLogin1 = () => {
}); });
}; };
export const selectOrganization = async (data: { organizationId: string }) => { export const selectOrganization = async (data: {
organizationId: string;
customUserAgent?: string;
}) => {
const { data: res } = await apiRequest.post<{ token: string }>( const { data: res } = await apiRequest.post<{ token: string }>(
"/api/v3/auth/select-organization", "/api/v3/auth/select-organization",
data data
@@ -71,11 +74,14 @@ export const selectOrganization = async (data: { organizationId: string }) => {
export const useSelectOrganization = () => { export const useSelectOrganization = () => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async (details: { organizationId: string }) => { mutationFn: async (details: { organizationId: string; customUserAgent?: string }) => {
const data = await selectOrganization(details); const data = await selectOrganization(details);
// If a custom user agent is set, then this session is meant for another consuming application, not the web application.
if (!details.customUserAgent) {
SecurityClient.setToken(data.token); SecurityClient.setToken(data.token);
SecurityClient.setProviderAuthToken(""); SecurityClient.setProviderAuthToken("");
}
return data; return data;
}, },
@@ -68,7 +68,10 @@ export default function LoginPage() {
return; return;
} }
const { token } = await selectOrg.mutateAsync({ organizationId: organization.id }); const { token } = await selectOrg.mutateAsync({
organizationId: organization.id,
customUserAgent: callbackPort ? "cli" : undefined
});
if (callbackPort) { if (callbackPort) {
const privateKey = localStorage.getItem("PRIVATE_KEY"); const privateKey = localStorage.getItem("PRIVATE_KEY");