bushwallyta271828 / logic_graph_scalable

This repository should support the logicgraph.dev website, taking over from logic_graph.
MIT License
0 stars 0 forks source link

Let's make the server actions in account-actions.tsx work properly (version 3). #172

Closed bushwallyta271828 closed 5 months ago

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

Example response:

backend-1 | INFO: 172.23.0.5:53674 - "GET /users/authenticated HTTP/1.1" 200 OK backend-1 | INFO: 172.23.0.5:59244 - "POST /users/create-account HTTP/1.1" 201 Created backend-1 | INFO: 172.23.0.5:59244 - "GET /users/authenticated HTTP/1.1" 200 OK backend-1 | INFO: 172.23.0.5:59250 - "GET /users/authenticated HTTP/1.1" 200 OK backend-1 | INFO: 172.23.0.5:59254 - "POST /users/sign-out HTTP/1.1" 200 OK backend-1 | INFO: 172.23.0.5:59254 - "GET /users/authenticated HTTP/1.1" 200 OK frontend-1 | tA [Error]: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options frontend-1 | at Proxy.callable (/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:13:12752) frontend-1 | at o (/.next/server/chunks/585.js:1:9509) frontend-1 | at process.processTicksAndRejections (node:internal/process/task_queues:95:5) backend-1 | INFO: 172.23.0.5:59262 - "GET /users/authenticated HTTP/1.1" 200 OK frontend-1 | tA [Error]: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options frontend-1 | at Proxy.callable (/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:13:12752) frontend-1 | at o (/.next/server/chunks/585.js:1:9509) frontend-1 | at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

bushwallyta271828 commented 5 months ago

Apparently you can't call a Server Action from a server-rendered component? Okay, time for some changes... (source: https://github.com/vercel/next.js/issues/51875)

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

At the expense of Account not updating correctly, I think I've fixed the "Cookies can only be modified in a Server Action or Route Handler" bug.

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

The one time when the Account button might not refresh is when we get a 401 response from a timed-out session while performing an unrelated fetch request. Other than that, all other ways in which the authentication state could change (sign in, create account, sign out, delete account) should result in page redirects.

One option is for fetchWrapper to redirect to the sign-in page whenever it gets a 401 error. I would have to think that through and make sure that there are no unintended consequences to that decision, however.

bushwallyta271828 commented 5 months ago

Oh, I just realized: since isAuthenticated doesn't return a 401 error, deleting sessionid cookies on 401 returns won't exactly delete sessionid cookies whenever we know that they should be deleted.

isAuthenticated is also different in that it is the only function whose value directly informs the AccountButton display. Maybe isAuthenticated can be treated as a unique view, i.e. it is the only view that provides information on authentication state where an authentication error doesn't return a 401 status.

bushwallyta271828 commented 5 months ago

Okay, here's the plan:

There are three pieces of information that I need to worry about staying updated for account / homepage purposes: the authentication state as needed in AccountButton, the debate name as needed in DebateName, and the username/email as needed in the Change Account page.

For the authentication state of AccountButton, there are five ways it should change: during sign up, during account creation, during sign out, during account deletion, and from any backend responses that indicate that the user is signed out. The first four ways all involve page redirects when successful, and since AccountButton lives inside a template it will refresh accordingly (CONSTRAINT: ALL FOUR OF THESE ACTIONS MUST HAVE REDIRECTS OR REVALIDATES!). As for backend responses, there are two sub-cases: isAuthenticated and everything else. isAuthenticated gets called by AccountButton itself, and so long as isAuthenticated only gets called by AccountButton, we're safe (CONSTRAINT: isAuthenticated CAN ONLY BE CALLED BY AccountButton AND THE AUTHORIZATION VIEW SHOULD ONLY BE CALLED BY isAuthenticated!). As for everything else, every other action that checks for authentication should return a 401 response when the user is not authenticated (CONSTRAINT: ALL OTHER VIEWS THAT RETURN INFORMATION ABOUT USER AUTHENTICATION SHOULD DO SO BY RETURNING 401). Then the fetchWrapper function can check for 401 responses and redirect to the sign-in page automatically, hence refreshing AccountButton (CONSTRAINT: fetchWrapper MUST REDIRECT / REVALIDATE WHEN GETTING A 401 STATUS). That should cover everything.

For the debate name, that can only get changed when in the debates view, so it will only need to be checked on page redirects (CONSTRAINT: DEBATE NAMES CANNOT BE CHANGED WITHIN THE DEBATE). Since DebateName is on a template, it should update correctly.

Lastly, for username/email in the Change Account page, this information gets updated after page load when the various Change forms are called. Since this information will be displayed inside a Client Component, I can use state variables inside the Client Component to keep track of the username and email and update them if form submission is successful.

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

I'm encountering a bizarre bug where changing the password (not the username or email) will automatically delete the sessionid cookie. I'm not getting a 401 response either.

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO:

TODO for future branches:

bushwallyta271828 commented 5 months ago

TODO for future branches: