Reputation: 2223
This app was a great starter for SvelteKit users trying to implement authentication, but after these breaking changes in v406 the API methods no longer work. This is what the code to hit the route is:
const response = await fetch("/api/sign-in", {
method: "POST",
body: JSON.stringify({ email, password }),
headers: {
"Content-Type": "application/json"
}
});
And this is an example of the tree structure for the routes:
How do I get transform this to make the old routes accessible with the new routing convention?
Upvotes: 2
Views: 3939
Reputation: 2223
With the new routing convention, the contents of api/sign-in.ts
need to be moved to api/sign-in/+server.ts
. Once in the new file, they need to be wrapped in the method(s) you need for the route. An simple example of the syntax for a POST
request route is:
export async function POST({ request }: { request: Request }) {
const { email, password } = await request.json();
// ...
// Insert your real logic here
// ...
// An example of a simple response
return new Response(JSON.stringify({
message: "Hello world!"
}, {
status: 200
});
}
So you would just need to drop your authentication logic into the new file with the correct wrapper, whether that is GET
, POST
, PUT
, etc. Note that the endpoints are required to be all caps now, more information is available here.
Upvotes: 3