The MagicLoginStrategy requires an Options object to define how tokens are signed, where users are redirected, and how the magic link is delivered.
Key configuration properties:
secret: The secret key used for signing and verifying JWTs.callbackUrl: The URL where the user is redirected after clicking the magic link (e.g., https://myapp.com/verify).jwtOptions: Optional SignOptions from jsonwebtoken to customize the JWT.sendMagicLink: An asynchronous function responsible for delivering the link. It receives the destination (e.g., email), the generated href, a verificationCode, and the req object.verify: A callback function used to validate the decoded token payload and identify the user.
import MagicLoginStrategy, { Options } from 'passport-magic-login';
const options: Options = {
secret: 'your-very-secure-secret',
callbackUrl: 'https://example.com/auth/callback',
jwtOptions: { expiresIn: '1h' },
sendMagicLink: async (destination, href, verificationCode, req) => {
// Implement your own logic to send email/SMS
await myEmailProvider.send({
to: destination,
body: `Click here to login: ${href}`
});
},
verify: (payload, verifyCallback, req) => {
// Logic to find or create a user based on payload
const user = findUserByEmail(payload.destination);
if (user) {
verifyCallback(null, user);
} else {
verifyCallback(new Error('User not found'));
}
}
};
const strategy = new MagicLoginStrategy(options);