https://github.com/leaonline/ddp-login
Provide loginWithLea to a DDP connection.
https://github.com/leaonline/ddp-login
connection ddp meteor microservices oauth2
Last synced: 4 months ago
JSON representation
Provide loginWithLea to a DDP connection.
- Host: GitHub
- URL: https://github.com/leaonline/ddp-login
- Owner: leaonline
- License: mit
- Created: 2019-10-14T15:32:09.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-03T11:02:15.000Z (almost 2 years ago)
- Last Synced: 2025-01-16T04:14:18.709Z (over 1 year ago)
- Topics: connection, ddp, meteor, microservices, oauth2
- Language: JavaScript
- Size: 18.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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`
```javascript
import { DDP } from 'meteor/ddp-client'
const log = (...args) => console.log('[DDP]:', ...args)
DDP.onReconnect(function (connection) {
log('connected to', connection._stream.endpoint)
const user = Meteor.users.findOne(userId) // get the userId your way
const { accessToken } = user.services.lea
DDP.loginWithLea(connection, { accessToken }, function(error, result) {
if (error) {
log('error - ', error.name, error.message)
}
if (result) {
log('logged in')
// run authentication-requiring contexts
}
})
})
DDP.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:
```javascript
Meteor.methods({
getToken: function() {
const { userId } = this
const user = Meteor.users.findOne(userId)
return {
accessToken: user?.services?.lea?.accessToken
}
}
})
```
```javascript
import { DDP } from 'meteor/ddp-client'
const log = (...args) => console.log('[DDP]:', ...args)
DDP.onReconnect(function (connection) {
log('connected to', connection._stream.endpoint)
Meteor.call('getToken', (error, { accessToken }) => {
if (error) return log('error - ', error.name, error.message)
DDP.loginWithLea(connection, { accessToken }, function (error, result) {
if (error) {
log('error - ', error.name, error.message)
}
if (result) {
log('logged in')
// run authentication-requiring contexts
}
})
})
})
DDP.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](./LICENSE)