Using aws amplify only stores session in frontend, how do I access it in backend
Answered
Siricid woodwasp posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Siricid woodwaspOP
While this does store session in cookies but I dont have access to resources in server such as getUser()
export async function handleSignIn({ username, password }: SignInInput) {
try {
const { isSignedIn, nextStep } = await signIn({
username,
password,
options: { authFlowType: "USER_SRP_AUTH" },
});
if (isSignedIn) {
console.log("signed in");
const session = await fetchAuthSession();
console.log("session", session);
cognitoUserPoolsTokenProvider.setKeyValueStorage(new CookieStorage());
}
} catch (error) {
console.log("error signing in", error);
}
}
Answered by Ray
import { cookies } from 'next/headers';
import { getCurrentUser } from '@aws-amplify/auth/server';
import { runWithAmplifyServerContext } from '@/utils/amplifyServerUtils';
// This page always dynamically renders per request
export const dynamic = 'force-dynamic';
export default async function AuthGetCurrentUserServer() {
try {
const currentUser = await runWithAmplifyServerContext({
nextServerContext: { cookies },
operation: (contextSpec) => getCurrentUser(contextSpec)
});
return (
<AuthFetchResult
description="The API is called on the server side."
data={currentUser}
/>
);
} catch (error) {
console.error(error);
return <p>Something went wrong...</p>;
}
}
5 Replies
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Siricid woodwaspOP
but then I dont have access to anything at frontend
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
backend mean external backend or you mean server side?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Siricid woodwaspOP
server side
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
import { cookies } from 'next/headers';
import { getCurrentUser } from '@aws-amplify/auth/server';
import { runWithAmplifyServerContext } from '@/utils/amplifyServerUtils';
// This page always dynamically renders per request
export const dynamic = 'force-dynamic';
export default async function AuthGetCurrentUserServer() {
try {
const currentUser = await runWithAmplifyServerContext({
nextServerContext: { cookies },
operation: (contextSpec) => getCurrentUser(contextSpec)
});
return (
<AuthFetchResult
description="The API is called on the server side."
data={currentUser}
/>
);
} catch (error) {
console.error(error);
return <p>Something went wrong...</p>;
}
}
Answer