feat: implement account settings and API tokens management
- Added SettingsComponent for user profile management, including display name and password change functionality. - Introduced TokensComponent for managing API tokens, including creation and revocation. - Created LayoutComponent for consistent application layout with navigation and user information. - Established main application structure in index.html and main.ts. - Integrated Tailwind CSS for styling and responsive design. - Configured TypeScript settings for strict type checking and module resolution.
This commit is contained in:
115
ts/models/user.ts
Normal file
115
ts/models/user.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* User model for Stack.Gallery Registry
|
||||
*/
|
||||
|
||||
import * as plugins from '../plugins.ts';
|
||||
import type { IUser, TUserStatus } from '../interfaces/auth.interfaces.ts';
|
||||
import { getDb } from './db.ts';
|
||||
|
||||
@plugins.smartdata.Collection(() => getDb())
|
||||
export class User extends plugins.smartdata.SmartDataDbDoc<User, User> implements IUser {
|
||||
@plugins.smartdata.unI()
|
||||
public id: string = '';
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
@plugins.smartdata.searchable()
|
||||
@plugins.smartdata.index({ unique: true })
|
||||
public email: string = '';
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
@plugins.smartdata.searchable()
|
||||
@plugins.smartdata.index({ unique: true })
|
||||
public username: string = '';
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public passwordHash: string = '';
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
@plugins.smartdata.searchable()
|
||||
public displayName: string = '';
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public avatarUrl?: string;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
@plugins.smartdata.index()
|
||||
public status: TUserStatus = 'pending_verification';
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public emailVerified: boolean = false;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public mfaEnabled: boolean = false;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public mfaSecret?: string;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public lastLoginAt?: Date;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public lastLoginIp?: string;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public failedLoginAttempts: number = 0;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public lockedUntil?: Date;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
@plugins.smartdata.index()
|
||||
public isPlatformAdmin: boolean = false;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
@plugins.smartdata.index()
|
||||
public createdAt: Date = new Date();
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public updatedAt: Date = new Date();
|
||||
|
||||
/**
|
||||
* Create a new user instance
|
||||
*/
|
||||
public static async createUser(data: {
|
||||
email: string;
|
||||
username: string;
|
||||
passwordHash: string;
|
||||
displayName?: string;
|
||||
}): Promise<User> {
|
||||
const user = new User();
|
||||
user.id = await User.getNewId();
|
||||
user.email = data.email.toLowerCase();
|
||||
user.username = data.username.toLowerCase();
|
||||
user.passwordHash = data.passwordHash;
|
||||
user.displayName = data.displayName || data.username;
|
||||
user.status = 'pending_verification';
|
||||
user.createdAt = new Date();
|
||||
user.updatedAt = new Date();
|
||||
await user.save();
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find user by email
|
||||
*/
|
||||
public static async findByEmail(email: string): Promise<User | null> {
|
||||
return await User.getInstance({ email: email.toLowerCase() });
|
||||
}
|
||||
|
||||
/**
|
||||
* Find user by username
|
||||
*/
|
||||
public static async findByUsername(username: string): Promise<User | null> {
|
||||
return await User.getInstance({ username: username.toLowerCase() });
|
||||
}
|
||||
|
||||
/**
|
||||
* Lifecycle hook: Update timestamps before save
|
||||
*/
|
||||
public async beforeSave(): Promise<void> {
|
||||
this.updatedAt = new Date();
|
||||
if (!this.id) {
|
||||
this.id = await User.getNewId();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user