|
|
|
@ -40,11 +40,16 @@ import { initialDatabaseValue } from './db/example-data'
|
|
|
|
|
import { validateObjectKeys } from '../shared/utils'
|
|
|
|
|
import { setupOauth } from './auth'
|
|
|
|
|
|
|
|
|
|
export async function createApiRouter() {
|
|
|
|
|
type SessionId = Opaque<string, string, 'session'>
|
|
|
|
|
type SessionId = Opaque<string, string, 'session'>
|
|
|
|
|
|
|
|
|
|
export interface SessionService {
|
|
|
|
|
createSession(userId: UserId): SessionId
|
|
|
|
|
getUserForSession(sid: SessionId): UserId | null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createApiRouter() {
|
|
|
|
|
const sessionStore: Record<SessionId, UserId> = {}
|
|
|
|
|
const sessions = {
|
|
|
|
|
const sessions: SessionService = {
|
|
|
|
|
createSession(userId: UserId) {
|
|
|
|
|
const sid = crypto.randomBytes(10).toString('hex') as SessionId
|
|
|
|
|
sessionStore[sid] = userId
|
|
|
|
@ -73,7 +78,7 @@ export async function createApiRouter() {
|
|
|
|
|
|
|
|
|
|
const authRouter: Router = express.Router()
|
|
|
|
|
|
|
|
|
|
setupOauth(authRouter)
|
|
|
|
|
setupOauth(authRouter, db, sessions)
|
|
|
|
|
r.use('/auth', authRouter)
|
|
|
|
|
|
|
|
|
|
r.get('/api/status', (req, res) => {
|
|
|
|
@ -84,18 +89,18 @@ export async function createApiRouter() {
|
|
|
|
|
res.json(await getRequestUser(req))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.post('/api/login', async (req, res) => {
|
|
|
|
|
const { id, name } = req.body
|
|
|
|
|
// r.post('/api/login', async (req, res) => {
|
|
|
|
|
// const { id, name } = req.body
|
|
|
|
|
|
|
|
|
|
const user = await getUser(db, id)
|
|
|
|
|
if (!user) {
|
|
|
|
|
res.sendStatus(StatusCodes.FORBIDDEN)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// const user = await getUser(db, id)
|
|
|
|
|
// if (!user) {
|
|
|
|
|
// res.sendStatus(StatusCodes.FORBIDDEN)
|
|
|
|
|
// return
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
res.cookie('sid', sessions.createSession(id), { maxAge: 1000 * 60 * 60 * 24 * 7 })
|
|
|
|
|
res.json({ status: 'ok' })
|
|
|
|
|
})
|
|
|
|
|
// res.cookie('sid', sessions.createSession(id), { maxAge: 1000 * 60 * 60 * 24 * 7 })
|
|
|
|
|
// res.json({ status: 'ok' })
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
r.post('/api/logout', (req, res) => {
|
|
|
|
|
res.cookie('sid', '', { expires: new Date() })
|
|
|
|
|