`This Serverless Function has timed out.` when deploying to vercel
Unanswered
Tonkinese posted this in #help-forum
TonkineseOP
Getting a
This Serverless Function has timed out. when i try to visit my nextjs app on vercel, despite a "Status: ready" status on the deployment page53 Replies
looks like something failed on your api route/server component
check logs ig
TonkineseOP
hmm runtime logs show
[GET] / status=504seems like issue from your codeside
your route prolly didn't respond within 10 secs
TonkineseOP
i can increase it in vercel settings right?
10 secs is limit for hobby plan
TonkineseOP
ah damn
TonkineseOP
it must be my shared free-tier mongodb instance then, that thing is terribly slow lol
TonkineseOP
i guess their dark pattern of providing a free tier with terrible latency works lol
time to upgrade to atlas, thanks for the help 👍
@averydelusionalperson sadge
TonkineseOP
oh i think it could be because of mongo's IP blocking dude
yeah that was the issue
You can use mongodb api, I've specifically made a package for it: https://www.npmjs.com/package/next-mongodb-api
@"use php" Yes, Its very common.
TonkineseOP
also dude, when receiving an oauth code, i'm meant to make another request to the provider's api to request user information right?
Yes, thats correct!
@"use php" Yes, thats correct!
TonkineseOP
yeah awesome, wasn't sure. is there an example of a simple implementation of this? I've got a complex one but i think it might be overkill
@Tonkinese yeah awesome, wasn't sure. is there an example of a simple implementation of this? I've got a complex one but i think it might be overkill
Can I know what provider you are using like apple?
TonkineseOP
just discord
You can also use this package to simplify the process: https://www.npmjs.com/package/arctic
And for generating sessions token, you can use [lucia auth](https://lucia-auth.com/)
documentation for discord: https://arctic.js.org/providers/discord
@Tonkinese i guess their dark pattern of providing a free tier with terrible latency works lol
You can use mongodb Api. I'm using it and it works just fine.
TonkineseOP
just to clarify i'm using auth@beta, prisma adapter and discord provider, can I still use those packages you linked with this setup?
@Tonkinese just to clarify i'm using auth@beta, prisma adapter and discord provider, can I still use those packages you linked with this setup?
if you are using next auth, Then you can use the discord provider inside authjs
TonkineseOP
yeah i've got the authorization request working, and my
callback/discord/route.ts looks like this: export async function GET(req: Request) {
const urlParams = new URL(req.url).searchParams;
const code = urlParams.get("code");
console.log(`CODE: ${code}`);
return NextResponse.redirect('http://localhost:3000')
}that's what I meant when I said " is there an example of a simple implementation of this?"
hold on, let me send you what I've done
TonkineseOP
thanks
Send POST request to
with body:
https://discord.com/api/oauth2/tokenwith body:
client_id: process.env.DISCORD_CLIENT_ID,
client_secret: process.env.DISCORD_CLIENT_SECRET,
grant_type: "authorization_code",
code: code,
redirect_uri: redirect_uriFor getting access token
const response = await fetch("https://discord.com/api/users/@me", {
headers: {
Authorization: `Bearer ${tokens.accessToken}`
}
});
const user = await response.json();TonkineseOP
ah sick, let me give that a shot
@"use php" Send POST request to `https://discord.com/api/oauth2/token`
with body:
TonkineseOP
hmm getting
{
"redirect_uri": [
"Not a well formed URL."
]
} when trying to make that post request@"use php" For getting access token
ts
const response = await fetch("https://discord.com/api/users/@me", {
headers: {
Authorization: `Bearer ${tokens.accessToken}`
}
});
const user = await response.json();
TonkineseOP
does this look ok?
import { NextResponse } from "next/server";
import querystring from "querystring";
export async function GET(req: Request) {
const urlParams = new URL(req.url).searchParams;
const code = urlParams.get("code");
const creds = btoa(`${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`);
const redirect = encodeURIComponent(
"http://localhost:3000/api/auth/discord/callback",
);
const response = await fetch(`https://discord.com/api/oauth2/token`, {
method: "POST",
headers: {
Authorization: `Basic ${creds}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: querystring.stringify({
grant_type: "authorization_code",
code: code,
redirect_uri: redirect,
}),
});
console.log("RESPONSE:");
console.log(response);`
}getting a 400 error
@Tonkinese hmm getting json
{
"redirect_uri": [
"Not a well formed URL."
]
} when trying to make that post request
Is the redirect uri same as where you’re redirecting your user?
TonkineseOP
yeah, i have http://localhost:3000/api/auth/callback/discord and https://myappname.vercel.app/api/auth/callback/discord listed and redirect uris in the discord developer settings
ah
typo
@"use php" Is the redirect uri same as where you’re redirecting your user?
TonkineseOP
hmm nope still getting that 400
(the "Not well formed URL" was an error i was getting in postman, not the above code)
I’ll see when I’m free
TonkineseOP
ah yeah that code above is also getting a
{ redirect_uri: [ 'Not a well formed URL.' ] } errorTonkineseOP
GOT IT WORKING, FINALLY
man what a nightmare
TonkineseOP
ok i've got the user object response from
https://discord.com/api/users/@me, what do I do next?TonkineseOP
like how to i create the following models, given the
user object? https://authjs.dev/getting-started/adapters/prisma#schema