-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
drleavio
wants to merge
1
commit into
nextauthjs:main
Choose a base branch
from
drleavio:patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+118
−0
Open
Update email.mdx #12969
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ExpressClient tab hasn’t been set yet. Tabs are managed by the main maintainers and are typically added when documentation reflects new framework support in auth.js, or when previously disabled tabs are reactivated.
For reference, you can check how tabs are handled here:
next-auth/docs/components/Code/index.tsx
Lines 13 to 37 in 39dd3b9
In the meantime, please move the code currently under the ExpressClient tab into the existing Express tab, and mention its usage within a React app context.