I am looking to retrieve a query token from localhost/get/point?token=hello
.
import { NextResponse } from 'next/server'
import base64url from 'base64url'
type Params = {
token: string
}
export async function GET(req: Request, context: { params: Params }) {
const token = req.query.token
const tokenData = JSON.parse(base64url.decode(token.split('.')[1]))
const verifyResult = Jwt.verify(token)
if (verifyResult == JwtStatusCode.TokenExpired) {
return NextResponse.json({ error: 'Token expired', message: 'Token expired'}, { status: 401 })
} else if (verifyResult === JwtStatusCode.TokenInvalid) {
return NextResponse.json({ error: 'Token invalid', message: 'Token invalid' }, { status: 401 })
}
const data = await client.user.findMany({
where: { name: tokenData.name }
})
if (data.length != 0) {
NextResponse.json({
point: data[0].point,
message: 'Success'
}, { status: 200 })
}
}
Within the pages directory, I utilized req.query for accessing data, but how does it work in the Next13 app directory?
I experimented with splitting the URL, however, I don't find it to be the most efficient method.