Skip to content

Commit 53d2de5

Browse files
committed
Split authentication service into overridable factory hooks
1 parent 66e8f64 commit 53d2de5

4 files changed

Lines changed: 29 additions & 19 deletions

File tree

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
import { AuthenticationResponse } from '@openops/shared';
2-
import { authenticationService } from './basic/authentication-service';
3-
import { SignInParams, SignUpParams } from './types';
1+
import { User, UserWithOrganization } from '@openops/shared';
2+
import { getProjectAndToken } from './context/create-project-auth-context';
3+
import { addUserToDefaultWorkspace } from './new-user/organization-assignment';
4+
import { ProjectContext } from './types';
45

5-
export type AuthenticationService = {
6-
signUp(params: SignUpParams): Promise<AuthenticationResponse>;
7-
signIn(request: SignInParams): Promise<AuthenticationResponse>;
6+
export type ProjectAndTokenService = {
7+
fetch(user: User, tablesRefreshToken: string): Promise<ProjectContext>;
88
};
99

10-
export function getAuthenticationService(): AuthenticationService {
11-
return authenticationService;
10+
export type UserCreatedHook = { execute: () => Promise<void> | void };
11+
12+
export function getProjectAndTokenService(): ProjectAndTokenService {
13+
return { fetch: getProjectAndToken };
14+
}
15+
16+
export function getUserCreatedHook(
17+
userWithOrganization: UserWithOrganization,
18+
): UserCreatedHook {
19+
return {
20+
execute: async (): Promise<void> => {
21+
await addUserToDefaultWorkspace(userWithOrganization);
22+
},
23+
};
1224
}

packages/server/api/src/app/authentication/authentication.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { analyticsDashboardService } from '../openops-analytics/analytics-dashbo
1616
import { resolveOrganizationIdForAuthnRequest } from '../organization/organization-utils';
1717
import { userService } from '../user/user-service';
1818
import { analyticsAuthenticationService } from './analytics-authentication-service';
19-
import { getAuthenticationService } from './authentication-service-factory';
19+
import { authenticationService } from './basic/authentication-service';
2020
import {
2121
removeAuthCookies,
2222
setAuthCookies,
@@ -100,7 +100,7 @@ const signUpRoute = async (request: any, reply: any) => {
100100
});
101101
}
102102

103-
const signUpResponse = await getAuthenticationService().signUp({
103+
const signUpResponse = await authenticationService.signUp({
104104
...request.body,
105105
verified: edition === OpsEdition.COMMUNITY,
106106
organizationId: null,
@@ -117,7 +117,7 @@ const signInRoute = async (request: any, reply: any) => {
117117
request,
118118
);
119119

120-
const signInResponse = await getAuthenticationService().signIn({
120+
const signInResponse = await authenticationService.signIn({
121121
email: request.body.email,
122122
password: request.body.password,
123123
organizationId,

packages/server/api/src/app/authentication/basic/authentication-service.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { authenticateUserInOpenOpsTables } from '@openops/common';
22
import { AuthenticationResponse } from '@openops/shared';
33
import { userService } from '../../user/user-service';
4+
import { getUserCreatedHook } from '../authentication-service-factory';
45
import { getProjectAndToken } from '../context/create-project-auth-context';
56
import { createUser } from '../new-user/create-user';
6-
import {
7-
addUserToDefaultWorkspace,
8-
assignDefaultOrganization,
9-
} from '../new-user/organization-assignment';
7+
import { assignDefaultOrganization } from '../new-user/organization-assignment';
108
import { SignInParams, SignUpParams } from '../types';
119
import {
1210
assertPasswordMatches,
@@ -18,8 +16,8 @@ export const authenticationService = {
1816
async signUp(params: SignUpParams): Promise<AuthenticationResponse> {
1917
const { user, tablesRefreshToken } = await createUser(params);
2018

21-
const updatedUser = await assignDefaultOrganization(user);
22-
await addUserToDefaultWorkspace(updatedUser);
19+
const userWithOrganization = await assignDefaultOrganization(user);
20+
await getUserCreatedHook(userWithOrganization).execute();
2321

2422
const projectContext = await getProjectAndToken(user, tablesRefreshToken);
2523
return buildAuthResponse(projectContext);

packages/server/api/src/app/database/seeds/dev-seeds.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { logger, SharedSystemProp, system } from '@openops/server-shared';
22
import { EnvironmentType, Provider } from '@openops/shared';
3-
import { getAuthenticationService } from '../../authentication/authentication-service-factory';
3+
import { authenticationService } from '../../authentication/basic/authentication-service';
44
import { FlagEntity } from '../../flags/flag.entity';
55
import { databaseConnection } from '../database-connection';
66

@@ -30,7 +30,7 @@ const seedDevUser = async (): Promise<void> => {
3030
const DEV_EMAIL = 'dev@openops.com';
3131
const DEV_PASSWORD = '12345678';
3232

33-
await getAuthenticationService().signUp({
33+
await authenticationService.signUp({
3434
email: DEV_EMAIL,
3535
password: DEV_PASSWORD,
3636
firstName: 'Dev',

0 commit comments

Comments
 (0)