From 49413c7ef78239402a4e0cb9e4eb18190fabe4b6 Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Wed, 28 Jun 2023 08:01:43 -0400 Subject: [PATCH] Type api response --- src/controllers/LoginController.ts | 6 ++++++ src/server.ts | 4 ---- src/services/AuthenticationService.ts | 5 +++-- web/src/api.ts | 2 +- web/src/layouts/default/View.vue | 2 +- web/src/store/app.ts | 5 +++-- web/src/views/Login.vue | 2 +- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/controllers/LoginController.ts b/src/controllers/LoginController.ts index e776256..262e2d1 100644 --- a/src/controllers/LoginController.ts +++ b/src/controllers/LoginController.ts @@ -1,6 +1,7 @@ import { Request, Response, Router } from 'express' import { AuthenticationService } from '../services/AuthenticationService.js' import { InvalidCredentialsError } from 'ldapts' +import { User } from '@prisma/client' const router = Router() @@ -24,8 +25,13 @@ class LoginController { res.status(401).json({ error: error.message }) } } + + static async me(req: Request, res: Response) { + res.json(res.locals.user as User) + } } router.post('/', LoginController.login) +router.get('/me', LoginController.me) export default router diff --git a/src/server.ts b/src/server.ts index ae62a4b..392611c 100644 --- a/src/server.ts +++ b/src/server.ts @@ -25,10 +25,6 @@ app.use('/api/printer', PrinterRouter) app.use('/api/printer-status', PrinterStatusRouter) app.use('/api/discovery', PrinterDiscoveryRouter) -app.get('/api/me', authMiddleware, async (req: Request, res: Response) => - res.json(res.locals.user) -) - app.use('/', express.static('public')) app.get('*', (req, res) => { diff --git a/src/services/AuthenticationService.ts b/src/services/AuthenticationService.ts index b51f63a..a111f4a 100644 --- a/src/services/AuthenticationService.ts +++ b/src/services/AuthenticationService.ts @@ -2,6 +2,7 @@ import jwt from 'jsonwebtoken' import { prisma } from '../prisma.js' import { LdapService } from './LdapService.js' import { UserService } from './UserService.js' +import { User } from '@prisma/client' const JWT_SECRET = process.env.JWT_SECRET || 'secret' @@ -16,13 +17,13 @@ export class AuthenticationService { await UserService.importUser(username) const token = jwt.sign({ username }, JWT_SECRET, { - expiresIn: '2 days' + expiresIn: process.env.JWT_EXPIRES_IN || '30 days' }) return `Bearer ${token}` } - static async jwtAuth(token: string) { + static async jwtAuth(token: string): Promise { try { const { username } = jwt.verify(token, JWT_SECRET) as { username: string } diff --git a/web/src/api.ts b/web/src/api.ts index 983f02b..66e94f0 100644 --- a/web/src/api.ts +++ b/web/src/api.ts @@ -1,6 +1,6 @@ const BASE_URL = process.env.BASE_URL || "http://localhost:8000/api/"; -export async function api(endpoint: string, options: any) { +export async function api(endpoint: string, options: any): Promise { const token = localStorage.getItem("token"); if (token) { diff --git a/web/src/layouts/default/View.vue b/web/src/layouts/default/View.vue index 55188e8..817efae 100644 --- a/web/src/layouts/default/View.vue +++ b/web/src/layouts/default/View.vue @@ -5,7 +5,7 @@ - + Application diff --git a/web/src/store/app.ts b/web/src/store/app.ts index c5e8396..8e95794 100644 --- a/web/src/store/app.ts +++ b/web/src/store/app.ts @@ -2,10 +2,11 @@ import { defineStore } from "pinia"; import { api } from "@/api"; import { useRouter } from "vue-router"; +import { User } from "@prisma/client"; export const useAppStore = defineStore("app", { state: () => ({ - me: null, + me: null as User | null, printers: [], }), @@ -18,7 +19,7 @@ export const useAppStore = defineStore("app", { const router = useRouter(); try { - this.me = await api("me", { method: "GET" }); + this.me = await api("login/me", { method: "GET" }); } catch (error) { router.push({ name: "Login" }); } diff --git a/web/src/views/Login.vue b/web/src/views/Login.vue index 01170ec..04bb08d 100644 --- a/web/src/views/Login.vue +++ b/web/src/views/Login.vue @@ -80,7 +80,7 @@ async function login() { try { loading.value = true; - const data = await api("login", { + const data = await api<{ token: string }>("login", { method: "POST", body: JSON.stringify({ username: username.value,