Login and such

This commit is contained in:
Jesper 2024-09-16 00:18:42 +02:00
parent 97487f6a1f
commit 349ad6356a
9 changed files with 126 additions and 0 deletions

7
backend/src/Database.ts Normal file
View File

@ -0,0 +1,7 @@
import { Database } from "bun:sqlite";
const db = new Database("../db.db", {strict: true})
db.exec("PRAGMA journal_mode = WAL;");
export default db

15
backend/src/README.md Normal file
View File

@ -0,0 +1,15 @@
# corpopasswordmanager
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```
This project was created using `bun init` in bun v1.1.22. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

View File

@ -0,0 +1,26 @@
import { Elysia } from 'elysia'
import db from '../Database'
import { User } from './index.ts'
import { authenticator } from 'otplib';
const user = new User();
const userController = new Elysia()
.post('/login', async ({ body }) => {
const msg = db.query(`select id, password, otp from users WHERE username = ?;`)
.get(body.username)
console.log(msg)
if(msg == null) return new Response("Invalid username or password", { status: 401 })
const validPassword = await Bun.password.verify(body.password, msg.password)
if (!validPassword) return new Response("Invalid username or password", { status: 401 })
// if(!authenticator.check(body.otp, msg.otp)) return new Response("Invalid OTP code", { status: 401 })
return await user.createToken(msg.id)
})
.post('/register', async({body}) => {
return await Bun.password.hash(body.password);
})
.get('/otp', () => {
return authenticator.generateSecret();
})
export default userController

14
backend/src/User/index.ts Normal file
View File

@ -0,0 +1,14 @@
import { nanoid } from 'nanoid';
import db from '../Database'
export class User {
async createToken(userId: number) {
db.query(`DELETE FROM tokens WHERE user_id = ?;`).run(userId);
const token = nanoid(24);
const dbRes = db.query(`INSERT into tokens (user_id, token) VALUES (?, ?);`)
.run(userId, token)
console.log(token);
return token;
}
}

View File

@ -0,0 +1,10 @@
import { Elysia } from 'elysia'
const adminController = new Elysia()
.get('/', () => "admin endpoint")
.post('/register', async({body}) => {
return await Bun.password.hash(body.password);
})
export default adminController

BIN
backend/src/bun.lockb Executable file

Binary file not shown.

9
backend/src/index.ts Normal file
View File

@ -0,0 +1,9 @@
import {Elysia} from 'elysia'
import userController from './User/Controller'
import adminController from './admin/Controller'
const app = new Elysia()
.get('/', () => "Hello")
.group('/user', (app) => app.use(userController))
.group('/admin', (app) => app.use(adminController))
.listen(3000)

18
backend/src/package.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "corpopasswordmanager",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"elysia": "^1.1.12",
"kysely": "^0.27.4",
"nanoid": "^5.0.7",
"otplib": "^12.0.1",
"qrcode": "^1.5.4"
}
}

27
backend/src/tsconfig.json Normal file
View File

@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}