flutter_keycloak

Manage your Keycloak tokens in a Flutter app.

This plugin exposes some util methods to interact with KeycloakHome in order to handle the user session.

Documentation

  • SetupAnchor
  • APIAnchor
  • UtilsAnchor

Setup

App configuration

Imports

Import the library and initialize the plugin

import 'package:flutter_keycloak/flutter_keycloak.dart';

final FlutterKeycloak _flutterKeycloak = FlutterKeycloak();

API

login

void login() async {
  await _flutterKeycloak.login(
    _conf,
    _username,
    _password,
    scope: _scope,
  );
}

Sometimes you may need to re-login your user w/ Keycloak via the login process but, for some reason, you don't want / can't display the login page.
This method will re-login your user.

refreshLogin

void refreshLogin() async {
  await _flutterKeycloak.refreshLogin(
    scope: 'offline_access',
  );
}

retrieveUserInfo

void retrieveUserInfo() async {
  final userInfo = await _flutterKeycloak.retrieveUserInfo();
  setState(() {
    _currentPrefs = userInfo.toString();
  });
}

logout

void logout() async {
  await _flutterKeycloak.logout();
  printStorage();
}

destroySession: Since the /openid-connect/token simply returns an access token and doesn't create any session on Keycloak side, if you used the login method you want to pass false.
Passing true tries to destroy the session: pay attention that on newer Keycloak versions this raises an error if no session is present, preventing the logout.