sign_in_with_apple_server 1.0.0 copy "sign_in_with_apple_server: ^1.0.0" to clipboard
sign_in_with_apple_server: ^1.0.0 copied to clipboard

Server-side utilities for implementing Sign in with Apple in a server written in Dart, complementing the client-side sign_in_with_apple package.

example/example.dart

import 'package:sign_in_with_apple_server/sign_in_with_apple_server.dart';

final siwa = SignInWithApple(
  config: SignInWithAppleConfiguration(
    serviceIdentifier: 'com.acme.app-service',
    bundleIdentifier: 'com.acme.app',
    redirectUri: 'https://acme.com/hooks/return-url',
    teamId: 'ABC1234567',
    keyId: '89DEFGHIJK',
    key: '-----BEGIN PRIVATE KEY---……',
  ),
);

/// A combined Sign in with Apple registration / login endpoint.
///
/// Returns a session key ("cookie") after successful authorization.
Future<String> signInEndpoint({
  required String authorizationCode,
  required String identityToken,

  /// If `true` this means the sign-in comes from an Apple native OS,
  /// in which case the bundle identifier is used, else the service ID.
  required bool useBundleIdentifier,
  required String? firstName,
  required String? lastName,
}) async {
  final verifiedIdentityToken = await siwa.verifyIdentityToken(
    identityToken,
    useBundleIdentifier: useBundleIdentifier,
    nonce: null,
  );

  // Now check whether the user is already known in the system,
  // by looking up the [verifiedIdentityToken.userId] which is Apple's
  // unique user identifer for the linked account.
  final foundUser = await findUserByAppleUserId(verifiedIdentityToken.userId);
  if (foundUser != null) {
    return createSession(foundUser.id);
  }

  // If no user was found, create a new user in your sytem.
  final refreshToken = await siwa.exchangeAuthorizationCode(
    authorizationCode,
    useBundleIdentifier: useBundleIdentifier,
  );

  final newUser = await createUser(
    appleUserId: verifiedIdentityToken.userId,
    email: verifiedIdentityToken.emailVerified == true
        ? verifiedIdentityToken.email
        : null,
    refreshToken: refreshToken.refreshToken,
    useBundleIdentifier: useBundleIdentifier, // store this for later
    firstName: firstName,
    lastName: lastName,
  );

  return createSession(newUser.id);
}
1
likes
160
points
6.52k
downloads

Publisher

verified publishertimm.preetz.xyz

Weekly Downloads

Server-side utilities for implementing Sign in with Apple in a server written in Dart, complementing the client-side sign_in_with_apple package.

Repository (GitHub)

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

dart_jsonwebtoken, http, meta

More

Packages that depend on sign_in_with_apple_server