diff --git a/packages/atlas-meta-server/.env b/packages/atlas-meta-server/.env index 295998af6d..aa9e254c90 100644 --- a/packages/atlas-meta-server/.env +++ b/packages/atlas-meta-server/.env @@ -3,3 +3,5 @@ APP_URL=https://gleev.xyz # ORION_URL is used only for generating queries code, in runtime, Orion URL is fetched from index.html ORION_URL=https://orion.joystream.org/graphql +# APP_AUTH_URL is used to obtain authorization cookie, which will be used later to fetch data from Orion +APP_AUTH_URL=https://auth.gleev.xyz/api/v1/anonymous-auth diff --git a/packages/atlas-meta-server/src/api/index.ts b/packages/atlas-meta-server/src/api/index.ts index 2c52cc1e11..eb7dbdb152 100644 --- a/packages/atlas-meta-server/src/api/index.ts +++ b/packages/atlas-meta-server/src/api/index.ts @@ -5,8 +5,8 @@ import { getSdk } from './__generated__/sdk' export class OrionClient { private sdk: ReturnType - constructor(graphqlUrl: string) { - const client = new GraphQLClient(graphqlUrl) + constructor(graphqlUrl: string, sessionCookie: string) { + const client = new GraphQLClient(graphqlUrl, { headers: { Cookie: sessionCookie } }) this.sdk = getSdk(client) } diff --git a/packages/atlas-meta-server/src/config.ts b/packages/atlas-meta-server/src/config.ts index 43199174fa..a1a24e52e8 100644 --- a/packages/atlas-meta-server/src/config.ts +++ b/packages/atlas-meta-server/src/config.ts @@ -5,7 +5,8 @@ import { getEnvVariable } from './utils' dotenv.config() const APP_URL = getEnvVariable('APP_URL', true) +const APP_AUTH_URL = getEnvVariable('APP_AUTH_URL', true) const PORT = 80 -export { PORT, APP_URL } +export { PORT, APP_URL, APP_AUTH_URL } diff --git a/packages/atlas-meta-server/src/index.ts b/packages/atlas-meta-server/src/index.ts index 16ce1f5924..dd5b1f2196 100644 --- a/packages/atlas-meta-server/src/index.ts +++ b/packages/atlas-meta-server/src/index.ts @@ -2,7 +2,7 @@ import express from 'express' import { OrionClient } from './api' -import { APP_URL, PORT } from './config' +import { APP_AUTH_URL, APP_URL, PORT } from './config' import { generateChannelMetaTags, generateChannelSchemaTagsHtml, @@ -10,7 +10,13 @@ import { generateVideoMetaTags, generateVideoSchemaTagsHtml, } from './tags' -import { applyMetaTagsToHtml, applySchemaTagsToHtml, fetchHtmlAndAppData, generateAssetUrl } from './utils' +import { + applyMetaTagsToHtml, + applySchemaTagsToHtml, + fetchAuthCookie, + fetchHtmlAndAppData, + generateAssetUrl, +} from './utils' const app = express() let orionClient: OrionClient @@ -108,9 +114,14 @@ const init = async () => { const [html, appData] = await fetchHtmlAndAppData(APP_URL) console.log('App data fetched') console.log(JSON.stringify(appData, null, 2)) - + console.log('Obtaining auth cookie...') + const authCookie = (await fetchAuthCookie(APP_AUTH_URL)) || '' + console.log(authCookie) + if (!authCookie) { + console.log('Cookie is empty, something went wrong') + } console.log('Initializing Orion client...') - orionClient = new OrionClient(appData.orionUrl) + orionClient = new OrionClient(appData.orionUrl, authCookie) await orionClient.testConnection() console.log('Orion client initialized') diff --git a/packages/atlas-meta-server/src/utils.ts b/packages/atlas-meta-server/src/utils.ts index 1366e03667..d7a798871e 100644 --- a/packages/atlas-meta-server/src/utils.ts +++ b/packages/atlas-meta-server/src/utils.ts @@ -56,6 +56,17 @@ export const applySchemaTagsToHtml = (html: HTMLElement, schemaTags: string) => head?.insertAdjacentHTML('beforeend', schemaTags) } +export const fetchAuthCookie = async (authUrl: string) => { + const response = await fetch(authUrl, { + method: 'POST', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + }) + return response.headers.get('set-cookie') +} + export const fetchHtmlAndAppData = async (url: string): Promise<[HTMLElement, AppData]> => { // fetch and parse html const response = await fetch(url)