Moor with bloc authentication

334 views Asked by At

I am developing a Todo flutter app and I am using flutter_moor and bloc and I want to achieve the following in the best and most secure way:

  • Let the user signup.
  • Let the user login after signingup.
  • Save user ID after signin to show user todos.

What is the best practice to achieve that?
Thank you everyone.
My database.dart is like following:

import 'package:moor_flutter/moor_flutter.dart';

part 'database.g.dart';

class Todos extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text().withLength(min: 6, max: 32)();
  TextColumn get description => text().named('body')();
  DateTimeColumn get createdAt => dateTime().nullable()();
  BoolColumn get finished => boolean().withDefault(Constant(false))();
  IntColumn get userID => integer()();
  IntColumn get typeID => integer().nullable()();
}

class TodoTypes extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text()();
}

class Users extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get username => text().withLength(min: 3, max: 32)();
  TextColumn get password => text().withLength(min: 3, max: 32)();
}

@UseMoor(tables: [Todos, TodoTypes, Users])
class AppDatabase extends _$AppDatabase {
  AppDatabase()
      : super(FlutterQueryExecutor(path: 'db.sqlite', logStatements: true));

  @override
  int get schemaVersion => 1;

  //Todos table
  Future<List<Todo>> getAllTodos() => select(todos).get();
  Stream<List<Todo>> watchAllTodos() => select(todos).watch();
  Future insertTodo(Todo todo) => into(todos).insert(todo);
  Future updateTodo(Todo todo) => update(todos).replace(todo);
  Future deleteTodo(Todo todo) => delete(todos).delete(todo);
  //Types table
  Future<List<TodoType>> getAllTypes() => select(todoTypes).get();
  Stream<List<TodoType>> watchAllTypes() => select(todoTypes).watch();
  Future insertTodoType(TodoType todoType) => into(todoTypes).insert(todoType);
  Future updateTodoType(TodoType todoType) =>
      update(todoTypes).replace(todoType);
  Future deleteTodoType(TodoType todoType) =>
      delete(todoTypes).delete(todoType);
  //Users table
  Future<List<User>> getAllUsers() => select(users).get();
  Stream<List<User>> watchAllUsers() => select(users).watch();
  Stream<List<User>> getUser(User u) {
    return (select(users)..where((t) => t.username.equals(u.username))).watch();
  }
  Future insertUser(User user) => into(users).insert(user);
  Future updateUser(User user) => update(users).replace(user);
  Future deleteUser(User user) => delete(users).delete(user);
}
0

There are 0 answers