supertokens_flutter 0.6.3 copy "supertokens_flutter: ^0.6.3" to clipboard
supertokens_flutter: ^0.6.3 copied to clipboard

SuperTokens SDK for Flutter apps

SuperTokens Flutter SDK #

chat on Discord

About #

This is a Flutter SDK written in pure dart that is responsible for maintaining a SuperTokens session for a Flutter app.

Learn more at https://supertokens.com

Usage #

Initialise the SDK #

import 'package:supertokens_flutter/supertokens.dart';

// Initialise the SDK on app launch
void main() {
    SuperTokens.init(apiDomain: "http://localhost:3001");
}
copied to clipboard

Checking if a session exists #

import 'package:supertokens_flutter/supertokens.dart';

Future<bool> doesSessionExist() async {
    return await SuperTokens.doesSessionExist();
}
copied to clipboard

Usage with http #

Making network requests

You can make requests as you normally would with http, the only difference is that you import the client from the supertokens package instead.

// Import http from the SuperTokens package
import 'package:supertokens_flutter/http.dart' as http;

Future<void> makeRequest() {
    Uri uri = Uri.parse("http://localhost:3001/api");
    var response = await http.get(uri);
    // handle response
}
copied to clipboard

The SuperTokens SDK will handle session expiry and automatic refreshing for you. When calling authentication APIs such as signin or signup, the SDK automatically captures the access- and refresh tokens from the headers and saves them for you.

Using a custom http Client

If you use a custom http client and want to use SuperTokens, you can simply provide the SDK with your client. All requests will continue to use your client along with the session logic that SuperTokens provides.

// Import http from the SuperTokens package
import 'package:supertokens_flutter/http.dart' as http;

Future<void> makeRequest() {
    Uri uri = Uri.parse("http://localhost:3001/api");

    // provide your custom client to SuperTokens
    var httpClient = http.Client(client: customClient)

    var response = await httpClient.get(uri);
    // handle response
}
copied to clipboard

Usage with Dio #

Add the SuperTokens interceptor

You can make requests as you normally would with dio.

import 'package:supertokens_flutter/dio.dart';

void setup() {
    Dio dio = Dio(...)
    dio.interceptors.add(SuperTokensInterceptorWrapper(client: dio));
}
copied to clipboard

Or use instance method instead.

import 'package:supertokens_flutter/dio.dart';

void setup() {
  Dio dio = Dio();  // Create a Dio instance.
  dio.addSupertokensInterceptor();
}
copied to clipboard

Making network requests

import 'package:supertokens_flutter/dio.dart';

void setup() {
    Dio dio = Dio(...)
    dio.interceptors.add(SuperTokensInterceptorWrapper(client: dio));

    var response = dio.get("http://localhost:3001/api");
    // handle response
}
copied to clipboard

Signing out #

import 'package:supertokens_flutter/supertokens.dart';

Future<void> signOut() async {
    await SuperTokens.signOut();
}
copied to clipboard

Getting the user id #

import 'package:supertokens_flutter/supertokens.dart';

Future<String> getUserId() async {
    return await SuperTokens.getUserId();
}
copied to clipboard

Manually refresh sessions #

import 'package:supertokens_flutter/supertokens.dart';

Future<void> manualRefresh() async {
    // Returns true if session was refreshed, false if session is expired
    var success = await SuperTokens.attemptRefreshingSession();
}
copied to clipboard

Contributing #

Please refer to the CONTRIBUTING.md file in this repo.

Contact us #

For any queries, or support requests, please email us at team@supertokens.com, or join our Discord server.

Authors #

Created with ❤️ by the folks at SuperTokens.com.

13
likes
90
points
552
downloads

Publisher

unverified uploader

Weekly Downloads

2024.08.26 - 2025.07.21

SuperTokens SDK for Flutter apps

Homepage
Repository (GitHub)
View/report issues
Contributing

Documentation

Documentation
API reference

License

unknown (license)

Dependencies

dio, flutter, http, mutex, shared_preferences

More

Packages that depend on supertokens_flutter