Skip to content

Commit 9d37124

Browse files
committed
refactor: enhance magic link strategy to create user if not found and update email templates
1 parent 0d54192 commit 9d37124

File tree

3 files changed

+67
-29
lines changed

3 files changed

+67
-29
lines changed

server/src/auth/strategies/magicLinkEmail.strategy.spec.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('MagicLinkEmailStrategy', () => {
1212

1313
const mockUserService = {
1414
findByEmail: jest.fn(),
15+
createWithEmail: jest.fn(),
1516
};
1617

1718
const mockMailingService = {
@@ -57,7 +58,10 @@ describe('MagicLinkEmailStrategy', () => {
5758
describe('sendMagicLink', () => {
5859
it('should send a magic link email', async () => {
5960
const email = '[email protected]';
60-
const magicLink = '/api/v1/auth/magic-link/callback?token=test_token';
61+
62+
const magicLink =
63+
'http://localhost/api/v1/auth/magic-link/callback?token=test_token';
64+
6165
const user = { username: 'testuser', email };
6266

6367
mockUserService.findByEmail.mockResolvedValue(user);
@@ -69,34 +73,50 @@ describe('MagicLinkEmailStrategy', () => {
6973
)(email, magicLink);
7074

7175
expect(mockUserService.findByEmail).toHaveBeenCalledWith(email);
72-
// TODO: Fix this test
76+
7377
expect(mockMailingService.sendEmail).toHaveBeenCalledWith({
7478
to: email,
7579
context: {
7680
magicLink:
77-
'http://localhost:3000/api/v1/auth/magic-link/callback?token=test_token',
81+
'http://localhost/api/v1/auth/magic-link/callback?token=test_token',
7882
username: 'testuser',
7983
},
8084
subject: 'Noteblock Magic Link',
8185
template: 'magic-link',
8286
});
8387
});
8488

85-
it('should log an error if user is not found', async () => {
86-
const email = '[email protected]';
87-
const magicLink = '/api/v1/auth/magic-link/callback?token=test_token';
89+
it('should create a new user if not found and send a magic link email', async () => {
90+
const email = '[email protected]';
8891

89-
mockUserService.findByEmail.mockResolvedValue(null);
92+
const magicLink =
93+
'http://localhost/api/v1/auth/magic-link/callback?token=test_token';
9094

91-
const loggerSpy = jest.spyOn(MagicLinkEmailStrategy.logger, 'error');
95+
const user = { username: 'testuser', email };
96+
97+
mockUserService.findByEmail.mockResolvedValue(null);
98+
mockUserService.createWithEmail.mockResolvedValue(user);
9299

93100
await MagicLinkEmailStrategy.sendMagicLink(
94101
'http://localhost:3000',
95102
userService,
96103
mailingService,
97104
)(email, magicLink);
98105

99-
expect(loggerSpy).toHaveBeenCalledWith('User not found');
106+
expect(mockUserService.findByEmail).toHaveBeenCalledWith(email);
107+
108+
expect(mockUserService.createWithEmail).toHaveBeenCalledWith(email);
109+
110+
expect(mockMailingService.sendEmail).toHaveBeenCalledWith({
111+
to: email,
112+
context: {
113+
magicLink:
114+
'http://localhost/api/v1/auth/magic-link/callback?token=test_token',
115+
username: 'testuser',
116+
},
117+
subject: 'Welcome to Noteblock.world',
118+
template: 'magic-link-new-account',
119+
});
100120
});
101121
});
102122

server/src/auth/strategies/magicLinkEmail.strategy.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,30 @@ export class MagicLinkEmailStrategy extends PassportStrategy(
5555
async (email: string, magicLink: string) => {
5656
let user = await userService.findByEmail(email);
5757

58-
if (!user)
58+
if (!user) {
5959
// Create user if not found
6060
user = await userService.createWithEmail(email);
6161

62-
const context = {
63-
magicLink: magicLink,
64-
username: user.username,
65-
};
66-
67-
MagicLinkEmailStrategy.logger.debug(
68-
`Sending magic link to ${email} with data ${JSON.stringify(
69-
context,
70-
null,
71-
2,
72-
)}`,
73-
);
74-
75-
mailingService.sendEmail({
76-
to: email,
77-
context,
78-
subject: 'Noteblock Magic Link',
79-
template: 'magic-link',
80-
});
62+
mailingService.sendEmail({
63+
to: email,
64+
context: {
65+
magicLink: magicLink,
66+
username: user.username,
67+
},
68+
subject: 'Welcome to Noteblock.world',
69+
template: 'magic-link-new-account',
70+
});
71+
} else {
72+
mailingService.sendEmail({
73+
to: email,
74+
context: {
75+
magicLink: magicLink,
76+
username: user.username,
77+
},
78+
subject: 'Noteblock Magic Link',
79+
template: 'magic-link',
80+
});
81+
}
8182
};
8283

8384
async validate(payload: authenticationLinkPayload) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Welcome to the OpenNoteBlockWorld!</title>
8+
</head>
9+
10+
<body>
11+
<h1>Wellcome, {{username}}!</h1>
12+
<p>Click the link below to log in to your account:</p>
13+
<a href="{{magicLink}}">Login</a>
14+
<p>If you did not request this login link, please ignore this email.</p>
15+
</body>
16+
17+
</html>

0 commit comments

Comments
 (0)