restore frontend session
This commit is contained in:
parent
24f437cac6
commit
bc62a25a8e
11 changed files with 146 additions and 14 deletions
|
|
@ -26,6 +26,7 @@
|
||||||
<env name="DB_CONNECTION" value="sqlite"/>
|
<env name="DB_CONNECTION" value="sqlite"/>
|
||||||
<env name="DB_DATABASE" value=":memory:"/>
|
<env name="DB_DATABASE" value=":memory:"/>
|
||||||
<env name="DB_URL" value=""/>
|
<env name="DB_URL" value=""/>
|
||||||
|
<env name="FRONTEND_URL" value="https://localhost:5173"/>
|
||||||
<env name="MAIL_MAILER" value="array"/>
|
<env name="MAIL_MAILER" value="array"/>
|
||||||
<env name="QUEUE_CONNECTION" value="sync"/>
|
<env name="QUEUE_CONNECTION" value="sync"/>
|
||||||
<env name="SESSION_DRIVER" value="array"/>
|
<env name="SESSION_DRIVER" value="array"/>
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ const frontendPort = process.env.VITE_PORT ?? '5173'
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
allowCypressEnv: false,
|
allowCypressEnv: false,
|
||||||
e2e: {
|
e2e: {
|
||||||
baseUrl: `http://127.0.0.1:${frontendPort}`,
|
baseUrl: `https://localhost:${frontendPort}`,
|
||||||
supportFile: false,
|
supportFile: false,
|
||||||
},
|
},
|
||||||
video: false,
|
video: false,
|
||||||
|
|
|
||||||
8
frontend/website/env.d.ts
vendored
8
frontend/website/env.d.ts
vendored
|
|
@ -1 +1,9 @@
|
||||||
/// <reference types="vite/client" />
|
/// <reference types="vite/client" />
|
||||||
|
|
||||||
|
interface ImportMetaEnv {
|
||||||
|
readonly VITE_API_URL: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ImportMeta {
|
||||||
|
readonly env: ImportMetaEnv
|
||||||
|
}
|
||||||
|
|
|
||||||
12
frontend/website/package-lock.json
generated
12
frontend/website/package-lock.json
generated
|
|
@ -10,7 +10,8 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"pinia": "^4.0.2",
|
"pinia": "^4.0.2",
|
||||||
"vue": "^3.5.40",
|
"vue": "^3.5.40",
|
||||||
"vue-router": "^5.2.0"
|
"vue-router": "^5.2.0",
|
||||||
|
"zod": "^4.4.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@tsconfig/node24": "^24.0.4",
|
"@tsconfig/node24": "^24.0.4",
|
||||||
|
|
@ -7620,6 +7621,15 @@
|
||||||
"funding": {
|
"funding": {
|
||||||
"url": "https://github.com/sponsors/sindresorhus"
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"node_modules/zod": {
|
||||||
|
"version": "4.4.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/zod/-/zod-4.4.3.tgz",
|
||||||
|
"integrity": "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/colinhacks"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,8 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"pinia": "^4.0.2",
|
"pinia": "^4.0.2",
|
||||||
"vue": "^3.5.40",
|
"vue": "^3.5.40",
|
||||||
"vue-router": "^5.2.0"
|
"vue-router": "^5.2.0",
|
||||||
|
"zod": "^4.4.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@tsconfig/node24": "^24.0.4",
|
"@tsconfig/node24": "^24.0.4",
|
||||||
|
|
|
||||||
|
|
@ -37,10 +37,12 @@ const router = createRouter({
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
router.beforeEach((to) => {
|
router.beforeEach(async (to) => {
|
||||||
const authStore = useAuthStore()
|
const authStore = useAuthStore()
|
||||||
|
|
||||||
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
|
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
|
||||||
|
const restoredSession = await authStore.fetchMe()
|
||||||
|
if (!restoredSession) {
|
||||||
return {
|
return {
|
||||||
name: 'login',
|
name: 'login',
|
||||||
query: {
|
query: {
|
||||||
|
|
@ -48,12 +50,20 @@ router.beforeEach((to) => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (to.meta.guestOnly && authStore.isAuthenticated) {
|
if (to.meta.guestOnly) {
|
||||||
|
let restoredSession = authStore.isAuthenticated
|
||||||
|
if (!restoredSession) {
|
||||||
|
restoredSession = await authStore.fetchMe()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (restoredSession) {
|
||||||
return {
|
return {
|
||||||
name: 'dashboard',
|
name: 'dashboard',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,67 @@
|
||||||
import { ref } from 'vue'
|
import { computed, ref } from 'vue'
|
||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import { API_BASE } from '@/utils/apiBase'
|
||||||
|
|
||||||
|
export const authUserSchema = z.object({
|
||||||
|
id: z.number().int().positive(),
|
||||||
|
email: z.string().email(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const meResponseSchema = z.object({
|
||||||
|
user: authUserSchema,
|
||||||
|
})
|
||||||
|
|
||||||
|
export type AuthUser = z.infer<typeof authUserSchema>
|
||||||
|
|
||||||
export const useAuthStore = defineStore('auth', () => {
|
export const useAuthStore = defineStore('auth', () => {
|
||||||
const isAuthenticated = ref(false)
|
const user = ref<AuthUser | null>(null)
|
||||||
|
const loading = ref(false)
|
||||||
|
const error = ref<string | null>(null)
|
||||||
|
const isAuthenticated = computed(() => user.value !== null)
|
||||||
|
|
||||||
|
async function fetchMe(): Promise<boolean> {
|
||||||
|
loading.value = true
|
||||||
|
error.value = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}/api/me`, {
|
||||||
|
method: 'GET',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/json',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if (response.status === 200) {
|
||||||
|
const responseBody: unknown = await response.json()
|
||||||
|
user.value = meResponseSchema.parse(responseBody).user
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
user.value = null
|
||||||
|
if (response.status !== 401) {
|
||||||
|
error.value = 'Unable to restore session'
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
} catch {
|
||||||
|
user.value = null
|
||||||
|
error.value = 'Unable to restore session'
|
||||||
|
|
||||||
|
return false
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
user,
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
|
fetchMe,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
17
frontend/website/src/utils/apiBase.ts
Normal file
17
frontend/website/src/utils/apiBase.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
function resolveApiBase(): string {
|
||||||
|
const configuredApiUrl = import.meta.env.VITE_API_URL
|
||||||
|
if (configuredApiUrl === '') {
|
||||||
|
throw new Error('VITE_API_URL must be configured')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!import.meta.env.DEV) {
|
||||||
|
return configuredApiUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
const apiUrl = new URL(configuredApiUrl)
|
||||||
|
apiUrl.hostname = window.location.hostname
|
||||||
|
|
||||||
|
return apiUrl.origin
|
||||||
|
}
|
||||||
|
|
||||||
|
export const API_BASE = resolveApiBase()
|
||||||
|
|
@ -1,12 +1,30 @@
|
||||||
|
import { existsSync, readFileSync } from 'node:fs'
|
||||||
import { fileURLToPath, URL } from 'node:url'
|
import { fileURLToPath, URL } from 'node:url'
|
||||||
|
|
||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
|
import type { ServerOptions } from 'vite'
|
||||||
import vue from '@vitejs/plugin-vue'
|
import vue from '@vitejs/plugin-vue'
|
||||||
import vueJsx from '@vitejs/plugin-vue-jsx'
|
import vueJsx from '@vitejs/plugin-vue-jsx'
|
||||||
import vueDevTools from 'vite-plugin-vue-devtools'
|
import vueDevTools from 'vite-plugin-vue-devtools'
|
||||||
|
|
||||||
|
const certificateKeyPath = fileURLToPath(
|
||||||
|
new URL('../../.cert/localhost-key.pem', import.meta.url),
|
||||||
|
)
|
||||||
|
const certificatePath = fileURLToPath(
|
||||||
|
new URL('../../.cert/localhost.pem', import.meta.url),
|
||||||
|
)
|
||||||
|
const serverOptions: ServerOptions = {}
|
||||||
|
|
||||||
|
if (existsSync(certificateKeyPath) && existsSync(certificatePath)) {
|
||||||
|
serverOptions.https = {
|
||||||
|
key: readFileSync(certificateKeyPath),
|
||||||
|
cert: readFileSync(certificatePath),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// https://vite.dev/config/
|
// https://vite.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
server: serverOptions,
|
||||||
plugins: [
|
plugins: [
|
||||||
vue(),
|
vue(),
|
||||||
vueJsx(),
|
vueJsx(),
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ export PGUSER="postgres"
|
||||||
export PGDATABASE="postgres"
|
export PGDATABASE="postgres"
|
||||||
|
|
||||||
DEV_APP_URL="https://localhost:$CADDY_PORT"
|
DEV_APP_URL="https://localhost:$CADDY_PORT"
|
||||||
|
DEV_FRONTEND_URL="https://localhost:$VITE_PORT"
|
||||||
DEV_DB_CONNECTION="pgsql"
|
DEV_DB_CONNECTION="pgsql"
|
||||||
DEV_DB_HOST="$PGHOST"
|
DEV_DB_HOST="$PGHOST"
|
||||||
DEV_DB_PORT="5432"
|
DEV_DB_PORT="5432"
|
||||||
|
|
@ -99,6 +100,7 @@ set_env_value() {
|
||||||
}
|
}
|
||||||
|
|
||||||
set_env_value APP_URL "$DEV_APP_URL"
|
set_env_value APP_URL "$DEV_APP_URL"
|
||||||
|
set_env_value FRONTEND_URL "$DEV_FRONTEND_URL"
|
||||||
set_env_value DB_CONNECTION "$DEV_DB_CONNECTION"
|
set_env_value DB_CONNECTION "$DEV_DB_CONNECTION"
|
||||||
set_env_value DB_HOST "$DEV_DB_HOST"
|
set_env_value DB_HOST "$DEV_DB_HOST"
|
||||||
set_env_value DB_PORT "$DEV_DB_PORT"
|
set_env_value DB_PORT "$DEV_DB_PORT"
|
||||||
|
|
@ -109,6 +111,13 @@ set_env_value MAIL_MAILER "$DEV_MAIL_MAILER"
|
||||||
set_env_value MAIL_HOST "$DEV_MAIL_HOST"
|
set_env_value MAIL_HOST "$DEV_MAIL_HOST"
|
||||||
set_env_value MAIL_PORT "$DEV_MAIL_PORT"
|
set_env_value MAIL_PORT "$DEV_MAIL_PORT"
|
||||||
|
|
||||||
|
FRONTEND_ENV_FILE="$REPO_ROOT/frontend/website/.env.local"
|
||||||
|
FRONTEND_API_URL="VITE_API_URL=$DEV_APP_URL"
|
||||||
|
if [ ! -f "$FRONTEND_ENV_FILE" ] \
|
||||||
|
|| [ "$(cat "$FRONTEND_ENV_FILE")" != "$FRONTEND_API_URL" ]; then
|
||||||
|
printf '%s\n' "$FRONTEND_API_URL" > "$FRONTEND_ENV_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
if [ ! -d "$REPO_ROOT/backend/vendor" ]; then
|
if [ ! -d "$REPO_ROOT/backend/vendor" ]; then
|
||||||
echo "[composer] installing backend dependencies"
|
echo "[composer] installing backend dependencies"
|
||||||
(cd "$REPO_ROOT/backend" && composer install)
|
(cd "$REPO_ROOT/backend" && composer install)
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,7 @@ processes:
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: ${VITE_PORT:-5173}
|
port: ${VITE_PORT:-5173}
|
||||||
path: /
|
path: /
|
||||||
|
scheme: https
|
||||||
initial_delay_seconds: 1
|
initial_delay_seconds: 1
|
||||||
period_seconds: 2
|
period_seconds: 2
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue