mnemo_cards/mnemo_cards_backend/lib/database/tables/auth.dart
Dmitry 8ce8569625
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Deploy Admin Panel / Deploy Admin Panel (push) Waiting to run
Deploy Admin Panel / Admin Panel Verification (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
fixes
2025-12-15 00:55:42 +03:00

87 lines
3.6 KiB
Dart

import 'package:drift/drift.dart';
import 'package:drift_postgres/drift_postgres.dart';
import 'users.dart';
import '../converters.dart' show generateUuid;
/// Таблица Tokens - токены авторизации пользователей
class Tokens extends Table {
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
TextColumn get id => text()
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
TextColumn get userId => text()
.references(Users, #id, onDelete: KeyAction.cascade)();
TextColumn get token => text().unique()();
TextColumn get externalUserId => text()();
Column<PgDateTime> get created => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
Column<PgDateTime> get expires => customType(PgTypes.timestampWithTimezone)();
BoolColumn get isDeleted => boolean()
.customConstraint('NOT NULL DEFAULT FALSE')();
Column<PgDateTime> get deletedAt => customType(PgTypes.timestampWithTimezone)
.nullable()();
@override
Set<Column> get primaryKey => {id};
@override
List<String> get customConstraints => [
'CONSTRAINT valid_expiry CHECK (expires > created)',
];
}
/// Таблица RefreshTokens - refresh токены для JWT
class RefreshTokens extends Table {
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
TextColumn get id => text()
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
TextColumn get userId => text()
.references(Users, #id, onDelete: KeyAction.cascade)();
TextColumn get jti => text().unique()(); // JWT ID
BoolColumn get isBlacklisted => boolean()
.customConstraint('NOT NULL DEFAULT FALSE')(); // PostgreSQL использует нативный BOOLEAN
Column<PgDateTime> get createdAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
Column<PgDateTime> get expiresAt => customType(PgTypes.timestampWithTimezone)();
BoolColumn get isDeleted => boolean()
.customConstraint('NOT NULL DEFAULT FALSE')();
Column<PgDateTime> get deletedAt => customType(PgTypes.timestampWithTimezone)
.nullable()();
@override
Set<Column> get primaryKey => {id};
}
/// Таблица TelegramAuthCodes - коды для авторизации через Telegram
class TelegramAuthCodes extends Table {
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
TextColumn get id => text()
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
TextColumn get code => text().unique()();
TextColumn get telegramUserId => text()();
TextColumn get telegramUsername => text().nullable()();
TextColumn get firstName => text().nullable()();
TextColumn get lastName => text().nullable()();
BoolColumn get isUsed => boolean()
.withDefault(const Constant(false))
.customConstraint('')(); // PostgreSQL использует нативный BOOLEAN
Column<PgDateTime> get usedAt => customType(PgTypes.timestampWithTimezone)
.nullable()();
Column<PgDateTime> get createdAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
Column<PgDateTime> get expiresAt => customType(PgTypes.timestampWithTimezone)();
BoolColumn get isDeleted => boolean()
.customConstraint('NOT NULL DEFAULT FALSE')();
Column<PgDateTime> get deletedAt => customType(PgTypes.timestampWithTimezone)
.nullable()();
@override
Set<Column> get primaryKey => {id};
}