leaonline:ddp-login

v2.0.2Published 6 months ago

DDP Login

Provides loginWithLea to a DDP connection. Requires custom Oauth2 server. Isomorphic, handles server-side remote connections, too.

Installation and usage

Install via meteor add leaonline:ddp-connection and use as the following:

Server example

You need a valid lea user account. See leaonline-accounts for how it works.

If you connect within a Meteor method, you can get the userId via this.userId

1import { DDP } from 'meteor/ddp-client'
2
3const log = (...args) => console.log('[DDP]:', ...args) 
4
5DDP.onReconnect(function (connection) {
6  log('connected to', connection._stream.endpoint)
7  
8  const user = Meteor.users.findOne(userId) // get the userId your way
9  const { accessToken } = user.services.lea
10  
11  DDP.loginWithLea(connection, { accessToken }, function(error, result) {
12    if (error) {
13      log('error - ', error.name, error.message)
14    }
15    
16    if (result) {
17      log('logged in')
18      // run authentication-requiring contexts
19    }
20  })
21})
22
23
24DDP.connect('http://localhost:8080') // some external Meteor app running on 8080

Client example

On the client you need a way to retrieve the accessToken from the server:

1Meteor.methods({
2  getToken: function() {
3    const { userId } = this
4    const user = Meteor.users.findOne(userId)
5    
6    return {
7      accessToken: user?.services?.lea?.accessToken
8    }
9  }
10})
1import { DDP } from 'meteor/ddp-client'
2
3const log = (...args) => console.log('[DDP]:', ...args)
4
5DDP.onReconnect(function (connection) {
6  log('connected to', connection._stream.endpoint)
7
8  Meteor.call('getToken', (error, { accessToken }) => {
9    if (error) return log('error - ', error.name, error.message)
10
11    DDP.loginWithLea(connection, { accessToken }, function (error, result) {
12      if (error) {
13        log('error - ', error.name, error.message)
14      }
15
16      if (result) {
17        log('logged in')
18        // run authentication-requiring contexts
19      }
20    })
21  })
22})
23
24DDP.connect('http://localhost:8080') // some external Meteor app running on 8080

You can also call the functions without a callback, which makes them return Promises.

License

MIT, see license file