62 lines
1.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Elysia } from 'elysia'
import { jwt } from '@elysiajs/jwt'
import { cookie } from '@elysiajs/cookie'
import { staticPlugin } from '@elysiajs/static'
const app = new Elysia()
.use(cookie())
.use(jwt({
name: 'jwt',
secret: 'your-secret-key' // В продакшене используйте безопасный секретный ключ
}))
.use(staticPlugin({
prefix: '/potree',
assets: '../build/potree'
}))
// Middleware для проверки авторизации
.derive(async ({ jwt, cookie, set }) => {
const token = cookie.auth
if (!token) {
set.status = 401
return { authorized: false }
}
const profile = await jwt.verify(token)
if (!profile) {
set.status = 401
return { authorized: false }
}
return { authorized: true, profile }
})
// Маршрут для авторизации
.post('/login', async ({ body, jwt, setCookie }) => {
const { username, password } = body as { username: string, password: string }
// Здесь должна быть ваша логика проверки учетных данных
if (username === 'admin' && password === 'password') {
const token = await jwt.sign({ username })
setCookie('auth', token, {
httpOnly: true,
maxAge: 7 * 86400 // 7 дней
})
return { success: true }
}
return { success: false, message: 'Неверные учетные данные' }
})
// Защищенный маршрут для Potree
.get('/viewer/*', async ({ authorized, set }) => {
if (!authorized) {
set.status = 401
return { error: 'Требуется авторизация' }
}
return { success: true }
})
.listen(3000)
console.log(`🚀 Сервер запущен на http://localhost:${app.server?.port}`)