Coder Social home page Coder Social logo

auth-callback.js's Introduction

auth-callback.js

GitHub API authentication using a callback method

@latest Build Status

Usage

Browsers

Load @octokit/auth-callback directly from cdn.pika.dev

<script type="module">
  import { createCallbackAuth } from "https://cdn.pika.dev/@octokit/auth-callback";
</script>

Node

Install with npm install @octokit/auth-callback

const { createCallbackAuth } = require("@octokit/auth-callback");
let token;

const auth = createCallbackAuth({ callback: () => token });
await auth();
// {
//   type: 'unauthenticated'
// }
token = "secret123";
await auth();
// {
//   type: 'token',
//   token: 'secret123',
//   tokenType: 'oauth'
// }

createCallbackAuth(options)

The createCallbackAuth method accepts a single options parameter

name type description
options.callback function Required. A method that returns or resolves with a token string.

auth()

The async auth() method does not accept any arguments

Authentication object

The async auth() method resolves to one of two possible authentication objects

  1. Unauthenticated if the callback() returns or resolves a falsy value
  2. Token authentication if the callback() returns or resolves with a string value

Unauthenticated

name type description
type string "unauthenticated"

Token authentication

name type description
type string "token"
token string The personal access token
tokenType string

One of:

  1. "oauth" (if returned string is an OAuth or personal access tokens)
  2. "installation" (if returned string is an installation access tokens)
  3. "app" (if returned string is a JSON Web Token (JWT) for GitHub App authentication)

auth.hook(request, route, parameters) or auth.hook(request, options)

auth.hook() hooks directly into the request life cycle. It amends the request to authenticate correctly based on the request URL.

The request option is an instance of @octokit/request. The route/options parameters are the same as for the request() method.

auth.hook() can be called directly to send an authenticated request

const { data: user } = await auth.hook(request, "GET /user");

Or it can be passed as option to request().

const requestWithAuth = request.defaults({
  request: {
    hook: auth.hook,
  },
});

const { data: user } = await requestWithAuth("GET /user");

Contributing

See CONTRIBUTING.md

License

MIT

auth-callback.js's People

Contributors

gr2m avatar nickfloyd avatar oscard0m avatar renovate[bot] avatar wolfy1339 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.