Best way to implement authentication (Oauth2) #1215
-
Hi there, I'm trying to implement user authentication based on Oauth2 Authorization Grant. Currently, I'm registering some global navigation guards to route unauthenticated users to a login page. There, I perform the login and then route them back to the page they wanted to see before the login process started. Those guards I register in While everything works smoothly in development mode, I get a "hydration mismatch" and page rendering breaks in production builds. This happens a) when I directly load a page that protected (e.g. I think it is pretty much this exact bug in vuepress v1: vuejs/vuepress#1382 How woud you go about implementing Oauth2 Authorization Grant in vuepress-next? Any ideas welcome! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You need to show your actual code. If you use beforeRoute guard to perform redirect, then ssg will render any protected pages with login because your code told the renderer to render login page. If you are logined and you no loger perform redirect when redirectly enter an protected page, then you are hygrating "the protected page content" on "the login page". So you will get a mismatch expected. Is my description clear enough to you? To perform such a feature, you may need to "mark the whole page" as client only and render blank content at ssr. Also place the redirect in onMounted lifecycle, check if the users are logined at that time, if not redirect to login page, if so render the content. That should lead to an expected result. Also another workaround is to ensure all pages are redirected to login page in all times, and render protected page contents dynamicly after users are logined. But this workaround is not how vuepress works. |
Beta Was this translation helpful? Give feedback.
You need to show your actual code.
If you use beforeRoute guard to perform redirect, then ssg will render any protected pages with login because your code told the renderer to render login page.
If you are logined and you no loger perform redirect when redirectly enter an protected page, then you are hygrating "the protected page content" on "the login page". So you will get a mismatch expected.
Is my description clear enough to you?
To perform such a feature, you may need to "mark the whole page" as client only and render blank content at ssr. Also place the redirect in onMounted lifecycle, check if the users are logined at that time, if not redirect to login page, if so render the conte…