Skip to content

Commit

Permalink
create account endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzYanick committed Jan 11, 2024
1 parent 0cf4a85 commit 160bc2a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import express from 'express'

import authMiddleware from './middleware/auth'
import tokenMiddleware from './middleware/token'
import isAdmin from './middleware/isAdmin'

import * as auth from './routes/auth'
import * as client from './routes/client'
import * as tunnel from './routes/tunnel'
import * as clientOnly from './routes/clientOnly'
import * as admin from './routes/admin'

import {
checkForRathole,
Expand Down Expand Up @@ -58,6 +60,13 @@ app.delete('/api/v1/tunnel/:id', authMiddleware, tunnel.remove)

app.get('/api/v1/clientOnly/generate', tokenMiddleware, clientOnly.generate)

app.post(
'/api/v1/admin/createAccount',
authMiddleware,
isAdmin,
admin.createAccount
)

app.use(express.static('dist'))
app.get('*', (_, res) => {
res.sendFile('dist/index.html', { root: '.' })
Expand Down
12 changes: 12 additions & 0 deletions src/backend/middleware/isAdmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Request, Response, NextFunction } from 'express'

const isAdmin = (req: Request, res: Response, next: NextFunction) => {
const user = req.user
if (user.adm) {
next()
} else {
res.status(403).json({ message: 'Forbidden' })
}
}

export default isAdmin
14 changes: 14 additions & 0 deletions src/backend/routes/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Request, Response } from 'express'

import db from '../db'

export const createAccount = (req: Request, res: Response) => {
const { username, password } = req.body
const user = db.users.find(username)
if (user) {
res.status(400).json({ message: 'Username already exists' })
} else {
db.users.create(username, password)
res.json({ message: 'Account created' })
}
}

0 comments on commit 160bc2a

Please sign in to comment.