Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 8fb6f44

Browse files
authored
fix(auth.auth_email_passwordless): fix broken login functionality (#85)
`userToken` parameter in Request was practically being ignored, this PR puts it to use in the database
1 parent b9171fa commit 8fb6f44

File tree

2 files changed

+77
-20
lines changed

2 files changed

+77
-20
lines changed

modules/auth/scripts/auth_email_passwordless.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,24 @@ export async function run(
1919

2020
if (!ctx.userConfig.email) throw new RuntimeError("provider_disabled");
2121

22+
// Check if the email is already associated with an identity
23+
const existingIdentity = await ctx.db.emailPasswordless.findFirst({
24+
where: { email: req.email },
25+
});
26+
2227
// Fetch existing user if session token is provided
23-
let userId: string | undefined;
28+
let userId: string | undefined = existingIdentity?.userId;
29+
2430
if (req.userToken) {
25-
const { userId } = await ctx.modules.users.authenticateUser({
31+
const authRes = await ctx.modules.users.authenticateUser({
2632
userToken: req.userToken,
2733
});
2834

29-
// Check if the email is already associated with an identity
30-
const existingIdentity = await ctx.db.emailPasswordless.findFirst({
31-
where: { email: req.email },
32-
});
33-
if (existingIdentity && existingIdentity.userId !== userId) {
35+
if (existingIdentity && existingIdentity.userId !== authRes.userId) {
3436
throw new RuntimeError("email_already_used");
3537
}
38+
39+
userId = authRes.userId;
3640
}
3741

3842
// Create verification

modules/auth/tests/e2e.ts

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,73 @@ import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";
33
import { faker } from "https://deno.land/x/deno_faker@v1.0.3/mod.ts";
44

55
test("e2e", async (ctx: TestContext) => {
6-
const authRes = await ctx.modules.auth.authEmailPasswordless({
7-
email: faker.internet.email(),
8-
});
6+
// First we create a new user, and "register" into the auth
7+
// using an authEmailPasswordless({ email, userToken })
8+
// call
9+
const { user } = await ctx.modules.users.createUser({});
910

10-
// Look up correct code
11-
const { code } = await ctx.db.emailPasswordlessVerification.findFirstOrThrow({
12-
where: {
13-
id: authRes.verification.id,
14-
},
11+
const { token: session } = await ctx.modules.users.createUserToken({
12+
userId: user.id
1513
});
1614

17-
const verifyRes = await ctx.modules.auth.verifyEmailPasswordless({
18-
verificationId: authRes.verification.id,
19-
code: code,
20-
});
21-
assertEquals(verifyRes.token.type, "user");
15+
const fakeEmail = faker.internet.email();
16+
17+
// Now we test that post-signin, we get the same user
18+
{
19+
const authRes = await ctx.modules.auth.authEmailPasswordless({
20+
email: fakeEmail,
21+
userToken: session.token
22+
});
23+
24+
// Look up correct code
25+
const { code } = await ctx.db.emailPasswordlessVerification.findFirstOrThrow({
26+
where: {
27+
id: authRes.verification.id,
28+
},
29+
});
30+
31+
// Now by verifying the email, we register, and can also use
32+
// this to verify the token
33+
const verifyRes = await ctx.modules.auth.verifyEmailPasswordless({
34+
verificationId: authRes.verification.id,
35+
code: code,
36+
});
37+
38+
assertEquals(verifyRes.token.type, "user");
39+
40+
41+
// Make sure we end up with the same user we started with
42+
const verifyRes2 = await ctx.modules.users.authenticateUser({
43+
userToken: verifyRes.token.token
44+
});
45+
46+
assertEquals(verifyRes2.userId, user.id);
47+
}
48+
49+
// Now we try logging back in with the same email,
50+
// but without a token, expecting the same user
51+
{
52+
const authRes = await ctx.modules.auth.authEmailPasswordless({
53+
email: fakeEmail
54+
});
55+
56+
// Look up correct code
57+
const { code: code } = await ctx.db.emailPasswordlessVerification.findFirstOrThrow({
58+
where: {
59+
id: authRes.verification.id,
60+
},
61+
});
62+
63+
const verifyRes = await ctx.modules.auth.verifyEmailPasswordless({
64+
verificationId: authRes.verification.id,
65+
code: code,
66+
});
67+
68+
const verifyRes2 = await ctx.modules.users.authenticateUser({
69+
userToken: verifyRes.token.token
70+
});
71+
72+
assertEquals(verifyRes2.userId, user.id);
73+
}
2274
});
75+

0 commit comments

Comments
 (0)