Skip to content

Commit cf545e7

Browse files
committed
fix: AsyncLocalStorage no longer needed since MikroORM v5
1 parent 189e673 commit cf545e7

File tree

6 files changed

+118
-156
lines changed

6 files changed

+118
-156
lines changed
Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,64 @@
11
import { AccountsServer } from '@accounts/server';
2-
import { ctxAsyncLocalStorage } from '@accounts/types';
32
import { MutationResolvers } from '../models';
43

54
export const Mutation: MutationResolvers = {
65
authenticate: async (_, args, ctx) => {
76
const { serviceName, params } = args;
87
const { injector, infos } = ctx;
98

10-
return ctxAsyncLocalStorage.run(ctx, async () => {
11-
const authenticated = await injector
12-
.get(AccountsServer)
13-
.loginWithService(serviceName, params, infos);
14-
return authenticated;
15-
});
9+
const authenticated = await injector
10+
.get(AccountsServer)
11+
.loginWithService(serviceName, params, infos);
12+
return authenticated;
1613
},
1714
verifyAuthentication: async (_, args, ctx) => {
1815
const { serviceName, params } = args;
1916
const { injector, infos } = ctx;
2017

21-
return ctxAsyncLocalStorage.run(ctx, async () => {
22-
const authenticated = await injector
23-
.get(AccountsServer)
24-
.authenticateWithService(serviceName, params, infos);
25-
return authenticated;
26-
});
18+
const authenticated = await injector
19+
.get(AccountsServer)
20+
.authenticateWithService(serviceName, params, infos);
21+
return authenticated;
2722
},
2823
impersonate: async (_, args, ctx) => {
2924
const { accessToken, impersonated } = args;
3025
const { injector, infos } = ctx;
3126

32-
return ctxAsyncLocalStorage.run(ctx, async () => {
33-
const impersonateRes = await injector.get(AccountsServer).impersonate(
34-
accessToken,
35-
{
36-
userId: impersonated.userId!,
37-
username: impersonated.username!,
38-
email: impersonated.email!,
39-
},
40-
infos
41-
);
27+
const impersonateRes = await injector.get(AccountsServer).impersonate(
28+
accessToken,
29+
{
30+
userId: impersonated.userId!,
31+
username: impersonated.username!,
32+
email: impersonated.email!,
33+
},
34+
infos
35+
);
4236

43-
// So ctx.user can be used in subsequent queries / mutations
44-
if (impersonateRes && impersonateRes.user && impersonateRes.tokens) {
45-
ctx.user = impersonateRes.user;
46-
ctx.authToken = impersonateRes.tokens.accessToken;
47-
}
37+
// So ctx.user can be used in subsequent queries / mutations
38+
if (impersonateRes && impersonateRes.user && impersonateRes.tokens) {
39+
ctx.user = impersonateRes.user;
40+
ctx.authToken = impersonateRes.tokens.accessToken;
41+
}
4842

49-
return impersonateRes;
50-
});
43+
return impersonateRes;
5144
},
5245
logout: async (_, __, context) => {
5346
const { authToken, injector } = context;
5447

55-
return ctxAsyncLocalStorage.run(context, async () => {
56-
if (authToken) {
57-
await injector.get(AccountsServer).logout(authToken);
58-
}
48+
if (authToken) {
49+
await injector.get(AccountsServer).logout(authToken);
50+
}
5951

60-
return null;
61-
});
52+
return null;
6253
},
6354
refreshTokens: async (_, args, ctx) => {
6455
const { accessToken, refreshToken } = args;
6556
const { injector, infos } = ctx;
6657

67-
return ctxAsyncLocalStorage.run(ctx, async () => {
68-
const refreshedSession = await injector
69-
.get(AccountsServer)
70-
.refreshTokens(accessToken, refreshToken, infos);
58+
const refreshedSession = await injector
59+
.get(AccountsServer)
60+
.refreshTokens(accessToken, refreshToken, infos);
7161

72-
return refreshedSession;
73-
});
62+
return refreshedSession;
7463
},
7564
};
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { AccountsMagicLink, RequestMagicLinkEmailErrors } from '@accounts/magic-link';
22
import { AccountsServer, AccountsJsError } from '@accounts/server';
3-
import { ctxAsyncLocalStorage } from '@accounts/types';
43
import { MutationResolvers } from '../models';
54

