npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

meteor-u5auth

v0.0.4

Published

Meteor package to authenticate against a OAuth2 provider

Downloads

16

Readme

OAuth2 Authentication for Meteor, With Your Own Provider

Add Oauth2 authentication to your Meteor app, supports Meteor 1.5+

Setup

  1. Add required dependencies:
meteor add accounts-base accounts-oauth service-configuration \
  random oauth oauth2 http
meteor npm install meteor-u5auth
  1. Configure Service

Add (or update) a service configuration for the u5auth service (on the server side only, so keep it in the ./server directory):

import { getLiveToken, setU5AuthDebug } from 'meteor-u5auth'

Meteor.startup(() => {

  if ()
  ServiceConfiguration.configurations.upsert({
    service: 'u5auth'
  }, {
    $set: {
      clientId: process.env.OAUTH2_ID || 'your-client-id',
      secret: process.env.OAUTH2_SECRET || 'your-client-secret',
      issuer: process.env.OAUTH2_SITE || 'https://my-oauth2-service.com',
      requestPermissions: [ 'email', 'userinfo', 'phone_number', 'sub' ],
      ttl: 60 /* minutes */ * 60 /* seconds */
    }
  })

})
  • clientId and secret must be issued by your OAuth2 provider.
  • issuer is the url pointing at your auth provider.
  • Ensure the ttl is in line with the expiry of your tokens, 1 hour in the code snippet above. The ttl will be used to get a new token, if 90% of the ttl have passed.
  • requestPermissions are the scopes you request with the token.
  • It is good practice to keep live/production secrets out of the code. Therefore, you may want to use the approach using environment variables demonstrated above (secret: process.env.OAUTH2_SECRET || 'test-secret').

Usage

See also the example app.

Logging In

On the client, use Meteor.loginWithU5Auth():

Meteor.loginWithU5Auth({}, err => {
  if (err) {
    throw err
  }
})

Logging Out

Use Meteor.logout:

Meteor.logout()

This may not destroy your session at the auth provider. As a result, the next login attempt may authorize you without prompting for (another) user/password. In order to fully log out, you may have to make an additional call (via Ajax?) to your auth provider.

Get Token

The OAuth2 token received during login can be used to make calls to other APIs or services that support your auth provider. The token can be used both client and server side like this:

import { getLiveToken } from 'meteor-u5auth'

function someFunction() {
  getLiveToken().then(token => {
    // now use the token
    ...
  })
}

Only use getLiveToken when you know the user is logged in. If in doubt, check e.g. if Meteor.user() is available.

getLiveToken will ensure that the token is not expired, i.e. it will refresh the access token (via the refresh token, if available) before resolving the promise.

Userinfo

The userinfo provided by the auth provider is available as:

Meteor.user().profile

Refresh Userinfo

In order to ensure userinfo is up-to-date, call refreshUserinfo:

import { refreshUserinfo } from 'meteor-u5auth'

function someFunction() {
  refreshUserinfo().then(() => console.log('refreshed'))
}

Now Meteor.user().profile will have updated details from the auth provider.

Debug

In order to debug anything related to this package, call setU5AuthDebug on the server:

import { setU5AuthDebug } from 'meteor-u5auth'

Meteor.startup(() => {
  setU5AuthDebug()
}

The server logs will now contain a detailed log under a prefix. Beware, though, that access tokens and refresh tokens are then logged in verbatim, which may be a security risk.