diff options
author | Vitaly Takmazov | 2023-10-08 21:09:54 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-10-08 21:09:54 +0300 |
commit | 79c54301f1e1c3b72f451b98b3120f01461f35c9 (patch) | |
tree | 7918d5e9a383e90af05722c204a9679e9a16acca | |
parent | f3ec3b0b88a43e2a48db51b81f9b5778ade11248 (diff) |
SSR auth WIP
-rw-r--r-- | vnext/server/middleware/renderer.js | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/vnext/server/middleware/renderer.js b/vnext/server/middleware/renderer.js index ae2600a7..bcecf760 100644 --- a/vnext/server/middleware/renderer.js +++ b/vnext/server/middleware/renderer.js @@ -11,6 +11,8 @@ import { VisitorProvider } from '../../src/ui/VisitorContext' import path from 'path' import fs from 'fs' +import { me } from '../../src/api' +import { HelmetProvider } from 'react-helmet-async' const STATIC_ROOT = config.get('service.static_root') || path.resolve(__dirname, 'public') @@ -22,6 +24,15 @@ const serverRenderer = async (req, res) => { // links const cookies = cookie.parse(req.headers.cookie || '') + + let visitor + + try { + visitor = await me() + } catch(e) { + console.log('Unauthenticated') + } + const links = await getLinks(req.originalUrl, cookies['sape_cookie']) fs.readFile(filePath, 'utf8', (err, htmlData) => { if (err) { @@ -40,10 +51,12 @@ const serverRenderer = async (req, res) => { const propsData = `<script>window.__PROPS__="${btoa(unescape(encodeURIComponent(JSON.stringify(props))))}";</script>${marker}` let didError = false const { pipe } = ReactDOMServer.renderToPipeableStream( - <VisitorProvider> - <StaticRouter location={req.baseUrl} context={routerContext}> - <App {...props} /> - </StaticRouter> + <VisitorProvider auth={visitor}> + <HelmetProvider> + <StaticRouter location={req.baseUrl} context={routerContext}> + <App {...props} /> + </StaticRouter> + </HelmetProvider> </VisitorProvider> , { onShellReady() { @@ -58,10 +71,10 @@ const serverRenderer = async (req, res) => { res.statusCode = 500 res.setHeader('Content-type', 'text/html') res.send( - '<h1>Something went wrong :(</h1>' + '<h1>Something went wrong :(</h1>' ) res.end() - }, + }, onAllReady() { if (!didError) { res.write(data[1]) |