65
export const Mutation: MutationResolvers = {
@@ -9,23 +8,21 @@ export const Mutation: MutationResolvers = {
98
const accountsServer = injector.get(AccountsServer);
109
const accountsMagicLink = injector.get(AccountsMagicLink);
1110

12-
return ctxAsyncLocalStorage.run(ctx, async () => {
13-
try {
14-
await accountsMagicLink.requestMagicLinkEmail(email);
15-
} catch (error) {
16-
// If ambiguousErrorMessages is true,
17-
// to prevent user enumeration we fail silently in case there is no user attached to this email
18-
if (
19-
accountsServer.options.ambiguousErrorMessages &&
20-
error instanceof AccountsJsError &&
21-
error.code === RequestMagicLinkEmailErrors.UserNotFound
22-
) {
23-
return null;
24-
}
25-
throw error;
11+
try {
12+
await accountsMagicLink.requestMagicLinkEmail(email);
13+
} catch (error) {
14+
// If ambiguousErrorMessages is true,
15+
// to prevent user enumeration we fail silently in case there is no user attached to this email
16+
if (
17+
accountsServer.options.ambiguousErrorMessages &&
18+
error instanceof AccountsJsError &&
19+
error.code === RequestMagicLinkEmailErrors.UserNotFound
20+
) {
21+
return null;
2622
}
23+
throw error;
24+
}
2725

28-
return null;
29-
});
26+
return null;
3027
},
3128
};
Lines changed: 69 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CreateUserServicePassword, ctxAsyncLocalStorage } from '@accounts/types';
1+
import { CreateUserServicePassword } from '@accounts/types';
22
import {
33
AccountsPassword,
44
CreateUserErrors,
@@ -18,10 +18,8 @@ export const Mutation: MutationResolvers = {
1818

1919
const userId = user.id;
2020

21-
return ctxAsyncLocalStorage.run(ctx, async () => {
22-
await injector.get(AccountsPassword).addEmail(userId, newEmail);
23-
return null;
24-
});
21+
await injector.get(AccountsPassword).addEmail(userId, newEmail);
22+
return null;
2523
},
2624
changePassword: async (_, { oldPassword, newPassword }, ctx) => {
2725
const { user, injector } = ctx;
@@ -32,10 +30,8 @@ export const Mutation: MutationResolvers = {
3230

3331
const userId = user.id;
3432

35-
return ctxAsyncLocalStorage.run(ctx, async () => {
36-
await injector.get(AccountsPassword).changePassword(userId, oldPassword, newPassword);
37-
return null;
38-
});
33+
await injector.get(AccountsPassword).changePassword(userId, oldPassword, newPassword);
34+
return null;
3935
},
4036
createUser: async (_, { user }, ctx) => {
4137
const { injector, infos } = ctx;
@@ -44,43 +40,41 @@ export const Mutation: MutationResolvers = {
4440

4541
let userId: string;
4642

47-
return ctxAsyncLocalStorage.run(ctx, async () => {
48-
try {
49-
userId = await accountsPassword.createUser(user as CreateUserServicePassword);
50-
} catch (error) {
51-
// If ambiguousErrorMessages is true we obfuscate the email or username already exist error
52-
// to prevent user enumeration during user creation
53-
if (
54-
accountsServer.options.ambiguousErrorMessages &&
55-
error instanceof AccountsJsError &&
56-
(error.code === CreateUserErrors.EmailAlreadyExists ||
57-
error.code === CreateUserErrors.UsernameAlreadyExists)
58-
) {
59-
return {};
60-
}
61-
throw error;
43+
try {
44+
userId = await accountsPassword.createUser(user as CreateUserServicePassword);
45+
} catch (error) {
46+
// If ambiguousErrorMessages is true we obfuscate the email or username already exist error
47+
// to prevent user enumeration during user creation
48+
if (
49+
accountsServer.options.ambiguousErrorMessages &&
50+
error instanceof AccountsJsError &&
51+
(error.code === CreateUserErrors.EmailAlreadyExists ||
52+
error.code === CreateUserErrors.UsernameAlreadyExists)
53+
) {
54+
return {};
6255
}
56+
throw error;
57+
}
6358

64-
if (!accountsServer.options.enableAutologin) {
65-
return {
66-
userId: accountsServer.options.ambiguousErrorMessages ? null : userId,
67-
};
68-
}
59+
if (!accountsServer.options.enableAutologin) {
60+
return {
61+
userId: accountsServer.options.ambiguousErrorMessages ? null : userId,
62+
};
63+
}
6964

70-
// When initializing AccountsServer we check that enableAutologin and ambiguousErrorMessages options
71-
// are not enabled at the same time
65+
// When initializing AccountsServer we check that enableAutologin and ambiguousErrorMessages options
66+
// are not enabled at the same time
7267

73-
const createdUser = await accountsServer.findUserById(userId);
68+
const createdUser = await accountsServer.findUserById(userId);
7469

75-
// If we are here - user must be created successfully
76-
// Explicitly saying this to Typescript compiler
77-
const loginResult = await accountsServer.loginWithUser(createdUser!, infos);
70+
// If we are here - user must be created successfully
71+
// Explicitly saying this to Typescript compiler
72+
const loginResult = await accountsServer.loginWithUser(createdUser!, infos);
7873

79-
return {
80-
userId,
81-
loginResult,
82-
};
83-
});
74+
return {
75+
userId,
76+
loginResult,
77+
};
8478
},
8579
twoFactorSet: async (_, { code, secret }, ctx) => {
8680
const { user, injector } = ctx;
@@ -92,10 +86,8 @@ export const Mutation: MutationResolvers = {
9286

9387
const userId = user.id;
9488

95-
return ctxAsyncLocalStorage.run(ctx, async () => {
96-
await injector.get(AccountsPassword).twoFactor.set(userId, secret as any, code);
97-
return null;
98-
});
89+
await injector.get(AccountsPassword).twoFactor.set(userId, secret as any, code);
90+
return null;
9991
},
10092
twoFactorUnset: async (_, { code }, ctx) => {
10193
const { user, injector } = ctx;
@@ -107,71 +99,61 @@ export const Mutation: MutationResolvers = {
10799

108100
const userId = user.id;
109101

110-
return ctxAsyncLocalStorage.run(ctx, async () => {
111-
await injector.get(AccountsPassword).twoFactor.unset(userId, code);
112-
return null;
113-
});
102+
await injector.get(AccountsPassword).twoFactor.unset(userId, code);
103+
return null;
114104
},
115105
resetPassword: async (_, { token, newPassword }, ctx) => {
116106
const { injector, infos } = ctx;
117107

118-
return ctxAsyncLocalStorage.run(ctx, async () => {
119-
return injector.get(AccountsPassword).resetPassword(token, newPassword, infos);
120-
});
108+
return injector.get(AccountsPassword).resetPassword(token, newPassword, infos);
121109
},
122110
sendResetPasswordEmail: async (_, { email }, ctx) => {
123111
const { injector } = ctx;
124112
const accountsServer = injector.get(AccountsServer);
125113
const accountsPassword = injector.get(AccountsPassword);
126114

127-
return ctxAsyncLocalStorage.run(ctx, async () => {
128-
try {
129-
await accountsPassword.sendResetPasswordEmail(email);
130-
} catch (error) {
131-
// If ambiguousErrorMessages is true,
132-
// to prevent user enumeration we fail silently in case there is no user attached to this email
133-
if (
134-
accountsServer.options.ambiguousErrorMessages &&
135-
error instanceof AccountsJsError &&
136-
error.code === SendResetPasswordEmailErrors.UserNotFound
137-
) {
138-
return null;
139-
}
140-
throw error;
115+
try {
116+
await accountsPassword.sendResetPasswordEmail(email);
117+
} catch (error) {
118+
// If ambiguousErrorMessages is true,
119+
// to prevent user enumeration we fail silently in case there is no user attached to this email
120+
if (
121+
accountsServer.options.ambiguousErrorMessages &&
122+
error instanceof AccountsJsError &&
123+
error.code === SendResetPasswordEmailErrors.UserNotFound
124+
) {
125+
return null;
141126
}
127+
throw error;
128+
}
142129

143-
return null;
144-
});
130+
return null;
145131
},
146132
verifyEmail: async (_, { token }, ctx) => {
147133
const { injector } = ctx;
148134

149-
return ctxAsyncLocalStorage.run(ctx, async () => {
150-
await injector.get(AccountsPassword).verifyEmail(token);
151-
return null;
152-
});
135+
await injector.get(AccountsPassword).verifyEmail(token);
136+
return null;
153137
},
154138
sendVerificationEmail: async (_, { email }, ctx) => {
155139
const { injector } = ctx;
156140
const accountsServer = injector.get(AccountsServer);
157141
const accountsPassword = injector.get(AccountsPassword);
158142

159-
return ctxAsyncLocalStorage.run(ctx, async () => {
160-
try {
161-
await accountsPassword.sendVerificationEmail(email);
162-
} catch (error) {
163-
// If ambiguousErrorMessages is true,
164-
// to prevent user enumeration we fail silently in case there is no user attached to this email
165-
if (
166-
accountsServer.options.ambiguousErrorMessages &&
167-
error instanceof AccountsJsError &&
168-
error.code === SendVerificationEmailErrors.UserNotFound
169-
) {
170-
return null;
171-
}
172-
throw error;
143+
try {
144+
await accountsPassword.sendVerificationEmail(email);
145+
} catch (error) {
146+
// If ambiguousErrorMessages is true,
147+
// to prevent user enumeration we fail silently in case there is no user attached to this email
148+
if (
149+
accountsServer.options.ambiguousErrorMessages &&
150+
error instanceof AccountsJsError &&
151+
error.code === SendVerificationEmailErrors.UserNotFound
152+
) {
153+
return null;
173154
}
174-
return null;
175-
});
155+
throw error;
156+
}
157+
return null;
176158
},
177159
};
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { QueryResolvers } from '../models';
22
import { AccountsPassword } from '@accounts/password';
3-
import { ctxAsyncLocalStorage } from '@accounts/types';
43

54
export const Query: QueryResolvers = {
65
twoFactorSecret: async (_, args, ctx) => {
@@ -11,10 +10,8 @@ export const Query: QueryResolvers = {
1110
throw new Error('Unauthorized');
1211
}
1312

14-
return ctxAsyncLocalStorage.run(ctx, async () => {
15-
// https://github.com/speakeasyjs/speakeasy/blob/master/index.js#L517
16-
const secret = injector.get(AccountsPassword).twoFactor.getNewAuthSecret();
17-
return secret;
18-
});
13+
// https://github.com/speakeasyjs/speakeasy/blob/master/index.js#L517
14+
const secret = injector.get(AccountsPassword).twoFactor.getNewAuthSecret();
15+
return secret;
1916
},
2017
};

0 commit comments

Comments
 (0)