Closed didemkkaslan closed 7 months ago
Hi @didemkkaslan thanks for raising this issue!
In aws-amplify
v6, you can import a server-side method for the REST API operations.
For instance, you can refactor your code a bit to something like this:
import { get } from "aws-amplify/api/server";
const user = await runWithAmplifyServerContext({
nextServerContext: { request: ctx.req, response: ctx.res },
operation: async (contextSpec) =>
get(contextSpec, {
apiName: APINames.PlatformCoreRestApi,
path: '/people/me',
options: {
headers: {
Authorization: `Bearer ${contextSpec.token.value.toString()}`,
},
},
}),
});
Can you try this and see if it resolves the issue?
In addition to the above example, the token
property of the contextSpec
object that's passed by runWithAmplifyServerContext
function, is not an auth token.
Hello @chrisbonifacio thanks for the help but I think get only accepts 1 paramater its not really accepting the contextSpec
I've tried below approach also with no luck it returns 401 Unauthorized
const user = await runWithAmplifyServerContext({
nextServerContext: { request: ctx.req, response: ctx.res },
operation: async (contextSpec) =>
get({
apiName: APINames.PlatformCoreRestApi,
path: '/people/me',
options: {
headers: {
Authorization: `Bearer ${contextSpec.token.value.toString()}`,
},
},
}),
});
What worked for me is this one( tho i really dont think its the easiest way):
const user = await runWithAmplifyServerContext({
nextServerContext: { request: ctx.req, response: ctx.res },
operation: async (contextSpec) => {
const session = await fetchAuthSession(contextSpec);
const token = session.tokens?.idToken?.toString();
const response = await get({
apiName: APINames.PlatformCoreRestApi,
path: '/people/me',
options: {
headers: {
Authorization: `Bearer ${token}`,
},
},
});
return (await response.response).body.json() as unknown as PeopleData;
},
});
@didemkkaslan awesome! Yes, fetchAuthSession
would be the recommended way to get the user's cognito tokens.
Thanks @HuiSF for the clarification!
Hi @didemkkaslan This documentation states that all APIs that are supported to be used on the server side, are exported from the /server
subpath.
In your use case you should use the get
API export from aws-amplify/api/server
.
Also, please avoid directly using @aws-amplify
namespaced packages. aws-amplify
is the entry point you should interact with.
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
REST API
Amplify Version
v6
Amplify Categories
api
Backend
CDK
Environment information
Describe the bug
I'm getting unauthorized error trying to access rest api endpoints using getServerSideProps in a NextJS app(pages router).
Expected behavior
It shouldnt be unauthorized
Reproduction steps
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response