feat(csms): add deployment script and database schema for user authentication
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import pkg from '../../../package.json' with { type: 'json' }
|
||||
import 'dotenv/config'
|
||||
import { serve } from '@hono/node-server'
|
||||
import { Hono } from 'hono'
|
||||
import { createNodeWebSocket } from '@hono/node-ws'
|
||||
@@ -6,25 +6,48 @@ import { getConnInfo } from '@hono/node-server/conninfo'
|
||||
import { cors } from 'hono/cors'
|
||||
import { logger } from 'hono/logger'
|
||||
import { showRoutes } from 'hono/dev'
|
||||
import { auth } from './lib/auth.ts'
|
||||
|
||||
const app = new Hono()
|
||||
const app = new Hono<{
|
||||
Variables: {
|
||||
user: typeof auth.$Infer.Session.user | null
|
||||
session: typeof auth.$Infer.Session.session | null
|
||||
}
|
||||
}>()
|
||||
|
||||
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app })
|
||||
|
||||
app.use(logger())
|
||||
|
||||
app.use('/ocpp', cors({
|
||||
origin: '*',
|
||||
allowMethods: ['GET', 'POST', 'OPTIONS'],
|
||||
allowHeaders: ['Content-Type', 'Authorization'],
|
||||
exposeHeaders: ['Content-Length'],
|
||||
credentials: true,
|
||||
}))
|
||||
app.use('*', async (c, next) => {
|
||||
const session = await auth.api.getSession({ headers: c.req.raw.headers })
|
||||
if (!session) {
|
||||
c.set('user', null)
|
||||
c.set('session', null)
|
||||
await next()
|
||||
return
|
||||
}
|
||||
c.set('user', session.user)
|
||||
c.set('session', session.session)
|
||||
await next()
|
||||
})
|
||||
|
||||
app.use(
|
||||
'/api/auth/*',
|
||||
cors({
|
||||
origin: '*',
|
||||
allowMethods: ['GET', 'POST', 'OPTIONS'],
|
||||
allowHeaders: ['Content-Type', 'Authorization'],
|
||||
exposeHeaders: ['Content-Length'],
|
||||
credentials: true,
|
||||
}),
|
||||
)
|
||||
|
||||
app.on(['POST', 'GET'], '/api/auth/*', (c) => auth.handler(c.req.raw))
|
||||
|
||||
app.get('/', (c) => {
|
||||
return c.json({
|
||||
platform: 'Helios CSMS',
|
||||
version: pkg.version,
|
||||
message: 'ok',
|
||||
})
|
||||
})
|
||||
@@ -35,7 +58,9 @@ app.get(
|
||||
return {
|
||||
onOpen(evt, ws) {
|
||||
const connInfo = getConnInfo(c)
|
||||
console.log(`New connection from ${connInfo.remote.address}:${connInfo.remote.port}`)
|
||||
console.log(
|
||||
`New connection from ${connInfo.remote.address}:${connInfo.remote.port}`,
|
||||
)
|
||||
},
|
||||
onMessage(evt, ws) {
|
||||
console.log(`Received message: ${evt.data}`)
|
||||
|
||||
Reference in New Issue
Block a user