So I have an Express / TypeGraphql backend running at localhost:5000 and Next / React app running at localhost:3000. I decided to use apollo-server for graphql API and apollo-client for the front end. After a successful login, I store httpOnly cookies in web browser.
What I want to do is, get cookies in every request and authorize the user if oauth2 tokens are valid. Even though requests are valid, and I get the query, cookies are shown as null. Also, I got no error in the console.
Here I save cookies =>
server.tsĀ
app.get('/auth/google/callback', function (req, res, next) {
passport.authenticate('google', { session: false }, (err, user, info) => {
if (err) return next(err);
if (!user) return res.redirect('/');
res.cookie('login_cookie', JSON.stringify(info), {
secure: false,
httpOnly: true,
expires: dayjs().add(30, 'days').toDate(),
sameSite: false,
});
return res.redirect('http://localhost:3000/home');
})(req, res, next);
});
Here I log cookies to console after a request =>
resolver.ts
@Query(() => [User])
async users(@Ctx() ctx: Context) {
console.log('cookies ', ctx.req.cookies);
return await ctx.prisma.user.findMany();
}
and lastly, this is my apollo client config =>
apollo-client.ts
const client = new ApolloClient({
cache: new InMemoryCache(),
credentials: "include",
uri: "http://localhost:5000/graphql",
});
After every request my console shows cookies: [Object: null prototype] {} even though I see them in browser's cookie storage.
request.ts
export async function getServerSideProps() {
const { data } = await client.query({
query: gql`
query allUsers {
users {
id
username
}
}
`,
});
return {
props: {
users: data.users,
},
};
}