example simple registration/login logic with basic UI

This commit is contained in:
Oleksandr Honcharov
2025-04-17 18:30:09 +03:00
parent 52d611cf4e
commit 37519bbdaf
24 changed files with 333 additions and 420 deletions
+6
View File
@@ -0,0 +1,6 @@
import 'dotenv/config'
import { drizzle } from 'drizzle-orm/node-postgres'
const connUrl = `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@localhost:5432/${process.env.POSTGRES_DB}`
export const db = drizzle(connUrl)
+9
View File
@@ -0,0 +1,9 @@
import { integer, pgTable, varchar } from 'drizzle-orm/pg-core'
export const usersTable = pgTable('users', {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
socialClub: varchar({ length: 255 }).notNull().unique(),
email: varchar({ length: 255 }).notNull().unique(),
login: varchar({ length: 255 }).notNull().unique(),
password: varchar({ length: 255 }).notNull(),
})
+84 -2
View File
@@ -1,3 +1,85 @@
import { checkLicense, doSensitiveOperation } from './x86_64-pc-windows-msvc'
import { fw } from '@entityseven/rage-fw-server'
import { AuthEvent } from '@shared/events'
console.log(doSensitiveOperation('test'))
import md5 from 'md5'
import { eq, or } from 'drizzle-orm'
import { usersTable } from './db/schema'
import { db } from './db'
fw.event.register(AuthEvent.REGISTER, async (player, [data]) => {
const { login, email, password } = data
const normalizedEmail = email.toLowerCase()
const normalizedLogin = login.toLowerCase()
try {
const existingUser = await db
.select()
.from(usersTable)
.where(
or(
eq(usersTable.login, normalizedLogin),
eq(usersTable.email, normalizedEmail),
eq(usersTable.socialClub, player.socialClub),
),
)
if (existingUser.length > 0) {
return 'User already exists'
}
const user: typeof usersTable.$inferInsert = {
socialClub: player.socialClub,
password: md5(password), // we can use md5 for simplicity, but in production you should use more secure hashing algorithms
email: normalizedEmail,
login: normalizedLogin,
}
await db.insert(usersTable).values(user)
} catch (e) {
console.log(e)
return 'Something went wrong'
}
console.log('User registered:', player.socialClub)
return 'User created successfully'
})
fw.event.register(AuthEvent.LOGIN, async (player, [data]) => {
const { login, password } = data
const normalizedLogin = login.toLowerCase()
console.log('Login data', data)
try {
const users = await db
.select()
.from(usersTable)
.where(eq(usersTable.login, normalizedLogin))
if (users.length === 0) {
return 'Invalid credentials'
}
const user = users[0]
if (user.password !== md5(password)) {
return 'Invalid credentials'
}
if (user.socialClub !== player.socialClub) {
return 'Invalid credentials'
}
console.log('User logged in:', user, player.socialClub)
// Auth success
return 'Login successful'
} catch (e) {
console.error('Login error:', e)
return 'Internal server error'
}
})
-31
View File
@@ -1,31 +0,0 @@
/* tslint:disable */
/* eslint-disable */
/* auto-generated by NAPI-RS */
/**
* # checkLicense
* Accepts a 16-character license key string.
* Gets the server's local IPv4 address, prints it to the console,
* and validates the key. If valid, unlocks other functions.
*
* Returns `true` if the license key is valid, `false` otherwise.
*/
export declare function checkLicense(licenseKey: string): boolean
/**
* # doSensitiveOperation
* An example function that should only run if the license is validated.
* Takes a name as input and returns a greeting string.
*/
export declare function doSensitiveOperation(name: string): string
/**
* # getSecretData
* Another example function that should only run if the license is validated.
* Returns a dummy secret number.
*/
export declare function getSecretData(): number
/**
* # isLicensed (Optional utility)
* Allows JavaScript to check the current license status without triggering exit.
*/
export declare function isLicensed(): boolean
@@ -1,318 +0,0 @@
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
/* auto-generated by NAPI-RS */
const { existsSync, readFileSync } = require('fs')
const { join } = require('path')
const { platform, arch } = process
let nativeBinding = null
let localFileExisted = false
let loadError = null
function isMusl() {
// For Node 10
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim()
return readFileSync(lddPath, 'utf8').includes('musl')
} catch (e) {
return true
}
} else {
const { glibcVersionRuntime } = process.report.getReport().header
return !glibcVersionRuntime
}
}
switch (platform) {
case 'android':
switch (arch) {
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'gta5.android-arm64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./gta5.android-arm64.node')
} else {
nativeBinding = require('gta5-android-arm64')
}
} catch (e) {
loadError = e
}
break
case 'arm':
localFileExisted = existsSync(join(__dirname, 'gta5.android-arm-eabi.node'))
try {
if (localFileExisted) {
nativeBinding = require('./gta5.android-arm-eabi.node')
} else {
nativeBinding = require('gta5-android-arm-eabi')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Android ${arch}`)
}
break
case 'win32':
switch (arch) {
case 'x64':
localFileExisted = existsSync(
join(__dirname, 'gta5.win32-x64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.win32-x64-msvc.node')
} else {
nativeBinding = require('gta5-win32-x64-msvc')
}
} catch (e) {
loadError = e
}
break
case 'ia32':
localFileExisted = existsSync(
join(__dirname, 'gta5.win32-ia32-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.win32-ia32-msvc.node')
} else {
nativeBinding = require('gta5-win32-ia32-msvc')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'gta5.win32-arm64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.win32-arm64-msvc.node')
} else {
nativeBinding = require('gta5-win32-arm64-msvc')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Windows: ${arch}`)
}
break
case 'darwin':
localFileExisted = existsSync(join(__dirname, 'gta5.darwin-universal.node'))
try {
if (localFileExisted) {
nativeBinding = require('./gta5.darwin-universal.node')
} else {
nativeBinding = require('gta5-darwin-universal')
}
break
} catch {}
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'gta5.darwin-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./gta5.darwin-x64.node')
} else {
nativeBinding = require('gta5-darwin-x64')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'gta5.darwin-arm64.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.darwin-arm64.node')
} else {
nativeBinding = require('gta5-darwin-arm64')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on macOS: ${arch}`)
}
break
case 'freebsd':
if (arch !== 'x64') {
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
}
localFileExisted = existsSync(join(__dirname, 'gta5.freebsd-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./gta5.freebsd-x64.node')
} else {
nativeBinding = require('gta5-freebsd-x64')
}
} catch (e) {
loadError = e
}
break
case 'linux':
switch (arch) {
case 'x64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'gta5.linux-x64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.linux-x64-musl.node')
} else {
nativeBinding = require('gta5-linux-x64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'gta5.linux-x64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.linux-x64-gnu.node')
} else {
nativeBinding = require('gta5-linux-x64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'gta5.linux-arm64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.linux-arm64-musl.node')
} else {
nativeBinding = require('gta5-linux-arm64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'gta5.linux-arm64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.linux-arm64-gnu.node')
} else {
nativeBinding = require('gta5-linux-arm64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'gta5.linux-arm-musleabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.linux-arm-musleabihf.node')
} else {
nativeBinding = require('gta5-linux-arm-musleabihf')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'gta5.linux-arm-gnueabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('gta5-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
}
}
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'gta5.linux-riscv64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.linux-riscv64-musl.node')
} else {
nativeBinding = require('gta5-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'gta5.linux-riscv64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.linux-riscv64-gnu.node')
} else {
nativeBinding = require('gta5-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 's390x':
localFileExisted = existsSync(
join(__dirname, 'gta5.linux-s390x-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./gta5.linux-s390x-gnu.node')
} else {
nativeBinding = require('gta5-linux-s390x-gnu')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
break
default:
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}
if (!nativeBinding) {
if (loadError) {
throw loadError
}
throw new Error(`Failed to load native binding`)
}
const { checkLicense, doSensitiveOperation, getSecretData, isLicensed } = nativeBinding
module.exports.checkLicense = checkLicense
module.exports.doSensitiveOperation = doSensitiveOperation
module.exports.getSecretData = getSecretData
module.exports.isLicensed = isLicensed