Type api response

This commit is contained in:
Douglas Barone 2023-06-28 08:01:43 -04:00
parent 23c27da17e
commit 49413c7ef7
7 changed files with 15 additions and 11 deletions

View File

@ -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

View File

@ -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) => {

View File

@ -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<User> {
try {
const { username } = jwt.verify(token, JWT_SECRET) as { username: string }

View File

@ -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<T>(endpoint: string, options: any): Promise<T> {
const token = localStorage.getItem("token");
if (token) {

View File

@ -5,7 +5,7 @@
<v-app-bar>
<v-app-bar-nav-icon @click="drawer = !drawer"></v-app-bar-nav-icon>
<v-avatar rounded>
<v-img :src="me?.thumbnailPhoto" />
<v-img :src="me?.thumbnailPhoto || undefined" />
</v-avatar>
<v-toolbar-title>Application</v-toolbar-title>
</v-app-bar>

View File

@ -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<User>("login/me", { method: "GET" });
} catch (error) {
router.push({ name: "Login" });
}

View File

@ -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,