Neil Balcombe
05/25/2022, 2:20 PM// Create Api
const api = new Api(stack, 'Api', {
defaults: {
authorizer: 'iam',
},
routes: {
'GET /private': 'functions/private.handler',
'GET /public': {
function: 'functions/public.handler',
authorizer: 'none',
},
},
});
// Create auth provider
const auth = new Auth(stack, 'Auth', {
login: ['email'],
});
// Allow authenticated users invoke API
auth.attachPermissionsForAuthUsers([api]);
// Create a React Static Site
const site = new ViteStaticSite(stack, 'Site', {
path: 'frontend',
environment: {
VITE_APP_API_URL: api.url,
VITE_APP_USER_POOL_ID: auth.userPoolId,
VITE_APP_IDENTITY_POOL_ID: auth.cognitoIdentityPoolId || '',
VITE_APP_USER_POOL_CLIENT_ID: auth.userPoolClientId,
VITE_APP_REGION: app.region,
},
});
I'm managing the login with Amplify and again using Amplify to call the API as such.
API.get("my-api", "/private")
.then((response) => console.log(response))
.catch((e) => console.log(e));
But I'm getting 403 errors when I call the endpoint.
I thought that by using the Amplify 'API' class it what use the logged in user, is this not the case? Am I bound to using the API class or can I achieve the same with using the fetch API?manitej
05/25/2022, 2:28 PMNeil Balcombe
05/25/2022, 2:32 PMAmplify.configure({
Auth: {
mandatorySignIn: true,
region: import.meta.env.VITE_APP_REGION,
userPoolId: import.meta.env.VITE_APP_USER_POOL_ID,
identityPoolId: import.meta.env.VITE_APP_IDENTITY_POOL_ID,
userPoolWebClientId: import.meta.env.VITE_APP_USER_POOL_CLIENT_ID
},
API: {
endpoints: [
{
name: "my-api",
endpoint: import.meta.env.VITE_APP_API_URL,
region: import.meta.env.VITE_APP_REGION
},
]
}
});
Calling the public API endpoint returns correctly, so I guess that tells me that it's all connected up.manitej
05/25/2022, 2:41 PMNeil Balcombe
05/25/2022, 2:45 PMasync function handleSubmit(event) {
try {
await Auth.signIn(email, password);
userHasAuthenticated(true);
console.log("Logged in successfully");
} catch (e) {
console.log(e.message);
}
}
manitej
05/25/2022, 2:45 PMmanitej
05/25/2022, 2:47 PMNeil Balcombe
05/25/2022, 2:54 PMasync function onLoad() {
try {
await Auth.currentSession();
userHasAuthenticated(true);
} catch (e) {
if (e !== "No current user") {
alert(e);
}
}
setIsAuthenticating(false);
}
Neil Balcombe
05/25/2022, 8:37 PM