/enhanced_meteorify

A dart package for interacting with Meteor

Primary LanguageDartBSD 2-Clause "Simplified" LicenseBSD-2-Clause

I'm running out of time to work on the package. Pull requests are welcome!

Enhanced Meteorify

Pub

Carefully extended meteorify package to interact with the Meteor framework.

Connect your web or flutter apps, written in Dart, to the Meteor framework.

Features

  • Connect to Meteor server
  • Use Meteor Subscriptions
  • Meteor Authentication
  • oAuth Authentication with Google, Facebook and Apple* (needs server-side code in JavaScript for use with Meteor)
  • Call custom Methods on Meteor
  • Access underlying databases

*Login with Apple currently only supports iOS 13+.

Dependency

Add this to your package's pubspec.yaml file:

dependencies:
  enhanced_meteorify: ^2.3.4

Null safety:

dependencies:
  enhanced_meteorify: ^3.1.0

Usage

Capturing the result of operations

You can use the await syntax to capture the result of operations and handle errors.

I'll be using the await syntax in this documentation to keep it short and straight.

Connection Operations

Connecting with Meteor

import 'package:meteorify/meteorify.dart';

main() async{
  try{
      var status = await Meteor.connect('ws://example.meteor.com/websocket', );
      // Do something after connection is successful
  }catch(error){
      print(error);
      //Handle error
  }
}

Check connection status

var isConnected = Meteor.isConnected;		

Disconnect from server

Meteor.disconnect();

Listen for connection updates

Meteor.connectionListener = (ConnectionStatus connectionStatus) {
  print(connectionStatus);
}

Listen for userId

Meteor.currentUserIdListener = (String userId) {
    print(userId);
}

Subscriptions

Subscribe to Data

var subscriptionId = await Meteor.subscribe(subscriptionName);

Subscriptions with Parameters

var subscriptionId = await Meteor.subscribe(subscriptionName,args:[arg1,arg2]);

Unsubscribe from Data

await Meteor.unsubscribe(subscriptionId);

Get subscribed data/collection

SubscribedCollection collection = await Meteor.collection(collectionName);
//collection.find({selectors});
//collection.findAll();
//collection.findOne(id);

Authentication

Creating New Account

var userId = await Meteor.createUser(username, email, password, profileOptions);

Login

  1. Login with password

    // Login with email
    try {
       String loginToken = await Meteor.loginWithPassword(email, password);
    } catch(error) {
       print(error);
    }
    
    // Login with username
    try {
       String loginToken = await Meteor.loginWithPassword(username, password);
    } catch(error) {
       print(error);
    }
  2. Login with token

    try {
       String token = await Meteor.loginWithToken(loginToken);
    } catch(error) {
       print(error);
    }
  3. Login with Google

    // `email` to register with. Must be fetched from the Google oAuth API
    // The unique Google `userId`. Must be fetched from the Google oAuth API
    // `authHeaders` from Google oAuth API for server side validation
    try {
       String token = await Meteor.loginWithGoogle(email, userId, authHeaders)
    } catch(error) {
       print(error);
    }

    Install google_sing_in package

    dependencies:
       flutter:
          sdk: flutter
       
       google_sign_in: ^4.4.4
    import 'package:google_sign_in/google_sign_in.dart';
    
    GoogleSignIn _googleSignIn = GoogleSignIn(
     scopes: ['profile', 'email', 'openid'],
    );
    
    Future<void> _loginWithGoogle(context) async {
     try {
       var info = await _googleSignIn.signIn();
       var authHeaders = await info.authHeaders;
       var result = await Meteor.loginWithGoogle(info.email, info.id, authHeaders);
      
       print(result);
     } catch (error) {
       print(error);
     }
    }
  4. Login with Facebook

    // [userId] the unique Facebook userId. Must be fetched from the Facebook Login API
    // [token] the token from Facebook API Login for server side validation
    try {
       String token = await Meteor.loginWithFacebook(userId, token)
    } catch(error) {
       print(error);
    }

    Install flutter_facebook_login package

    dependencies:
       flutter:
          sdk: flutter
       
       flutter_facebook_login: ^3.0.0
    import 'package:flutter_facebook_login/flutter_facebook_login.dart';
    
    Future<void> _loginWithFacebook(context) async {
       final result = await facebookLogin.logIn(['email, public_profile']);
    
       switch (result.status) {
       case FacebookLoginStatus.loggedIn:
          var userId = result.accessToken.userId;
          var token = result.accessToken.userId;
          var res = await Meteor.loginWithFacebook(userId, token);
          
          print(res);
          break;
       case FacebookLoginStatus.cancelledByUser:
          print(':/');
          break;
       case FacebookLoginStatus.error:
          print('error: ${result.errorMessage}');
          break;
       }
    }
  5. Login with Apple

    // [userId] the unique Apple userId. Must be fetch from the Apple Login API
    // [jwt] the jwt from Apple API Login to get user's e-mail
    // [givenName] user's given Name. Must be fetched from the Apple Login API
    // [lastName] user's last Name. Must be fetched from the Apple Login API
    try {
       String token = await Meteor.loginWithApple(userId, jwt, givenName, lastName)
    } catch(error) {
       print(error);
    }

    Install apple_sign_in package

    dependencies:
       flutter:
          sdk: flutter
       
       apple_sign_in: ^0.1.0
 import 'package:apple_sign_in/apple_sign_in.dart';

 Future<void> _loginWithApple(context) async {
  try {
    final AuthorizationResult result = await AppleSignIn.performRequests([
      AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
    ]);

    switch (result.status) {
      case AuthorizationStatus.authorized:
        var userId = result.credential.user;
        var jwt = result.credential.identityToken;
        var givenName = result.credential.fullName.givenName;
        var lastName = result.credential.fullName.familyName;

        var res = await Meteor.loginWithApple(userId, jwt, givenName, lastName);
       
        print(res);
        break;
      case AuthorizationStatus.error:
        print('Erro: ${result.error.localizedDescription}');
        break;
      case AuthorizationStatus.cancelled:
        print(':/');
        break;
    }
  } catch (error) {
    print(error);
  }
}
  1. Change Password (need to be logged in)

    bool result = await Meteor.changePassword(oldPassword, newPassword);
  2. Forgot Password

    bool result = await Meteor.forgotPassword(email);
  3. Reset Password

    bool result = await Meteor.resetPassword(resetToken, newPassword);
  4. Logout

await Meteor.logout();
  1. Get logged in userId
String userId = Meteor.currentUserId;
  1. Check if logged in
bool isLoggedIn = Meteor.isLoggedIn();
  1. Get current user as map
Map<String,dynamic> currentUser = await Meteor.userAsMap();

Call Custom Methods

Defining custom methods in meteor server

export const helloWorld = new ValidatedMethod({
  name: 'hello',
  validate: new SimpleSchema({
    firstname: {type: String},
    lastname: {type: String},
  }).validator(),
  run({ firstname,lastname }) {
    const message = "hello "+ firstname + " " + lastname;
    console.log(message);
    return message;
  },
});

Invoking custom methods

try{
  var result = await Meteor.call('hello',[{'firstname':'Wendell','lastname':'Rocha'}]);
  print(result);
}catch(error){
  print(error);
}