From 2dfe47b16e1946afc0a611c90b6fc32f7abb594f Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Wed, 11 Jan 2023 12:29:37 +0100 Subject: [PATCH] refactor: added support for base url using a shared function --- client/App.tsx | 3 ++- client/api.tsx | 6 +----- client/components/Header.tsx | 10 +++------- client/hooks.tsx | 2 +- client/hooks/useCurrentUser.tsx | 4 +++- client/pages/AdminPage.tsx | 6 ++++-- client/pages/LoginPage.tsx | 7 +++++-- client/pages/ProblemPage.tsx | 3 ++- client/pages/ProfilePage.tsx | 2 +- server/auth.ts | 5 +++-- shared/utils.ts | 16 ++++++++++++++++ 11 files changed, 41 insertions(+), 23 deletions(-) diff --git a/client/App.tsx b/client/App.tsx index 25b1334..7eb0b5e 100644 --- a/client/App.tsx +++ b/client/App.tsx @@ -1,7 +1,8 @@ import Router from 'preact-router' import { route } from 'preact-router' import { useContext, useEffect } from 'preact/hooks' -import { prependBaseUrl } from './api' +import { prependBaseUrl } from '../shared/utils' + import { ServerContext } from './hooks' import { UserProvider } from './hooks/useCurrentUser' diff --git a/client/api.tsx b/client/api.tsx index 2641cde..682a551 100644 --- a/client/api.tsx +++ b/client/api.tsx @@ -1,8 +1,4 @@ -// TODO: move to `shared/` and distinguish client and server (fix imports) - -export function prependBaseUrl(route: string) { - return import.meta.env.BASE_URL + (route.startsWith('/') ? route.substring(1) : route) -} +import { prependBaseUrl } from '../shared/utils' export const server = { async get(url: string) { diff --git a/client/components/Header.tsx b/client/components/Header.tsx index 1ee05f7..e65af02 100644 --- a/client/components/Header.tsx +++ b/client/components/Header.tsx @@ -1,13 +1,9 @@ import { Link } from 'preact-router/match' + import { isAdministrator } from '../../shared/model' -import { prependBaseUrl } from '../api' -import { useCurrentUser } from '../hooks/useCurrentUser' +import { prependBaseUrl } from '../../shared/utils' -// const ROLE_LABEL: Record = { -// ['admin']: 'Admin', -// ['moderator']: 'Moderatore', -// ['student']: 'Studente', -// } +import { useCurrentUser } from '../hooks/useCurrentUser' export const Header = ({}) => { const [user] = useCurrentUser() diff --git a/client/hooks.tsx b/client/hooks.tsx index 60c3fc6..ae4ea83 100644 --- a/client/hooks.tsx +++ b/client/hooks.tsx @@ -1,7 +1,7 @@ import { StateUpdater, useEffect, useState } from 'preact/hooks' import { createContext } from 'preact' -import { prependBaseUrl } from './api' +import { prependBaseUrl } from '../shared/utils' type Metadata = { title?: string diff --git a/client/hooks/useCurrentUser.tsx b/client/hooks/useCurrentUser.tsx index 30f244e..e331bd2 100644 --- a/client/hooks/useCurrentUser.tsx +++ b/client/hooks/useCurrentUser.tsx @@ -1,7 +1,9 @@ import { ComponentChildren, createContext, JSX } from 'preact' import { StateUpdater, useContext, useEffect, useState } from 'preact/hooks' import { User } from '../../shared/model' -import { prependBaseUrl, server } from '../api' + +import { server } from '../api' +import { prependBaseUrl } from '../../shared/utils' type UserContextValue = { ready: boolean diff --git a/client/pages/AdminPage.tsx b/client/pages/AdminPage.tsx index 70441c1..ca6217d 100644 --- a/client/pages/AdminPage.tsx +++ b/client/pages/AdminPage.tsx @@ -1,8 +1,10 @@ import { route } from 'preact-router' import { useState } from 'preact/hooks' import { isAdministrator, Solution as SolutionModel, SolutionId, User as UserModel } from '../../shared/model' -import { sortByStringKey } from '../../shared/utils' -import { prependBaseUrl, server } from '../api' + +import { server } from '../api' +import { prependBaseUrl, sortByStringKey } from '../../shared/utils' + import { Header } from '../components/Header' import { MarkdownEditor } from '../components/MarkdownEditor' import { Select } from '../components/Select' diff --git a/client/pages/LoginPage.tsx b/client/pages/LoginPage.tsx index 3ec88b1..ec75b87 100644 --- a/client/pages/LoginPage.tsx +++ b/client/pages/LoginPage.tsx @@ -1,4 +1,5 @@ -import { prependBaseUrl } from '../api.js' +import { prependBaseUrl } from '../../shared/utils' + import { Header } from '../components/Header.jsx' export const LoginPage = () => { @@ -7,7 +8,9 @@ export const LoginPage = () => {
diff --git a/client/pages/ProblemPage.tsx b/client/pages/ProblemPage.tsx index fdc1201..d47db6a 100644 --- a/client/pages/ProblemPage.tsx +++ b/client/pages/ProblemPage.tsx @@ -1,7 +1,8 @@ import { route } from 'preact-router' import { useContext, useEffect, useState } from 'preact/hooks' import { isAdministrator, Problem as ProblemModel, Solution as SolutionModel } from '../../shared/model' -import { prependBaseUrl, server } from '../api' +import { prependBaseUrl } from '../../shared/utils' +import { server } from '../api' import { Header } from '../components/Header' import { MarkdownEditor } from '../components/MarkdownEditor' import { Problem } from '../components/Problem' diff --git a/client/pages/ProfilePage.tsx b/client/pages/ProfilePage.tsx index 7acbcc5..ea310e0 100644 --- a/client/pages/ProfilePage.tsx +++ b/client/pages/ProfilePage.tsx @@ -1,7 +1,7 @@ import { route } from 'preact-router' import { isAdministrator, Solution as SolutionModel, User } from '../../shared/model' import { sortByStringKey } from '../../shared/utils' -import { prependBaseUrl } from '../api' +import { prependBaseUrl } from '../../shared/utils' import { Header } from '../components/Header' import { Solution } from '../components/Solution' import { useResource } from '../hooks' diff --git a/server/auth.ts b/server/auth.ts index 3eda8fd..fd4f815 100644 --- a/server/auth.ts +++ b/server/auth.ts @@ -4,6 +4,7 @@ import fetch from 'node-fetch' import { DatabaseConnection, getUser, createUser } from './db/database' import { UserId } from '../shared/model' import { SessionService } from './routes' +import { prependBaseUrl } from '../shared/utils' export function setupOAuth(r: Router, db: DatabaseConnection, sessions: SessionService) { const config = { @@ -80,10 +81,10 @@ export function setupOAuth(r: Router, db: DatabaseConnection, sessions: SessionS res.cookie('sid', sessions.createSession(authUser.id), { maxAge: 1000 * 60 * 60 * 24 * 7 }) - return res.status(200).redirect('/') + return res.status(200).redirect(prependBaseUrl('/')) } catch (error) { console.error('Access Token Error', error.message) - return res.status(500).redirect(`/error?message=${encodeURIComponent('Autenticazione fallita')}`) + return res.status(500).redirect(prependBaseUrl(`/error?message=${encodeURIComponent('Autenticazione fallita')}`)) } }) } diff --git a/shared/utils.ts b/shared/utils.ts index cd97779..56a0aa9 100644 --- a/shared/utils.ts +++ b/shared/utils.ts @@ -28,3 +28,19 @@ function sortByKey(compareFn: (a: K, b: K) => number, items: T[], keyFn: ( sortedList.sort((a, b) => order * compareFn(keyFn(a), keyFn(b))) return sortedList } + +// +// Base Url +// + +export function prependBaseUrl(route: string) { + // @ts-ignore + if (typeof window === 'undefined') { + // NodeJS + return process.env.BASE_URL + (route.startsWith('/') ? route.substring(1) : route) + } else { + // Browser + // @ts-ignore + return import.meta.env.BASE_URL + (route.startsWith('/') ? route.substring(1) : route) + } +}