@dgcode/auth
TypeScript icon, indicating that this package has built-in type declarations

0.1.42 • Public • Published

@dgcode/auth

authentication helper for google apis

Install

$ npm install @dgcode/auth

Usage

While Google documents its Node.JS setup for API calls fairly well, the whole process can quickly become annoying when the same setup code needs to be replicated across multiple side-projects. This library provides a one-liner to achieve the same results.

Basic authorization

import { authorize } from '@dgcode/auth';

authorize({
  credentials: 'my-keys-folder/google-secret.json',
  scopes: [ 'admin.directory' ]
}).then(auth => {

  // `auth` is a Google OAuth2Client instance

});

Authentication

The following impersonates a user with help of a service account:

import { authenticate } from '@dgcode/auth';

authenticate({
  email: 'someuser@gmail.com',
  key: 'my-keys-folder/service-account.json',
  scopes: [ 'gmail.settings' ]
}).then(auth => {

  // `auth` is a Google JWT instance

});

Post-auth usage

Once an auth has been determined (using one of the above setups), you can pass it to send authorized requests via the googleapis module (conveniently re-exported by this library):

import { authenticate, google } from '@dgcode/auth';

authorize({
  credentials: 'my-keys-folder/google-secret.json',
  scopes: [ 'admin.directory.user' ]
}).then(auth => {

  const userKey = 'someuser@gmail.com';
  return google.admin('directory_v1').users.get({ auth, userKey });

}).then(res => {
  const user = res.data;
  // `user` is a User resource object provided by Google APIs
});

With a Client manager

All processes above can be even more conveniently executed with help of a centralized Client instance:

import { Client } from '@dgcode/auth';

async function getGoogleUser(email) {

  const client = new Client();

  await client.authorize({
    credentials: 'my-keys-folder/google-secret.json',
    scopes: [ 'admin.directory.user' ]
  });

  const userKey = email; 
  const { data: user } = await client.service('admin(directory_v1)').users.get({
    userKey
  });

  return user;

}

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @dgcode/auth

Weekly Downloads

0

Version

0.1.42

License

MIT

Unpacked Size

81.3 kB

Total Files

27

Last publish

Collaborators

  • dgcoder