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

View File

@ -74,60 +74,21 @@ export function useLoginForm({ onSubmitSuccess }: UseLoginFormProps = {}) {
setIsLoading(true)
console.log('Submitting login data:', formData)
// --- TODO: Replace with actual API call to your RageMP server ---
try {
// Example: Simulate API call
await new Promise(resolve => setTimeout(resolve, 1000))
// Assuming API returns success:
console.log('Login successful!')
// Here you would typically receive a token or session info
// Handle 'saveLogin' and 'savePassword' (e.g., using localStorage/sessionStorage)
if (formData.saveLogin) {
localStorage.setItem('savedUsername', formData.identifier) // Example
} else {
localStorage.removeItem('savedUsername') // Example
}
// Password saving is generally discouraged for security reasons,
// but if required:
if (formData.savePassword) {
// Be VERY careful with storing passwords. Consider secure storage or tokens.
// localStorage.setItem('savedPassword', formData.password); // **Highly discouraged**
console.warn(
'Password saving enabled - ensure secure storage mechanism.',
)
}
setErrors({}) // Clear errors on success
if (onSubmitSuccess) {
onSubmitSuccess(formData) // Call success callback
}
// You might redirect the user or update application state here
// --- Mock Error Handling (remove in real implementation) ---
// if (formData.identifier === 'wrong') {
// throw new Error("Invalid credentials.");
// }
// --- End Mock Error Handling ---
} catch (apiError: unknown) {
console.error('Login API error:', apiError)
setSubmitError('Login failed. Please check your credentials.')
} finally {
setIsLoading(false)
}
// --- End API call section ---
},
[formData, validateForm, onSubmitSuccess],
)
// Effect to potentially load saved username on initial render (example)
// useEffect(() => {
// const savedUser = localStorage.getItem('savedUsername');
// if (savedUser) {
// setFormData(prev => ({ ...prev, identifier: savedUser, saveLogin: true }));
// }
// }, []);
return {
formData,
errors,

View File

@ -74,13 +74,7 @@ export function useRegisterForm({
setIsLoading(true)
console.log('Submitting registration data:', formData)
// --- TODO: Replace with actual API call to your RageMP server ---
try {
// Example: Simulate API call
await new Promise(resolve => setTimeout(resolve, 1500))
// Assuming API returns success:
console.log('Registration successful!')
setFormData({
username: '',
email: '',
@ -92,20 +86,12 @@ export function useRegisterForm({
if (onSubmitSuccess) {
onSubmitSuccess(formData) // Call success callback if provided
}
// --- Mock Error Handling (remove in real implementation) ---
// if (formData.email.includes('fail')) {
// throw new Error("Registration failed: Email already exists.");
// }
// --- End Mock Error Handling ---
} catch (apiError: unknown) {
console.error('Registration API error:', apiError)
// Try to set a user-friendly error message
setSubmitError('Registration failed. Please try again.')
} finally {
setIsLoading(false)
}
// --- End API call section ---
},
[formData, validateForm, onSubmitSuccess],
)

View File

@ -1,4 +1,8 @@
import React from 'react'
import { fw } from '@entityseven/rage-fw-browser'
import { AuthEvent } from '../../../../shared/events'
import Input from '../../components/ui/Input'
import Checkbox from '../../components/ui/Checkbox'
import Button from '../../components/ui/Button'
@ -6,10 +10,17 @@ import { useLoginForm } from '../../hooks/useLoginForm'
import { LoginFormData } from '../../validation'
const LoginPage: React.FC = () => {
const handleLoginSuccess = (data: LoginFormData) => {
console.log('Login success callback triggered:', data)
// Redirect user or update app state
alert(`Login successful for ${data.identifier}!`)
const handleLoginSuccess = async (data: LoginFormData) => {
const tempData = {
login: data.identifier,
password: data.password,
}
const response = await fw.event.triggerServer(AuthEvent.LOGIN, [
tempData,
])
console.log(response)
}
const {
@ -56,13 +67,13 @@ const LoginPage: React.FC = () => {
<div className="rounded-md shadow-sm -space-y-px">
<Input
label="Email or Username"
label="Username"
id="identifier"
name="identifier"
type="text" // Use text to allow both email and username
autoComplete="username" // Browsers often use 'username' for this field
required
placeholder="Email address or Username"
placeholder="Username"
value={formData.identifier}
onChange={handleChange}
error={errors.identifier}

View File

@ -1,14 +1,27 @@
import React from 'react'
import { fw } from '@entityseven/rage-fw-browser'
import { AuthEvent } from '../../../../shared/events'
import Input from '../../components/ui/Input'
import Button from '../../components/ui/Button'
import { useRegisterForm } from '../../hooks/useRegisterForm'
import { RegisterFormData } from '../../validation'
const RegisterPage: React.FC = () => {
const handleRegistrationSuccess = (data: RegisterFormData) => {
console.log('Registration success callback triggered:', data)
// Maybe show a success message or redirect
alert(`Registration successful for ${data.username}!`)
const handleRegistrationSuccess = async (data: RegisterFormData) => {
const tempData = {
login: data.username,
password: data.password,
email: data.email,
}
const response = await fw.event.triggerServer(AuthEvent.REGISTER, [
tempData,
])
console.log(response)
}
const {

View File

@ -1,3 +1,3 @@
import { fw } from '@entityseven/rage-fw-client'
fw.player.browser = mp.browsers.new('https://localhost:5173')
fw.player.browser = mp.browsers.new('package://cef/index.html')

5
apps/server/.env Normal file
View File

@ -0,0 +1,5 @@
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="mypassword"
POSTGRES_DB="postgres"
DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB}"

View File

@ -0,0 +1,20 @@
services:
database:
image: postgres:17
ports:
- "5432:5432"
healthcheck:
test:
[ "CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}" ]
interval: 5s
timeout: 10s
retries: 5
environment:
- POSTGRES_PASSWORD
- POSTGRES_USER
- POSTGRES_DB
volumes:
- rage_fw_db_data:/var/lib/postgresql/data
volumes:
rage_fw_db_data:

View File

@ -0,0 +1,15 @@
import 'dotenv/config'
import { defineConfig } from 'drizzle-kit'
export default defineConfig({
out: './drizzle',
schema: './src/db/schema.ts',
dialect: 'postgresql',
dbCredentials: {
user: process.env.POSTGRES_USER!,
password: process.env.POSTGRES_PASSWORD!,
database: process.env.POSTGRES_DB!,
host: 'localhost',
ssl: false,
},
})

View File

@ -0,0 +1,10 @@
CREATE TABLE "users" (
"id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
"socialClub" varchar(255) NOT NULL,
"email" varchar(255) NOT NULL,
"login" varchar(255) NOT NULL,
"password" varchar(255) NOT NULL,
CONSTRAINT "users_socialClub_unique" UNIQUE("socialClub"),
CONSTRAINT "users_email_unique" UNIQUE("email"),
CONSTRAINT "users_login_unique" UNIQUE("login")
);

View File

@ -0,0 +1,95 @@
{
"id": "690592ed-01d9-40ee-91c5-45e2ed38926b",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"identity": {
"type": "always",
"name": "users_id_seq",
"schema": "public",
"increment": "1",
"startWith": "1",
"minValue": "1",
"maxValue": "2147483647",
"cache": "1",
"cycle": false
}
},
"socialClub": {
"name": "socialClub",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"login": {
"name": "login",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"password": {
"name": "password",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"users_socialClub_unique": {
"name": "users_socialClub_unique",
"nullsNotDistinct": false,
"columns": [
"socialClub"
]
},
"users_email_unique": {
"name": "users_email_unique",
"nullsNotDistinct": false,
"columns": [
"email"
]
},
"users_login_unique": {
"name": "users_login_unique",
"nullsNotDistinct": false,
"columns": [
"login"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1744901803714,
"tag": "0000_burly_jubilee",
"breakpoints": true
}
]
}

View File

@ -5,9 +5,19 @@
"license": "CC BY-ND",
"description": "Server side of rage-fw example",
"scripts": {
"build": "esbuild src/index.ts --bundle --platform=node --target=node14.10 --external:*.node --format=cjs --outfile=../../server/packages/server/index.js"
"build": "esbuild src/index.ts --bundle --platform=node --target=node14.10 --format=cjs --outfile=../../server/packages/server/index.js"
},
"dependencies": {
"@entityseven/rage-fw-server": "latest"
"@entityseven/rage-fw-server": "latest",
"dotenv": "^16.5.0",
"drizzle-orm": "^0.42.0",
"md5": "^2.3.0",
"pg": "^8.14.1"
},
"devDependencies": {
"@types/pg": "^8.11.13",
"drizzle-kit": "^0.31.0",
"tsx": "^4.19.3",
"@types/md5": "^2.3.5"
}
}

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)

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(),
})

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'
}
})

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

View File

@ -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

View File

@ -1,10 +1,17 @@
import { AuthEvent } from '../../events'
declare module '@entityseven/rage-fw-shared-types' {
export interface RageFW_ICustomClientEvent {
customClientEvent(greetings: string): string
}
export interface RageFW_ICustomServerEvent {
customServerEvent(greetings: string): string
[AuthEvent.LOGIN](data: { login: string; password: string }): string
[AuthEvent.REGISTER](data: {
login: string
email: string
password: string
}): string
}
export interface RageFW_ICustomBrowserEvent {

View File

@ -0,0 +1,4 @@
export enum AuthEvent {
LOGIN = 'AuthEvent::LOGIN',
REGISTER = 'AuthEvent::REGISTER',
}

7
apps/shared/package.json Normal file
View File

@ -0,0 +1,7 @@
{
"name": "@shared",
"version": "0.1.0",
"author": "Entity Seven Group",
"license": "CC BY-ND",
"description": "Shared types for rage-fw example"
}

View File

@ -1,7 +1,9 @@
{
"name": "framework-example",
"description": "This project is example of RAGE FW usage.",
"workspaces": ["apps/*"],
"workspaces": [
"apps/*"
],
"scripts": {
"server:update": "cd server && rage-win64.exe",
"build:client": "cd apps/client && pnpm build",
@ -20,5 +22,6 @@
},
"author": "Entity Seven Group",
"license": "MIT",
"version": "0.1.0"
"version": "0.1.0",
"packageManager": "pnpm@10.2.1+sha512.398035c7bd696d0ba0b10a688ed558285329d27ea994804a52bad9167d8e3a72bcb993f9699585d3ca25779ac64949ef422757a6c31102c12ab932e5cbe5cc92"
}

5
server/.env Normal file
View File

@ -0,0 +1,5 @@
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="mypassword"
POSTGRES_DB="postgres"
DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB}"

Binary file not shown.