-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
feat(@aws-amplify/auth): UniversalStorage for SSR #5710
Conversation
Codecov Report
@@ Coverage Diff @@
## main #5710 +/- ##
==========================================
+ Coverage 73.48% 73.51% +0.02%
==========================================
Files 205 204 -1
Lines 12611 11915 -696
Branches 2457 2335 -122
==========================================
- Hits 9267 8759 -508
+ Misses 3153 2979 -174
+ Partials 191 177 -14
Continue to review full report at Codecov.
|
@@ -48,6 +48,7 @@ export { | |||
Signer, | |||
I18n, | |||
ServiceWorker, | |||
UniversalStorage, |
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.
Is this a breaking change that we would need to do a Major publish for?
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.
It would be a minor.
: // @ts-ignore Argument of type 'Record<string, string>' is not assignable to parameter of type 'Pick<any, "req"> | { req: any; }'. | ||
// Property 'req' is missing in type 'Record<string, string>' but required in type '{ req: any; }'.ts(2345) |
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.
There are a few // @ts-ignore
comments that exist because of nookies
' forked behavior between server & client. On the server, context
is { req, res }
, while on the browser, it comes from document.cookies
.
…auth # Conflicts: # packages/core/package.json
// @ts-ignore Argument of type 'Record<string, string>' is not assignable to parameter of type 'Pick<any, "res"> | { res: any; }'. | ||
// Property 'res' is missing in type 'Record<string, string>' but required in type '{ res: any; }'.ts(2345) | ||
nookies.set(this.store, key, value, { | ||
maxAge: 30 * 24 * 60 * 60, |
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.
Remove this so that it's session storage only.
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.
Frameworks take care of XSRF.
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.
Set HTTP-only (document.cookie
) & Secure (HTTPS) for live environments.
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.
Don't set it to the top-level domain.
Fixed via #6146 |
This pull request has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs. Looking for a help forum? We recommend joining the Amplify Community Discord server |
Why
window.localStorage
isn't available to server-side processes, unless credentials are explicitly sent via headers, query params, or POST body.However, cookies are, which means that loading
https://example.com/profile
can render a user-specific page on the server without making a round-trip on the client as you would in a SPA.What
Add support for Next.js (#5435) via a new
UniversalStorage
adapter that persists the minimum subset of credentials in cookies, so that they're available on the server for authenticated requests.The following real, working examples come from my private sample: https://github.com/aws-amplify/amplify-js-samples-staging/pull/90
How
isomorphic-unfetch
to support SSR fetch for CognitoUniversalStorage
from my sample to be exported from bothaws-amplify
and@aws-amplify/core
.authToken
andidToken
are persisted for the server.localStorage
, as usual.UniversalStorage
is the default in the browser or not.UniversalStorage.setContext(ctx)
is still required per-request.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.