-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NextJS Example using cookieStorage [WIP] #804
Conversation
Codecov Report
@@ Coverage Diff @@
## master #804 +/- ##
==========================================
- Coverage 95.27% 95.17% -0.11%
==========================================
Files 81 80 -1
Lines 1819 1781 -38
Branches 355 371 +16
==========================================
- Hits 1733 1695 -38
Misses 78 78
Partials 8 8
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks for cleaning that mess! I guess I need to learn to refactor better myself, I just hacked it away until it worked haha!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good so far.
Could you expand this example to also include an apollo client which uses the accounts apollo link to send the tokens to the app's graphql server.
); | ||
|
||
HomePage.getInitialProps = async function(ctx) { | ||
const token = auth(ctx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once you have the access token you need to called accountsServer.resumeSession
which will verify the token, the session, and return the authenticated user. This is similar to what the context builder does in our graphql package.
You can find an example of that code here
export const context = (moduleName: string) => async ( |
@TimMikeladze that's a good idea, maybe our examples should contain some custom queries / mutations to show how you can integrate it with your code (something simple, maybe a todo list?) |
@agustif I pushed some additional code to add a graphql endpoint and create an accounts server with typeorm. It's on the https://github.com/accounts-js/accounts/tree/example-nextjs branch. I'm not quite sure how to go about pushing changes directly to this PR... @pradel any ideas? |
@TimMikeladze I am using https://desktop.github.com/ where you have a gui to clone a pr directly on your machine and then you can push on the same pr :) |
I accepted all incoming changes @tim so yarn.lock wouldn't bitch! seems OK now! |
This is awesome. Thank you. I can't wait to dive into this pr. |
I was having some trouble getting the TokenStorage to work in this example so I implemented my own. Hopefully this will help someone else who ran into this issue as well. import cookies from 'next-cookies'
import Cookies from 'js-cookie'
export const tokenStorage = (ctx: any) => ({
setItem: async (key: string, value: string) => {
Cookies.set(key, value)
},
getItem: async (key: string) => {
const allCookies = cookies(ctx)
const item = allCookies[escape(key)]
return item
},
removeItem: async (key: string) => {
Cookies.remove(key)
}
}) |
@TrillCyborg I am curious, how do you do to inject the token storage to accounts-js afterwards (I guess inside getInitialProps right)? |
Precisely. I pass the |
Hey could someone maybe help wrap this up? Would love to see it merge, but I'm not sure what's missing by @TimMikeladze requsets also maybe you @pradel can help? Thank you guys |
I'm working on my own implementation on this and am hung up on a few things. Next recommends a HOC approach. Here, they use an initApolloClient function to initialize the client. I was able to get user accounts working with SSR using this approach, https://github.com/zeit/next.js/blob/canary/examples/with-apollo-auth/lib/apollo.js Has anyone unlocked this approach completely? |
I have accounts.js working great in a next.js project using this HOC method. What messy-ness are you running into? |
I'd love to check out your implementation, or at least your withApollo. I have things a bit more worked out now, but I'm having a hard time getting the session refreshed without using accounts-link. I'll clean my up a bit and post it in a gist here. |
@TrillCyborg After hacking away on it today, I'm running into issues regarding the order of initializing things where |
This is what's working for me. You need to use the HOC in each page that talks to GraphQL. I hope it helps you. https://gist.github.com/TrillCyborg/7ce2ba18e89176cbff02f1b86a262ca1 |
Thank so much for this. @TrillCyborg moving everything to react context was the missing piece of the loading order puzzle. |
@TrillCyborg all is working well with this approach and I've implemented exactly, however because It seems like when the React context is created client side , the |
Are you using the withApollo HOC on all your pages talking to Apollo or accounts.js? Since the HOC uses getInitialProps you need it on components with a default export in the pages directory. |
@TrillCyborg Yah, wrapping pages like...
All mutations give same error. Before I had deleted the previously saved cookies, the code was SSRing the logged in user just fine, so the server side loading of things work fine. ctx is defined on server side within tokenStorage but not client for some reason. I would post more code, but my withApollo and apollo.ts files the exact same as yours currently. I'm attempting login via context
I'm just now realizing that getInitialProps is only called client side via client side routing by NextJS design. So accountsPassword and the like are being created by this line in
Which is initializing without passing ctx, which is why it's unavailable client side. |
I don't know why I didn't think of this sooner, but I just adjusted the getItem method.
|
Is there a better way to use a HOC for JS accounts with the new apollo configuration? |
Hi All, Does anyone have a recent example for using accounts-js client with nextjs? |
AccountsJS + NextJS (SSR)
related to issue #802
This is very much a WIP
Although, it seems Home/Login/Signup work as it is.
It's using apollo-link, I haven't had any problems with localStorage for now.
Still need to work out dynamic routing for reset_password. two_factor. and verify_token.
Feedback very much appreciated @pradel @TimMikeladze @davidyaha