Get started
Dart and Flutter SDK
Get started

Authentication in the SDK

Authentication in the SDK

The publishable key goes in every client. A session token goes beside it for the Data API, for drafts and for every write. Your server mints the token with the secret key, and the app keeps it fresh.

Mint on your server

Call postSession with your secret key, for the user your own sign-in names:

import 'dart:io';

import 'package:mountaya_admin/admin_schema.dart' as admin_schema;
import 'package:mountaya_admin/mountaya_admin.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;

final environment = Platform.environment;
final server = Mountaya(
  credential: SecretCredential(environment['MOUNTAYA_SECRET_KEY'] ?? ''),
);

// What your session route answers, as JSON: token, expires_at, user_id.
Future<rest_schema.Session> mintSession(String userId) async {
  final minted = await server.admin.sessions.postSession(
    body: admin_schema.SessionCreateParams(
      publishable_key: environment['MOUNTAYA_PUBLISHABLE_KEY'] ?? '',
      // The member your own sign-in vouches for, never one the request names.
      user_id: userId,
    ),
  );
  return minted.data;
}

final session = await mintSession('7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1');
print(session.expires_at);
await server.close();

Your session route answers session.toJson(); the server quickstart serves it at POST /session.

Keep the token fresh in the app

Give the client a SessionTokenEngine. It calls your route, and mints again before each token expires:

import 'dart:convert';
import 'dart:io';

import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;

const publishableKey = String.fromEnvironment('MOUNTAYA_PUBLISHABLE_KEY');
const sessionUrl = String.fromEnvironment('MOUNTAYA_SESSION_URL');

// Posts to your session route and reads the session it answers.
Future<rest_schema.Session> mintSession() async {
  final http = HttpClient();
  try {
    final request = await http.postUrl(Uri.parse(sessionUrl));
    final response = await request.close();
    final body = await response.transform(utf8.decoder).join();
    if (response.statusCode != HttpStatus.ok) {
      throw HttpException('The session route answered ${response.statusCode}.');
    }
    return rest_schema.Session.fromJson(jsonDecode(body));
  } finally {
    http.close();
  }
}

final sessions = SessionTokenEngine(mint: mintSession);
final mountaya = Mountaya(
  credential: .publishable(key: publishableKey, sessionToken: .source(sessions)),
);
await mountaya.close();

One client serves the whole app. Each request, and each map tile, carries the current token. Call sessions.refresh() after your user signs in, and sessions.disable() when they sign out.

Open a shared itinerary

A share link carries a share token, which replaces the key for its visitor:

import 'package:mountaya_client/mountaya_client.dart';

// Read from the share link your visitor opened.
const shareToken = 'share_your_share_token';

final visitor = Mountaya(credential: .share(token: shareToken));
final itinerary = await visitor.studio.itineraries.getItinerary(
  itinerary_id: '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21',
);
print(itinerary.data.name);
await visitor.close();

Credential(publishableKey:, shareToken:) builds the credential from parts you hold apart, and sends the share token alone when both are set.

Act as a member

A token minted without user_id reads only what your organization publishes, and writes nothing. To write, mint the token for the member your own sign-in vouches for:

import 'dart:io';

import 'package:mountaya_admin/admin_schema.dart' as admin_schema;
import 'package:mountaya_admin/mountaya_admin.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;

final environment = Platform.environment;
final publishableKey = environment['MOUNTAYA_PUBLISHABLE_KEY'] ?? '';
final server = Mountaya(
  credential: SecretCredential(environment['MOUNTAYA_SECRET_KEY'] ?? ''),
);
final minted = await server.admin.sessions.postSession(
  body: admin_schema.SessionCreateParams(
    publishable_key: publishableKey,
    user_id: '7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1',
    surface: rest_schema.SessionSurface.studioEmbedded,
  ),
);

// Every call with this token acts as that member.
final member = Mountaya(
  credential: .publishable(key: publishableKey, sessionToken: .minted(minted.data)),
);
final renamed = await member.studio.itineraries.patchItinerary(
  itinerary_id: '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21',
  body: rest_schema.ItineraryUpdateParams(name: 'Tour du Mont-Blanc, summer'),
);
print(renamed.data.name);
await member.close();
await server.close();

Read the member's id from your server's own session, never from the request.

Keep the secret key out of the app

SecretCredential exists on the Dart VM only. In a Flutter build the name does not exist, so code that holds a secret key does not compile, and no key can ship inside an app.

Next steps

Last updated