Comments (5)
In addition to the above example, the token
property of the contextSpec
object that's passed by runWithAmplifyServerContext
function, is not an auth token.
from amplify-js.
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.
from amplify-js.
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?
from amplify-js.
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;
},
});
from amplify-js.
@didemkkaslan awesome! Yes, fetchAuthSession
would be the recommended way to get the user's cognito tokens.
Thanks @HuiSF for the clarification!
from amplify-js.
Related Issues (20)
- window is not defined in a Lambda function when creating InvokeModelCommand isntance for Bedrock HOT 2
- Auth signUp works but signIn Throws AuthUserPoolException HOT 4
- Auth v6 - How to set cookie HttpOnly? ( Token-saving mechanism - Cookie Storage ) HOT 1
- Fetching Auth Session with / without AWS Credentials HOT 1
- Amplify Rest API Next js, Not signing the request with aws credential HOT 5
- id token doesn't contain ClientMetadata after refresh HOT 7
- Next runWithAmplifyServerContext operation sometimes returns contextSpec for another user if requests are in parallel HOT 1
- Upload of image >5MB using uploadData() ends in 403 Forbidden error HOT 7
- Since v6 I am no longer receiving _userauth.sign_in, _userauth.sign_up, _userauth.auth_fail events. Only _session.start and _session.stop HOT 5
- DataStore - Backend subscriptions filtering error HOT 2
- channelType, Address and optOut removed from v6, Alternative method to set these? HOT 4
- Validating phone number fails even if phone number is optional HOT 3
- TypeError: "Cannot convert null value to object" - AppSync Filters Not Matching Typescript Filters on SecondaryIndex HOT 5
- Login flow - random User cancelled OAuth flow errors HOT 3
- Support for Content-MD5 header irrespective of objectlock. HOT 4
- Using backend for both React Native and Custom Admin page with React JS HOT 9
- Feature Request: Amplify Gen2 SelectionSet Support for CustomQueries
- Feature Request: Amplify Gen 2 - Define Global SelectionSets in resource.ts
- AuthError is an unexpected type calling getCurrentUser() with no authenticated user. HOT 2
- API Adapter Class for REST Operations HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from amplify-js.