# @dgcode/auth

> authentication helper for google apis

Latest version **0.1.42** (published 2019-01-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @dgcode/auth
pnpm add @dgcode/auth
yarn add @dgcode/auth
bun add @dgcode/auth
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.42 |
| Published | 2019-01-09 |
| First published | 2018-11-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 79.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Aurelien Monasse |
| Maintainers | dgcoder |

## Links

- npm: https://www.npmjs.com/package/@dgcode/auth
- npm.io page: https://npm.io/package/@dgcode/auth

## Dependencies (5)

- [opn](https://npm.io/package/opn.md) ^5.4.0
- [chalk](https://npm.io/package/chalk.md) ^2.4.1
- [debug](https://npm.io/package/debug.md) ^4.1.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^7.0.0
- [googleapis](https://npm.io/package/googleapis.md) ^34.0.0

## Recent versions

- 0.1.42 (latest) — 2019-01-09
- 0.1.41 — 2019-01-07
- 0.1.16 — 2018-12-21
- 0.1.14 — 2018-12-19
- 0.1.13 — 2018-12-19
- 0.1.6 — 2018-12-17
- 0.1.3 — 2018-11-07
- 0.1.1 — 2018-11-06

## README

# `@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](https://developers.google.com/sheets/api/quickstart/nodejs), 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](https://www.npmjs.com/package/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

---
_Source: https://npm.io/package/@dgcode/auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
