Files
Lazuli/src/lib/server/users.ts

54 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-01-23 01:21:41 -05:00
import Database from 'better-sqlite3'
import Services from '$lib/services.json'
import { generateUUID } from '$lib/utils'
const db = new Database('./src/lib/server/users.db', { verbose: console.info })
db.pragma('foreign_keys = ON')
const initUsersTable = 'CREATE TABLE IF NOT EXISTS Users(id VARCHAR(36) PRIMARY KEY, username VARCHAR(30) UNIQUE NOT NULL, password VARCHAR(72) NOT NULL)'
2024-01-23 12:05:33 -05:00
const initConnectionsTable =
'CREATE TABLE IF NOT EXISTS Connections(id VARCHAR(36) PRIMARY KEY, userId VARCHAR(36) NOT NULL, serviceType VARCHAR(64) NOT NULL, serviceUser TEXT NOT NULL, serviceUrl TEXT NOT NULL, accessToken TEXT NOT NULL, refreshToken TEXT, expiry INTEGER, FOREIGN KEY(userId) REFERENCES Users(id))'
2024-01-23 01:21:41 -05:00
db.exec(initUsersTable)
2024-01-23 12:05:33 -05:00
db.exec(initConnectionsTable)
2024-01-23 01:21:41 -05:00
export interface User {
id: string
username: string
password: string
}
2024-01-23 12:05:33 -05:00
export interface Service {
url: string
serviceType: 'jellyfin' | 'youtube-music'
displayName: string
icon: string
primaryColor: string
}
export interface Connection {
id: string
user: User
service: Service
serviceUser: string
accessToken: string
refreshToken: string | null
expiry: number | null
}
2024-01-23 01:21:41 -05:00
export class Users {
static addUser = (username: string, hashedPassword: string): User => {
const userId = generateUUID()
db.prepare('INSERT INTO Users(id, username, password) VALUES(?, ?, ?)').run(userId, username, hashedPassword)
return this.getUser(userId)
}
static getUser = (id: string): User => {
return db.prepare('SELECT * FROM Users WHERE id = ?').get(id) as User
}
}
2024-01-23 12:05:33 -05:00
export class Connections {
// static getConnection = (id: string): Connection => {
// const connectionRow = db.prepare('SELECT * FROM Connections WHERE id = ?').get(id)
// }
}