question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server side authentication with cookies

See original GitHub issue

I store an authentication token (JWT) in a cookie. I’m trying to access this server side by creating an AuthLink that I compose with HttpLink, which is then passed to the withData HOC. The problem is that in my AuthLink component, I don’t seem to have access to the request so that I can set cookies so that the graphql request made in getDataFromTree is properly authenticated.

I’ve looked at other approaches that are similar to next-apollo and they all pass in the ctx from getInitialProps into the function that initializes the ApolloClient. For example:

// example `withApollo` HOC that passes `ctx` (albeit indirectly through `getToken`)
// that can be accessed in an AuthLink component to access the request cookies
static async getInitialProps(context) {
      const { Component, router, ctx } = context;
      const apolloClient = await initApollo(
        {},
        {
          getToken: () => nextCookies(ctx).token,
        },
      );

When using the withData HOC, it doesn’t appear that the request context is made available to the ApolloClient init function, so I’m unable to access and set the cookies in the server side graphql request:

// withData.js
WithApollo.getInitialProps = async ctx => {
          const { AppTree } = ctx

          let pageProps = {}
          if (PageComponent.getInitialProps) {
            pageProps = await PageComponent.getInitialProps(ctx)
          }

          // Run all GraphQL queries in the component tree
          // and extract the resulting data
          const apolloClient = initApolloClient(apolloConfig, null) // <-- ctx not passed in

I feel like I’m missing something obvious. How do I make an authenticated server side graphql request with next-apollo? Is there a way to get the current request so that I can get the cookies server side?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
adamsoffercommented, Oct 22, 2019

Hey @jimwheaton - glad you got that sorted. I’ll make sure to update the lib to support this.

1reaction
adamsoffercommented, May 1, 2020

@mikeruddy I just published v4.0.0 with a fix. Check out the README or this example for the latest configuration instructions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cookie-based vs. Cookieless Authentication: What's the Future?
Cookies are pieces of data used to identify the user and their preferences. The browser returns the cookie to the server every time...
Read more >
How does cookie based authentication work? - Stack Overflow
The user provides a username and password in the login form and the client/browser sends a login request. · After the request is...
Read more >
Cookie vs Token authentication - Section.io
A Cookie-based authentication uses the HTTP cookies to authenticate the client requests and maintain session information on the server over ...
Read more >
Cookies - Auth0
Traditional, server-side web applications have traditionally used cookie-based authentication. Cookie-based authentication is implemented by each web platform ...
Read more >
Web Authentication: Cookies vs. Tokens - Bits and Pieces
Cookie -Based Authentication · 1. User login to the application using credentials. · 3. Server responds with the cookie to the browser by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found