Type api response
This commit is contained in:
parent
23c27da17e
commit
49413c7ef7
|
@ -1,6 +1,7 @@
|
||||||
import { Request, Response, Router } from 'express'
|
import { Request, Response, Router } from 'express'
|
||||||
import { AuthenticationService } from '../services/AuthenticationService.js'
|
import { AuthenticationService } from '../services/AuthenticationService.js'
|
||||||
import { InvalidCredentialsError } from 'ldapts'
|
import { InvalidCredentialsError } from 'ldapts'
|
||||||
|
import { User } from '@prisma/client'
|
||||||
|
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
|
@ -24,8 +25,13 @@ class LoginController {
|
||||||
res.status(401).json({ error: error.message })
|
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.post('/', LoginController.login)
|
||||||
|
router.get('/me', LoginController.me)
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|
|
@ -25,10 +25,6 @@ app.use('/api/printer', PrinterRouter)
|
||||||
app.use('/api/printer-status', PrinterStatusRouter)
|
app.use('/api/printer-status', PrinterStatusRouter)
|
||||||
app.use('/api/discovery', PrinterDiscoveryRouter)
|
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.use('/', express.static('public'))
|
||||||
|
|
||||||
app.get('*', (req, res) => {
|
app.get('*', (req, res) => {
|
||||||
|
|
|
@ -2,6 +2,7 @@ import jwt from 'jsonwebtoken'
|
||||||
import { prisma } from '../prisma.js'
|
import { prisma } from '../prisma.js'
|
||||||
import { LdapService } from './LdapService.js'
|
import { LdapService } from './LdapService.js'
|
||||||
import { UserService } from './UserService.js'
|
import { UserService } from './UserService.js'
|
||||||
|
import { User } from '@prisma/client'
|
||||||
|
|
||||||
const JWT_SECRET = process.env.JWT_SECRET || 'secret'
|
const JWT_SECRET = process.env.JWT_SECRET || 'secret'
|
||||||
|
|
||||||
|
@ -16,13 +17,13 @@ export class AuthenticationService {
|
||||||
await UserService.importUser(username)
|
await UserService.importUser(username)
|
||||||
|
|
||||||
const token = jwt.sign({ username }, JWT_SECRET, {
|
const token = jwt.sign({ username }, JWT_SECRET, {
|
||||||
expiresIn: '2 days'
|
expiresIn: process.env.JWT_EXPIRES_IN || '30 days'
|
||||||
})
|
})
|
||||||
|
|
||||||
return `Bearer ${token}`
|
return `Bearer ${token}`
|
||||||
}
|
}
|
||||||
|
|
||||||
static async jwtAuth(token: string) {
|
static async jwtAuth(token: string): Promise<User> {
|
||||||
try {
|
try {
|
||||||
const { username } = jwt.verify(token, JWT_SECRET) as { username: string }
|
const { username } = jwt.verify(token, JWT_SECRET) as { username: string }
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const BASE_URL = process.env.BASE_URL || "http://localhost:8000/api/";
|
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");
|
const token = localStorage.getItem("token");
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<v-app-bar>
|
<v-app-bar>
|
||||||
<v-app-bar-nav-icon @click="drawer = !drawer"></v-app-bar-nav-icon>
|
<v-app-bar-nav-icon @click="drawer = !drawer"></v-app-bar-nav-icon>
|
||||||
<v-avatar rounded>
|
<v-avatar rounded>
|
||||||
<v-img :src="me?.thumbnailPhoto" />
|
<v-img :src="me?.thumbnailPhoto || undefined" />
|
||||||
</v-avatar>
|
</v-avatar>
|
||||||
<v-toolbar-title>Application</v-toolbar-title>
|
<v-toolbar-title>Application</v-toolbar-title>
|
||||||
</v-app-bar>
|
</v-app-bar>
|
||||||
|
|
|
@ -2,10 +2,11 @@
|
||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import { api } from "@/api";
|
import { api } from "@/api";
|
||||||
import { useRouter } from "vue-router";
|
import { useRouter } from "vue-router";
|
||||||
|
import { User } from "@prisma/client";
|
||||||
|
|
||||||
export const useAppStore = defineStore("app", {
|
export const useAppStore = defineStore("app", {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
me: null,
|
me: null as User | null,
|
||||||
printers: [],
|
printers: [],
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
@ -18,7 +19,7 @@ export const useAppStore = defineStore("app", {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.me = await api("me", { method: "GET" });
|
this.me = await api<User>("login/me", { method: "GET" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
router.push({ name: "Login" });
|
router.push({ name: "Login" });
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@ async function login() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
const data = await api("login", {
|
const data = await api<{ token: string }>("login", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
username: username.value,
|
username: username.value,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user