From 3c5b60e1aa77d65ce45816db0b06f3a0371c8699 Mon Sep 17 00:00:00 2001 From: Yanis Rigaudeau Date: Fri, 21 Oct 2022 00:02:00 +0200 Subject: [PATCH] login kept in memory --- api/src/config.ts | 1 + api/src/framework/express/server.ts | 15 ++++++-- api/src/framework/express/user.ts | 2 +- www/rollup.config.js | 3 +- www/src/App.svelte | 4 +++ www/src/components/NavBar.svelte | 55 +++++++++++++++++++++++++++++ www/src/components/navBar.svelte | 38 -------------------- www/src/functions/localStorage.ts | 19 ++++++++++ www/src/functions/request.ts | 8 +++-- www/src/functions/user.ts | 18 ++++++++-- www/src/main.ts | 1 - www/src/pages/Login.svelte | 21 +++++++---- www/src/pages/Profile.svelte | 23 ++++++++++++ www/src/pages/test3D.svelte | 2 +- www/src/store/user.ts | 6 +++- www/tsconfig.json | 1 - 16 files changed, 160 insertions(+), 57 deletions(-) create mode 100644 www/src/components/NavBar.svelte delete mode 100644 www/src/components/navBar.svelte create mode 100644 www/src/functions/localStorage.ts create mode 100644 www/src/pages/Profile.svelte diff --git a/api/src/config.ts b/api/src/config.ts index af48e6c..a697bda 100644 --- a/api/src/config.ts +++ b/api/src/config.ts @@ -2,6 +2,7 @@ import { readFileSync } from 'fs'; export type ServerConfig = { port: number; + origin: string[]; }; export type MongoConfig = { diff --git a/api/src/framework/express/server.ts b/api/src/framework/express/server.ts index 3f3a117..0043bd6 100644 --- a/api/src/framework/express/server.ts +++ b/api/src/framework/express/server.ts @@ -16,10 +16,21 @@ class Server { this.app = express(); this.app.use(express.json()); - this.app.use(cors()); + this.app.use( + cors({ + maxAge: 86400, + credentials: true, + origin: this.config.origin, + }), + ); this.app.use(RequestId()); this.app.use( - session({ secret: randomUUID(), cookie: { maxAge: 1000 * 3600 * 24 } }), + session({ + secret: randomUUID(), + cookie: { maxAge: 1000 * 3600 * 24 }, + resave: false, + saveUninitialized: false, + }), ); //this.app.use(checkPermissions()); this.app.use(getRoutes(services)); diff --git a/api/src/framework/express/user.ts b/api/src/framework/express/user.ts index d4988fc..3a6758b 100644 --- a/api/src/framework/express/user.ts +++ b/api/src/framework/express/user.ts @@ -27,7 +27,7 @@ function LogoutHandler(services: Services): RequestHandler { req.session.user = null; res.status(204).send(); } else { - next({ message: 'not logged in' }); + next({ status: 401, message: 'not logged in' }); } }; } diff --git a/www/rollup.config.js b/www/rollup.config.js index 123769b..9fc55ef 100644 --- a/www/rollup.config.js +++ b/www/rollup.config.js @@ -38,7 +38,7 @@ function serve() { export default { input: 'src/main.ts', output: { - sourcemap: true, + sourcemap: !production, format: 'iife', name: 'app', file: 'public/build/bundle.js', @@ -73,6 +73,7 @@ export default { }), commonjs(), replace({ + preventAssignment: true, 'process.env.APIURL': production ? '"/api"' : '"http://localhost:8000"', 'process.env.CORS': production ? 'false' : 'true', }), diff --git a/www/src/App.svelte b/www/src/App.svelte index 9800d4a..4802038 100644 --- a/www/src/App.svelte +++ b/www/src/App.svelte @@ -4,6 +4,7 @@ import Test3D from './pages/test3D.svelte'; import Login from './pages/Login.svelte'; + import Profile from './pages/Profile.svelte'; @@ -13,4 +14,7 @@ + + + diff --git a/www/src/components/NavBar.svelte b/www/src/components/NavBar.svelte new file mode 100644 index 0000000..e73798b --- /dev/null +++ b/www/src/components/NavBar.svelte @@ -0,0 +1,55 @@ + + + + + diff --git a/www/src/components/navBar.svelte b/www/src/components/navBar.svelte deleted file mode 100644 index 8d4e8b5..0000000 --- a/www/src/components/navBar.svelte +++ /dev/null @@ -1,38 +0,0 @@ - - - - - diff --git a/www/src/functions/localStorage.ts b/www/src/functions/localStorage.ts new file mode 100644 index 0000000..bd51083 --- /dev/null +++ b/www/src/functions/localStorage.ts @@ -0,0 +1,19 @@ +export function save(key: string, data: unknown) { + if (data) { + localStorage.setItem(key, JSON.stringify(data)); + } else { + remove(key); + } +} + +export function remove(key: string) { + localStorage.removeItem(key); +} + +export function read(key: string) { + const data = localStorage.getItem(key); + if (data) { + return JSON.parse(data) as T; + } + return null; +} diff --git a/www/src/functions/request.ts b/www/src/functions/request.ts index b8f0b57..6050926 100644 --- a/www/src/functions/request.ts +++ b/www/src/functions/request.ts @@ -8,7 +8,10 @@ export async function get(route: string): Promise { return request('GET', route); } -export async function post(route: string, data: unknown): Promise { +export async function post( + route: string, + data?: unknown, +): Promise { return request('POST', route, data); } @@ -25,12 +28,13 @@ async function request( headers: { 'content-type': 'application/json', }, + credentials: process.env.CORS ? 'include' : 'same-origin', mode: process.env.CORS ? 'cors' : 'same-origin', method: method, body: data ? JSON.stringify(data) : null, }); - if (response.ok) { + if (response.ok && response.status !== 204) { return response.json() as T; } return null; diff --git a/www/src/functions/user.ts b/www/src/functions/user.ts index fb4ab72..c63a6fc 100644 --- a/www/src/functions/user.ts +++ b/www/src/functions/user.ts @@ -1,8 +1,22 @@ -import type { LoginUserBody, UserInfo } from '@core'; -import { post } from './request'; +import type { CreateUserBody, LoginUserBody, UserInfo } from '@core'; +import { post, get } from './request'; import { currentUser } from '../store/user'; export async function login(raw: LoginUserBody) { const user = await post('/user/login', raw); currentUser.set(user); + return user; +} + +export async function logout() { + await post('/user/logout'); + currentUser.set(null); +} + +export async function read(uuid: string) { + const user = await get(`/user/read/${uuid}`); +} + +export async function create(raw: CreateUserBody) { + const user = await post('/user/create', raw); } diff --git a/www/src/main.ts b/www/src/main.ts index b849f93..50a0732 100644 --- a/www/src/main.ts +++ b/www/src/main.ts @@ -2,7 +2,6 @@ import App from './App.svelte'; const app = new App({ target: document.body, - props: {}, }); export default app; diff --git a/www/src/pages/Login.svelte b/www/src/pages/Login.svelte index 77b7deb..a918172 100644 --- a/www/src/pages/Login.svelte +++ b/www/src/pages/Login.svelte @@ -1,14 +1,23 @@
@@ -16,25 +25,23 @@ - loginHandler()}>Sign in - {$currentUser?.uuid} - {$currentUser?.username} - {$currentUser?.role}
diff --git a/www/src/pages/Profile.svelte b/www/src/pages/Profile.svelte new file mode 100644 index 0000000..0bfe38b --- /dev/null +++ b/www/src/pages/Profile.svelte @@ -0,0 +1,23 @@ + + + +{$currentUser?.uuid} +{$currentUser?.username} +{$currentUser?.role} + + + diff --git a/www/src/pages/test3D.svelte b/www/src/pages/test3D.svelte index fa89024..a617860 100644 --- a/www/src/pages/test3D.svelte +++ b/www/src/pages/test3D.svelte @@ -1,5 +1,5 @@