Skip to content

Commit

Permalink
Added auth cookie to atlas-meta-server queries (#4721)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem <Artem Slugin>
  • Loading branch information
attemka authored Aug 24, 2023
1 parent 6f9abaa commit cf413c0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/atlas-meta-server/.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions packages/atlas-meta-server/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { getSdk } from './__generated__/sdk'
export class OrionClient {
private sdk: ReturnType<typeof getSdk>

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)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/atlas-meta-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
19 changes: 15 additions & 4 deletions packages/atlas-meta-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
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,
generateCommonMetaTags,
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
Expand Down Expand Up @@ -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')

Expand Down
11 changes: 11 additions & 0 deletions packages/atlas-meta-server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cf413c0

Please sign in to comment.