Back

JWT Dart SDK JWT token regeneration

  • 0
  • Flutter
Cartok
29 Nov, 2023, 16:20

Hi there!

I am currently trying to use the Dart SDK but am quite new to Dart, Flutter and Appwrite. I got some programming experience though.

Do I need to manually create and refresh JWT tokens, or is that automatically done by the SDK? The documentation shows nothing about createJWT and setJWT.

TL;DR
The user is asking if they need to manually create and refresh JWT tokens when using the Dart SDK for Appwrite. They provided some code and are looking for clarification. No solution is provided in the thread.
Cartok
29 Nov, 2023, 16:23

I wonder if I wrote this here now in vain:

TypeScript
import 'dart:developer';

import 'package:appwrite/appwrite.dart';
import 'package:appwrite/models.dart';
import 'package:notes_tasks/appwrite/account.dart';

import 'client.dart';

final Database database = Database(client);

class Database extends Databases {
  static const databaseId = '6566589714f646a1b072';
  static const notesCollectionId = '656658c1d82904d25cde';

  static Database? _instance;
  Database._private(Client client) : super(client);
  factory Database(Client client) {
    _instance ??= Database._private(client);
    return _instance!;
  }

  Future<T> wrap<T>(Future<T> Function() closure) async {
    try {
      return await closure();
    } catch (e) {
      inspect(e);

      try {
        var jwt = await account.createJWT();
        client.setJWT(jwt.jwt);

        return await closure();
      } catch (e) {
        inspect(e);

        // TODO: Rather check error type string.
        if (e is AppwriteException && e.code == 401) {
          // TODO: Create and send a Bloc event, listen to it in view and redirect to login screen.
          rethrow;
        }

        throw Exception('Unexpected error while refreshing JWT.'
            'Check the database configuration.');
      }
    }
  }

  @override
  Future<DocumentList> listDocuments({
    String databaseId = databaseId,
    required String collectionId,
    List<String>? queries,
  }) async {
    return wrap(() => super.listDocuments(
          databaseId: databaseId,
          collectionId: collectionId,
          queries: queries,
        ));
  }

  @override
  Future<Document> getDocument({
    String databaseId = databaseId,
    required String collectionId,
    required String documentId,
    List<String>? queries,
  }) {
    return wrap(() => super.getDocument(
          databaseId: databaseId,
          collectionId: collectionId,
          documentId: documentId,
          queries: queries,
        ));
  }
}
Drake
29 Nov, 2023, 17:38

For auth, have you looked and done this? https://appwrite.io/docs/products/auth/quick-start

Drake
29 Nov, 2023, 17:40

btw, if you're interested, we streamed building a flutter app from scratch here: https://www.youtube.com/@FreshFullStack/videos

The video where we start on mobile is here: https://www.youtube.com/watch?v=4cahiFt_bAg

Cartok
30 Nov, 2023, 08:25

Great, thanks for the videos.

Cartok
30 Nov, 2023, 08:26

Yes, I did that. I have a registration and login form. So no manual JWT token handling is required, once logged in?

Cartok
30 Nov, 2023, 08:36

One thing I just noticed is, that I can make DB requests without logging into the account.

Cartok
30 Nov, 2023, 08:41

I will watch the second video, maybe things get clearer

Ernest
30 Nov, 2023, 10:26

If you can make requests to the DB without logging in it's probably because you've allowed Anyone to access the collection in this case. Checkout appwrite's permissions https://appwrite.io/docs/products/databases/permissions

Ernest
30 Nov, 2023, 10:27

Steven do you have a link for the mobile app code repo?

Drake
30 Nov, 2023, 15:53
Drake
30 Nov, 2023, 15:54

the SDK automatically handles sessions in the Client (including persisting it). Maybe that's what's going on?

Ernest
30 Nov, 2023, 16:06

Thanks. I was hoping to learn something from how you've handled jwt expiry when fetching images for a user

Drake
30 Nov, 2023, 16:10

our photos are public so no need for JWT 😅

Ernest
30 Nov, 2023, 16:16

Yes, realised from the code. I'll think of a way when I get the time to work on my project. Still keeping an eye on the repo (honour of second star is all mine). Thanks anyways

Cartok
30 Nov, 2023, 16:19

@Ernest I only allowed a specific user access. Thats why I am puzzeled

Ernest
30 Nov, 2023, 16:21

As Steven said above it's probably due to the session cache. To verify that it's the cache, you can clear the app's data (if you're developing on android) then try to access the db

Cartok
30 Nov, 2023, 16:36

I'll try out. So far I used the Linux client. I will have another look.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more