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)
- Unregistered or expired token error in Android HOT 5
- Support for React Native new architecture and bridgeless HOT 4
- Unable to access database user from my local server in the remote server: LOG {"ACTIVITY_NULL": 4, "PERMISSION_DENIED": 1, "POSITION_UNAVAILABLE": 2, "TIMEOUT": 3, "code": 3, "message": "Location request timed out"} HOT 14
- Azure SSO with Amplify without hosted UI HOT 15
- Proper Way to Combine / Consolidate GraphQL Queries in DynamoDB - Custom Amplify Resolver? - React Native HOT 5
- IAM auth for Appsync from Amplify/Next is (still)Broken HOT 2
- Amplify v6 - TypeError: Cannot read property 'decode' of undefined (underlying error response from `signIn` method) HOT 8
- Authentication does not work with CUSTOM_AUTH and with CONTINUE_SIGN_IN_WITH_MFA_SELECTION HOT 6
- Cant call rest api from getserversideprops HOT 1
- Nested Relational Data Not Returned in GraphQL API Queries on AWS Amplify HOT 5
- DataStore synchronization issues with DataStore under poor network conditions HOT 8
- Amplify v5 build fails when adding amazon-cognito-identity-js because duplicate symbols HOT 10
- signOut() is asking users to sign in again when using apple as identity provider HOT 5
- NotImplemented: A header you provided implies functionality that is not implemented - AWS Amplify Storage remove() HOT 19
- SignIn call gave error - AuthUserPoolException: Auth UserPool not configured. HOT 12
- Type error: Re-exporting a type when 'isolatedModules' is enabled requires using 'export type'. HOT 4
- Datastore.query returns undefined when user logs out and logs in again.React JS Amplify HOT 4
- signInWithRedirect always fails with signInWithRedirect_failure (Using Cognito) HOT 2
- FormData Related Exception When Utilizing Node.js <16.15.0 HOT 1
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.