Skip to content

Update email.mdx #12969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions docs/pages/getting-started/authentication/email.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ export { handle } from "./auth"
```

</Code.Svelte>
<Code.Express>
```ts filename="./server.ts"
import express from 'express';
import dotenv from 'dotenv';
import authRoutes from './routes/authRoutes';

dotenv.config();
const app = express();

app.use(express.json());
app.use('/auth', authRoutes);

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
```
</Code.Express>
</Code>

### Add Signin Button
Expand Down Expand Up @@ -226,6 +244,21 @@ export default component$(() => {
```

</Code.Svelte>

<Code.Express>
```ts filename="routes/authRoutes.ts"

import { Router } from 'express';
import { sendMagicLink, verifyMagicLink } from '../controllers/authController';

const router = Router();

router.post('/login', sendMagicLink);
router.get('/verify', verifyMagicLink);

export default router;
```
</Code.Express>
</Code>

### Signin
Expand Down Expand Up @@ -316,6 +349,54 @@ export { handle } from "./auth"
```

</Code.Svelte>

<Code.Express>
```ts filename="controllers/authController.ts"

import { Request, Response } from 'express';
import jwt from 'jsonwebtoken';
import { Resend } from 'resend';
import dotenv from 'dotenv';

dotenv.config();

const resend = new Resend(process.env.RESEND_API_KEY || '');

export const sendMagicLink = async (req: Request, res: Response) => {
const { email } = req.body;

if (!email) {
return res.status(400).json({ error: 'Email is required' });
}

const token = jwt.sign({ email }, process.env.JWT_SECRET as string, { expiresIn: '15m' });
const magicLink = `${process.env.BASE_URL}/auth/verify?token=${token}`;

try {
await resend.emails.send({
from: 'YourApp <[email protected]>',
to: email,
subject: 'Login Link',
html: `<p>Click to log in: <a href="${magicLink}">${magicLink}</a></p>`,
});
res.status(200).json({ message: 'Magic link sent' });
} catch (error) {
res.status(500).json({ error: 'Failed to send email' });
}
};

export const verifyMagicLink = (req: Request, res: Response) => {
const { token } = req.query;

try {
const decoded = jwt.verify(token as string, process.env.JWT_SECRET as string) as { email: string };
res.status(200).json({ message: 'Authenticated', email: decoded.email });
} catch (err) {
res.status(401).json({ error: 'Invalid or expired token' });
}
};
```
</Code.Express>
</Code>

### Add Signin Button
Expand Down Expand Up @@ -404,6 +485,43 @@ export default component$(() => {
```

</Code.Svelte>

<Code.ExpressClient>
```tsx filename="Login.tsx"
import { useState } from 'react';
import axios from 'axios';

const Login = () => {
const [email, setEmail] = useState('');
const [message, setMessage] = useState('');

const handleSendLink = async () => {
try {
const res = await axios.post('/auth/login', { email });
setMessage(res.data.message);
} catch (err: any) {
setMessage(err.response?.data?.error || 'Error sending link');
}
};

return (
<div>
<h2>Login with Magic Link</h2>
<input
type="email"
placeholder="Enter your email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<button onClick={handleSendLink}>Send Magic Link</button>
<p>{message}</p>
</div>
);
};

export default Login;
```
</Code.ExpressClient>
</Code>

### Signin
Expand Down