Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat: show portal console link for signed-in website visitors

Timothy Yinc59cbaa15cf1
diff --git a/apps/cloud/app/components/CloudMarketingHeader.vue b/apps/cloud/app/components/CloudMarketingHeader.vue
index 7671eda5..e149d7bf 100644
--- a/apps/cloud/app/components/CloudMarketingHeader.vue
+++ b/apps/cloud/app/components/CloudMarketingHeader.vue
@@ -2,6 +2,7 @@
import { ArrowUpRight, ArrowRight, Menu } from 'lucide-vue-next'
import { PopoverRoot, PopoverTrigger, PopoverContent } from 'reka-ui'
const route = useRoute()
+const { authenticated } = useCloudMarketingSession()
const mobileMenuOpen = ref(false),
links = useCloudLinks()
</script>
@@ -27,32 +28,50 @@ const mobileMenuOpen = ref(false),
class="cf-inline-icon" /></a
><CloudThemeToggle />
<CfButton
+ v-if="authenticated"
as-child
- variant="secondary"
size="sm"
><NuxtLink
- :to="links.signin"
+ :to="links.console"
external
- >{{ $t('cloud.messages.logIn') }}</NuxtLink
- ></CfButton
- ><CfButton
- as-child
- size="sm"
- ><NuxtLink
- :to="links.signup"
- external
- >{{ $t('cloud.messages.getStarted')
+ >{{ $t('cloud.messages.visitConsole')
}}<ArrowUpRight :size="14" /></NuxtLink
></CfButton>
+ <template v-else>
+ <CfButton
+ as-child
+ variant="secondary"
+ size="sm"
+ ><NuxtLink
+ :to="links.signin"
+ external
+ >{{ $t('cloud.messages.logIn') }}</NuxtLink
+ ></CfButton
+ ><CfButton
+ as-child
+ size="sm"
+ ><NuxtLink
+ :to="links.signup"
+ external
+ >{{ $t('cloud.messages.getStarted')
+ }}<ArrowUpRight :size="14" /></NuxtLink
+ ></CfButton>
+ </template>
</nav>
<div class="cf-mobile-header-actions">
<CfButton
as-child
size="sm"
><NuxtLink
- :to="links.signup"
+ :to="authenticated ? links.console : links.signup"
external
- >{{ $t('cloud.messages.signUp') }}</NuxtLink
+ >{{
+ $t(
+ authenticated
+ ? 'cloud.messages.visitConsole'
+ : 'cloud.messages.signUp',
+ )
+ }}</NuxtLink
></CfButton
>
<PopoverRoot v-model:open="mobileMenuOpen">
@@ -72,9 +91,15 @@ const mobileMenuOpen = ref(false),
>
<nav :aria-label="$t('cloud.messages.mobileNavigation')">
<NuxtLink
- :to="links.signin"
+ :to="authenticated ? links.console : links.signin"
external
- >{{ $t('cloud.messages.logIn') }}<ArrowRight :size="16"
+ >{{
+ $t(
+ authenticated
+ ? 'cloud.messages.visitConsole'
+ : 'cloud.messages.logIn',
+ )
+ }}<ArrowRight :size="16"
/></NuxtLink>
<NuxtLink to="/pricing">{{ $t('cloud.billing.pricing') }}</NuxtLink
><a href="https://docs.chronoframe.art/"
diff --git a/apps/cloud/app/composables/useCloudLinks.ts b/apps/cloud/app/composables/useCloudLinks.ts
index f2b4d064..7d60e11b 100644
--- a/apps/cloud/app/composables/useCloudLinks.ts
+++ b/apps/cloud/app/composables/useCloudLinks.ts
@@ -9,6 +9,8 @@ export function useCloudLinks() {
website: 'https://chronoframe.art/',
signin: portal + '/signin',
signup: portal + '/signup',
+ console: portal + '/workspace',
+ marketingSession: portal + '/api/auth/marketing-session',
redeem: portal + '/account/redeem',
portalPricing: portal + '/account/plan#plans',
purchase: (plan: string, interval: 1 | 3) =>
diff --git a/apps/cloud/app/composables/useCloudMarketingSession.ts b/apps/cloud/app/composables/useCloudMarketingSession.ts
new file mode 100644
index 00000000..0fa87857
--- /dev/null
+++ b/apps/cloud/app/composables/useCloudMarketingSession.ts
@@ -0,0 +1,52 @@
+/** Host-only portal cookies stay on the portal; the website receives only a boolean. */
+export function useCloudMarketingSession() {
+ const links = useCloudLinks()
+ const authenticated = ref(false)
+ let generation = 0,
+ disposed = false,
+ controller: AbortController | undefined
+ async function refresh() {
+ const current = ++generation
+ controller?.abort()
+ controller = new AbortController()
+ const requestController = controller
+ const timeout = setTimeout(() => requestController.abort(), 5000)
+ try {
+ const result = await $fetch<{ authenticated: boolean }>(
+ links.marketingSession,
+ {
+ credentials: 'include',
+ cache: 'no-store',
+ signal: controller.signal,
+ },
+ )
+ if (!disposed && current === generation)
+ authenticated.value = result.authenticated === true
+ } catch {
+ if (!disposed && current === generation) authenticated.value = false
+ } finally {
+ clearTimeout(timeout)
+ }
+ }
+ const resume = () => {
+ void refresh()
+ }
+ const visibility = () => {
+ if (document.visibilityState === 'visible') resume()
+ }
+ onMounted(() => {
+ resume()
+ window.addEventListener('focus', resume)
+ window.addEventListener('pageshow', resume)
+ document.addEventListener('visibilitychange', visibility)
+ })
+ onBeforeUnmount(() => {
+ disposed = true
+ generation++
+ controller?.abort()
+ window.removeEventListener('focus', resume)
+ window.removeEventListener('pageshow', resume)
+ document.removeEventListener('visibilitychange', visibility)
+ })
+ return { authenticated }
+}
diff --git a/apps/cloud/i18n/locales/en.json b/apps/cloud/i18n/locales/en.json
index f30130f1..8574fa6a 100644
--- a/apps/cloud/i18n/locales/en.json
+++ b/apps/cloud/i18n/locales/en.json
@@ -21,6 +21,7 @@
"browseTitle": "Browse your way"
},
"messages": {
+ "visitConsole": "Open console",
"skipToContent": "Skip to content",
"albums": "Albums",
"organizeYourPhotographsAndChooseHowTheyAreShared": "Organize your photographs and choose how they are shared.",
diff --git a/apps/cloud/i18n/locales/zh-Hans.json b/apps/cloud/i18n/locales/zh-Hans.json
index 144cf816..57ba8471 100644
--- a/apps/cloud/i18n/locales/zh-Hans.json
+++ b/apps/cloud/i18n/locales/zh-Hans.json
@@ -21,6 +21,7 @@
"browseTitle": "用喜欢的方式浏览"
},
"messages": {
+ "visitConsole": "访问控制台",
"skipToContent": "跳转到主要内容",
"albums": "相簿",
"organizeYourPhotographsAndChooseHowTheyAreShared": "整理照片,并选择相簿的分享方式。",
diff --git a/apps/cloud/server/api/auth/marketing-session.get.ts b/apps/cloud/server/api/auth/marketing-session.get.ts
new file mode 100644
index 00000000..d2e4bad2
--- /dev/null
+++ b/apps/cloud/server/api/auth/marketing-session.get.ts
@@ -0,0 +1,28 @@
+import { createError, getHeader, setResponseHeaders } from 'h3'
+import {
+ definePlatformHandler,
+ platformPortalOrigin,
+ platformSession,
+} from '../../../../shared/platform/runtime'
+/** Share only login presence with the first-party marketing site, never profile data or cookies. */
+export default definePlatformHandler(async (event) => {
+ const config = useRuntimeConfig(event).platform
+ platformPortalOrigin(event, config.portalOrigin)
+ const origin = getHeader(event, 'origin')
+ const marketing =
+ config.portalOrigin === 'https://app.chronoframe.art' &&
+ origin === 'https://chronoframe.art'
+ if (origin && origin !== config.portalOrigin && !marketing)
+ throw createError({ statusCode: 403 })
+ setResponseHeaders(event, {
+ 'cache-control': 'private, no-store',
+ vary: 'Origin, Cookie',
+ ...(marketing
+ ? {
+ 'access-control-allow-origin': origin!,
+ 'access-control-allow-credentials': 'true',
+ }
+ : {}),
+ })
+ return { authenticated: !!(await platformSession(event, config.secret)) }
+})
diff --git a/apps/cloud/server/middleware/platform-boundary.ts b/apps/cloud/server/middleware/platform-boundary.ts
index 06b7dda4..cfeab095 100644
--- a/apps/cloud/server/middleware/platform-boundary.ts
+++ b/apps/cloud/server/middleware/platform-boundary.ts
@@ -16,6 +16,7 @@ export default defineEventHandler((event) => {
[
'/api/auth/status',
'/api/auth/session',
+ '/api/auth/marketing-session',
'/api/auth/logout',
'/api/auth/security',
'/api/sites',
diff --git a/tests/marketing-session.test.ts b/tests/marketing-session.test.ts
new file mode 100644
index 00000000..7ce97816
--- /dev/null
+++ b/tests/marketing-session.test.ts
@@ -0,0 +1,245 @@
+import test from 'node:test'
+import assert from 'node:assert/strict'
+import { readFileSync } from 'node:fs'
+import vm from 'node:vm'
+import ts from 'typescript'
+import { ref } from 'vue'
+function load(file: string, deps: Record<string, unknown>) {
+ const source = readFileSync(file, 'utf8').replace(
+ /^import[\s\S]*?from ['"][^'"]+['"]\s*\n/gm,
+ '',
+ )
+ const code = ts.transpileModule(source, {
+ compilerOptions: {
+ module: ts.ModuleKind.CommonJS,
+ target: ts.ScriptTarget.ES2022,
+ },
+ }).outputText
+ const exports: Record<string, any> = {}
+ vm.runInNewContext(code, { exports, ...deps })
+ return exports
+}
+test('marketing login status is boolean-only and credentialed CORS is restricted to the canonical website', async () => {
+ let portalOrigin = 'https://app.chronoframe.art',
+ authenticated = true,
+ reads = 0
+ const api = load('apps/cloud/server/api/auth/marketing-session.get.ts', {
+ definePlatformHandler: (handler: unknown) => handler,
+ useRuntimeConfig: () => ({
+ platform: { portalOrigin, secret: 'unused-test-secret' },
+ }),
+ platformPortalOrigin: (event: { target: string }, expected: string) => {
+ if (event.target !== expected) throw { statusCode: 403 }
+ },
+ getHeader: (event: { origin?: string }) => event.origin,
+ createError: (value: { statusCode: number }) => value,
+ setResponseHeaders: (event: { headers: unknown }, headers: unknown) => {
+ event.headers = headers
+ },
+ platformSession: async () => {
+ reads++
+ return authenticated
+ ? { userId: 42, email: '[email protected]', expiresAt: 1 }
+ : null
+ },
+ }).default
+ const event = {
+ target: portalOrigin,
+ origin: 'https://chronoframe.art',
+ headers: {} as Record<string, string>,
+ }
+ assert.equal(JSON.stringify(await api(event)), '{"authenticated":true}')
+ assert.equal(
+ event.headers['access-control-allow-origin'],
+ 'https://chronoframe.art',
+ )
+ assert.equal(event.headers['access-control-allow-credentials'], 'true')
+ assert.equal(event.headers['cache-control'], 'private, no-store')
+ assert.equal(event.headers.vary, 'Origin, Cookie')
+ authenticated = false
+ assert.equal(JSON.stringify(await api(event)), '{"authenticated":false}')
+ const previous = reads
+ for (const origin of [
+ 'https://evil.chronoframe.art',
+ 'https://chronoframe.art.evil.test',
+ 'null',
+ 'http://chronoframe.art',
+ ])
+ await assert.rejects(api({ target: portalOrigin, origin }), {
+ statusCode: 403,
+ })
+ await assert.rejects(
+ api({
+ target: 'https://chronoframe.art',
+ origin: 'https://chronoframe.art',
+ }),
+ { statusCode: 403 },
+ )
+ portalOrigin = 'https://preview.example'
+ await assert.rejects(
+ api({ target: portalOrigin, origin: 'https://chronoframe.art' }),
+ { statusCode: 403 },
+ )
+ assert.equal(reads, previous)
+ assert.equal(
+ JSON.stringify(await api({ target: portalOrigin, origin: portalOrigin })),
+ '{"authenticated":false}',
+ )
+ const boundary = load('apps/cloud/server/middleware/platform-boundary.ts', {
+ defineEventHandler: (handler: unknown) => handler,
+ setResponseHeader: () => {},
+ getRequestURL: () =>
+ new URL('https://app.chronoframe.art/api/auth/marketing-session'),
+ isProcessorAction: () => false,
+ accountRouteAllowed: () => false,
+ createError: (value: unknown) => value,
+ }).default
+ assert.doesNotThrow(() => boundary({ method: 'GET' }))
+})
+test('marketing session refreshes on return, ignores stale responses and cleans up listeners', async () => {
+ const window = new EventTarget()
+ const document = Object.assign(new EventTarget(), {
+ visibilityState: 'visible',
+ })
+ let mount!: () => void, unmount!: () => void
+ const requests: {
+ url: string
+ options: { credentials: string; cache: string; signal: AbortSignal }
+ resolve: (value: unknown) => void
+ reject: (reason: unknown) => void
+ }[] = []
+ const module = load(
+ 'apps/cloud/app/composables/useCloudMarketingSession.ts',
+ {
+ ref,
+ AbortController,
+ setTimeout,
+ clearTimeout,
+ window,
+ document,
+ useCloudLinks: () => ({
+ marketingSession:
+ 'https://app.chronoframe.art/api/auth/marketing-session',
+ }),
+ onMounted: (callback: () => void) => {
+ mount = callback
+ },
+ onBeforeUnmount: (callback: () => void) => {
+ unmount = callback
+ },
+ $fetch: (url: string, options: (typeof requests)[number]['options']) =>
+ new Promise((resolve, reject) => {
+ requests.push({ url, options, resolve, reject })
+ }),
+ },
+ )
+ const state = module.useCloudMarketingSession()
+ assert.equal(requests.length, 0) // No cross-origin SSR credential forwarding.
+ mount()
+ assert.equal(requests[0]?.options.credentials, 'include')
+ assert.equal(requests[0]?.options.cache, 'no-store')
+ requests[0]!.resolve({ authenticated: true })
+ await new Promise((resolve) => setImmediate(resolve))
+ assert.equal(state.authenticated.value, true)
+ window.dispatchEvent(new Event('focus'))
+ requests[1]!.resolve({ authenticated: false })
+ await new Promise((resolve) => setImmediate(resolve))
+ assert.equal(state.authenticated.value, false)
+ document.visibilityState = 'hidden'
+ document.dispatchEvent(new Event('visibilitychange'))
+ assert.equal(requests.length, 2)
+ document.visibilityState = 'visible'
+ document.dispatchEvent(new Event('visibilitychange'))
+ window.dispatchEvent(new Event('pageshow'))
+ assert.equal(requests[2]?.options.signal.aborted, true)
+ requests[3]!.resolve({ authenticated: true })
+ await new Promise((resolve) => setImmediate(resolve))
+ requests[2]!.resolve({ authenticated: false })
+ await new Promise((resolve) => setImmediate(resolve))
+ assert.equal(state.authenticated.value, true)
+ window.dispatchEvent(new Event('focus'))
+ requests[4]!.reject(Error('network unavailable'))
+ await new Promise((resolve) => setImmediate(resolve))
+ assert.equal(state.authenticated.value, false)
+ unmount()
+ window.dispatchEvent(new Event('focus'))
+ document.dispatchEvent(new Event('visibilitychange'))
+ assert.equal(requests.length, 5)
+})
+
+test('desktop and mobile marketing navigation reflect login presence and point at the portal console', async () => {
+ const vue = await import('vue')
+ const { parse, compileScript } = await import('vue/compiler-sfc')
+ const { renderToString } = await import('vue/server-renderer')
+ const passthrough = (
+ _props: unknown,
+ context: { slots: { default?: () => unknown } },
+ ) => vue.h('div', context.slots.default?.())
+ const icon = () => vue.h('span')
+ const filename = 'apps/cloud/app/components/CloudMarketingHeader.vue'
+ const { descriptor } = parse(readFileSync(filename, 'utf8'), { filename })
+ const source = compileScript(descriptor, {
+ id: 'marketing-header',
+ inlineTemplate: true,
+ }).content
+ const code = ts.transpileModule(source, {
+ compilerOptions: {
+ module: ts.ModuleKind.CommonJS,
+ target: ts.ScriptTarget.ES2022,
+ },
+ }).outputText
+ for (const authenticated of [true, false]) {
+ const exports: Record<string, any> = {}
+ vm.runInNewContext(code, {
+ exports,
+ ref,
+ useRoute: () => ({ path: '/' }),
+ useCloudMarketingSession: () => ({ authenticated: ref(authenticated) }),
+ useCloudLinks: () => ({
+ console: 'https://app.chronoframe.art/workspace',
+ signin: 'https://app.chronoframe.art/signin',
+ signup: 'https://app.chronoframe.art/signup',
+ }),
+ require: (name: string) =>
+ name === 'vue'
+ ? vue
+ : name === 'lucide-vue-next'
+ ? { ArrowUpRight: icon, ArrowRight: icon, Menu: icon }
+ : {
+ PopoverRoot: passthrough,
+ PopoverContent: passthrough,
+ PopoverTrigger: passthrough,
+ },
+ })
+ const app = vue.createSSRApp(exports.default)
+ app.config.globalProperties.$t = (key: string) =>
+ key === 'cloud.messages.visitConsole' ? '访问控制台' : key
+ app.component('CloudBrand', icon)
+ app.component('CloudThemeToggle', icon)
+ app.component('CfButton', passthrough)
+ app.component('NuxtLink', {
+ props: ['to'],
+ setup:
+ (
+ props: { to: string },
+ context: { slots: { default?: () => unknown } },
+ ) =>
+ () =>
+ vue.h('a', { href: props.to }, context.slots.default?.()),
+ })
+ const html = await renderToString(app)
+ if (authenticated) {
+ assert.equal((html.match(/访问控制台/g) ?? []).length, 3)
+ assert.equal(
+ (html.match(/href="https:\/\/app.chronoframe.art\/workspace"/g) ?? [])
+ .length,
+ 3,
+ )
+ assert(!html.includes('/signup') && !html.includes('/signin'))
+ } else {
+ assert(!html.includes('访问控制台'))
+ assert(html.includes('href="https://app.chronoframe.art/signup"'))
+ assert(html.includes('href="https://app.chronoframe.art/signin"'))
+ }
+ }
+})