Enable 1-click passwordless email authentication with single-use SHA-256 hashed cryptographic tokens and 15-minute expirations.
1. Security Architecture
To prevent database leakage attacks, HVT never stores raw magic link tokens in plaintext. When a user requests a sign-in link, HVT generates a 32-byte cryptographically secure random token, computes its SHA-256 hash, and stores only the hash in the database.
The email sent via Resend contains the raw token in the URL query string. When clicked, HVT hashes the incoming token and compares it using constant-time verification.
2. Requesting a Magic Link (React SDK)
Use useHVTAuth to send a magic link email:
tsx
import { useState } from'react';
import { useHVTAuth } from'@hvt/sdk/react';
exportfunctionMagicLinkForm() {
const { requestMagicLink } = useHVTAuth();
const [email, setEmail] = useState('');
const [sent, setSent] = useState(false);
consthandleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
awaitrequestMagicLink({ email });
setSent(true);
};
if (sent) {
return<p>Check your email! We sent a 1-click login link to {email}.</p>;
}
return (
<formonSubmit={handleSubmit}><inputtype="email"placeholder="[email protected]"value={email}onChange={(e) => setEmail(e.target.value)}
required
/>
<buttontype="submit">Send Magic Link</button></form>
);
}
3. Verification Route & Callback
When the user clicks the link in their email, route them to a verification page: