From 79c54301f1e1c3b72f451b98b3120f01461f35c9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 8 Oct 2023 21:09:54 +0300 Subject: SSR auth WIP --- vnext/server/middleware/renderer.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'vnext/server/middleware') 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 = `${marker}` let didError = false const { pipe } = ReactDOMServer.renderToPipeableStream( - - - - + + + + + + , { onShellReady() { @@ -58,10 +71,10 @@ const serverRenderer = async (req, res) => { res.statusCode = 500 res.setHeader('Content-type', 'text/html') res.send( - '

Something went wrong :(

' + '

Something went wrong :(

' ) res.end() - }, + }, onAllReady() { if (!didError) { res.write(data[1]) -- cgit v1.2.3