initial commit - code moved to separate repo
This commit is contained in:
15
pages/api/examples/jwt.ts
Normal file
15
pages/api/examples/jwt.ts
Normal file
@ -0,0 +1,15 @@
|
||||
// This is an example of how to read a JSON Web Token from an API route
|
||||
import { getToken } from "next-auth/jwt"
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next"
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
// If you don't have the NEXTAUTH_SECRET environment variable set,
|
||||
// you will have to pass your secret as `secret` to `getToken`
|
||||
const token = await getToken({ req })
|
||||
console.log(token)
|
||||
res.send(JSON.stringify(token, null, 2))
|
||||
}
|
19
pages/api/examples/protected.ts
Normal file
19
pages/api/examples/protected.ts
Normal file
@ -0,0 +1,19 @@
|
||||
// This is an example of to protect an API route
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "../auth/[...nextauth]";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
|
||||
if (session) {
|
||||
return res.send({
|
||||
content: "This is protected content. You can access this content because you are signed in.",
|
||||
});
|
||||
}
|
||||
|
||||
res.send({
|
||||
error: "You must be signed in to view the protected content on this page.",
|
||||
});
|
||||
}
|
10
pages/api/examples/session.ts
Normal file
10
pages/api/examples/session.ts
Normal file
@ -0,0 +1,10 @@
|
||||
// This is an example of how to access a session from an API route
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "../auth/[...nextauth]";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
res.send(JSON.stringify(session, null, 2));
|
||||
}
|
Reference in New Issue
Block a user