# oauthor

> Oauth2 client for nodejs

Latest version **1.0.1** (published 2015-10-17) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install oauthor
pnpm add oauthor
yarn add oauthor
bun add oauthor
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2015-10-17 |
| First published | 2015-10-17 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Adrian Carballo |
| Maintainers | adrianca |
| Keywords | oauth, oauth2, promise, auth, simple, powerful |

## Links

- npm: https://www.npmjs.com/package/oauthor
- Repository: https://github.com/adrianca/oauthor
- Homepage: https://github.com/adrianca/oauthor#readme
- Issues: https://github.com/adrianca/oauthor/issues
- npm.io page: https://npm.io/package/oauthor

## Dependencies (4)

- [deep-extend](https://npm.io/package/deep-extend.md) 0.4.0
- [querystring](https://npm.io/package/querystring.md) 0.2.0
- [request-promise](https://npm.io/package/request-promise.md) 1.0.1
- [continuation-local-storage](https://npm.io/package/continuation-local-storage.md) 3.1.4

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@oxyhq/services](https://npm.io/package/@oxyhq/services.md) — 2.3K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2015-10-17
- 1.0.0 — 2015-10-17

## README

# oauthor
OAuth2 client library for node

[![Travis](https://img.shields.io/travis/adrianca/oauthor.svg?style=flat-square)](https://travis-ci.org/adrianca/oauthor)
[![npm](https://img.shields.io/npm/v/oauthor.svg)](https://travis-ci.org/adrianca/oauthor)

## Get started

Install the oauthor package:

`npm install --save oauthor`

```js
    // Step 1: Create oauth config
    var oauthor = require('oauthor');
    var oauth = oauthor(
        'https://provider.com/oauth',
        'https://provider.com/access_token',
        'yourclientid',
        'yourclientsecret'
    );

    // Step 2: Perform authorize request and handle redirect
    var code = redirect(oauth.authorizeUrl({redirect_uri: '...'}));

    // Step 3: Get code from the redirect and exchange for access token.
    oauth.requestAccessToken(code).then(response => {
        // The provider should have sent an access token in the response
        var accessToken = response.access_token;    
        // Sign all subsequent requests to the provider.
        var client = oauth.sign(accessToken);

        // Use `client` now to make authorized requests.
        client({url: 'https://provider.com./me'}).then(user => ...)
    });
```

## Case study: Facebook

```js
    // Step 1: Create oauth config
    var oauthor = require('oauthor');
    var oauth = oauthor(
        'https://www.facebook.com/dialog/oauth',
        'https://graph.facebook.com/oauth/access_token',
        facebook_client_id,
        facebook_client_secret, 
        {
            params: {redirect_uri: 'https://myapp.com/redirect/facebook'}
        }
    );

    // Step 2: Perform authorize request
    var code = redirect(oauth.authorizeUrl({
        scope: 'public_profile'
    }));

    // Step 3: Get code from the redirect and exchange for access token.
    oauth.requestAccessToken(code).then(response => {
        // The provider should have sent an access token in the response
        var accessToken = response.access_token;    
        // Sign all subsequent requests to the provider.
        var client = oauth.sign(accessToken);

        // Use `client` now to make authorized requests.
        client({
            url: 'https://graph.facebook.com/me',
            qs: {fields: 'id,name,email,link,gender,locale,timezone'}
        }).then(profile => ...)
    });
```

## Use with generators

The oauthor package exposes a simple Promise api ready to be used with generators (using `co` and the likes).

```js
    // Step 1: Create oauth config
    var oauthor = require('oauthor');
    var oauth = oauthor(
        'https://provider.com/oauth',
        'https://provider.com/access_token',
        'yourclientid',
        'yourclientsecret'
    );

    // Step 2: Perform authorize request and handle redirect
    var code = redirect(oauth.authorizeUrl({redirect_uri: '...'}));

    // Step 3: Get code from the redirect and exchange for access token.
    var response = yield oauth.requestAccessToken(code);

    // The provider should have sent an access token in the response
    var accessToken = response.access_token;    
    // Sign all subsequent requests to the provider.
    var client = oauth.sign(accessToken);

    // Use `client` now to make authorized requests.
    var user = yield client({url: 'https://provider.com./me'});
```

## API

## `authorizeUrl`

Creates the url that should be used for authorization, with the specified
parameters as query string.

### Parameters

* `params` **`[Object]`** Parameters to send in the query      string. (optional, default `{}`)



Returns `String` The url that can be used to perform authorization.

## `requestAccessToken`

Performs a request to the access token endpoint that was specified, with
the code received from the authorization step, and optional parameters.

### Parameters

* `code` **`String`** The code obtained in the authorization step.
* `params` **`[Object]`** Parameters that will be posted to the request. (optional, default `{}`)



Returns `Object` The response from the provider.

## `sign`

Specifies an access token that should be used to sign all subsequent 
requests. The return value is a function that can be used to make 
authorized requests. The function takes an object specifying the request 
options that will be passed to the `request` function.
See [request](http://github.com/request/request#requestoptions-callback) for more info on the supported options.

### Parameters

* `accessToken` **`String`** The access token obtained from the provider.


### Examples

```js
    // Sign all requests with `youraccesscode`.
    var client = oauth.sign('youraccesscode');
    
    // Suppose you have the endpoint `https://provider.com/me` that
    // returns your user profile when queried.
    client({url: 'https://provider.com/me'}).then((user) => ...)
```

Returns `Function`

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