Giter VIP home page Giter VIP logo

hapi-auth-github's Introduction

hapi-auth-github makes it easy to login with github

GitHub Authentication Plugin for Hapi.js Apps with detailed documentation.

Build Status Test Coverage JavaScript Style Guide Code Climate Dependency Status devDependencies Status

Why?

We use GitHub for all our coding projects and are building a tool to keep track of all them: https://github.com/dwyl/tudo

Given that other people will have projects that need GitHub Authentication,
we have de-coupled our OAuth code into this re-useable Hapi Plugin.

If you have any questions or would like to contribute to this module, please get in touch: Join the chat at https://gitter.im/dwyl/chat

What?

An easy-to-use Hapi.js plugin that gives you GitHub OAuth Authentication
in a few simple steps and has human-readable, maintained code.

Note: if you are new to Hapi check out: https://github.com/dwyl/learn-hapi

How?

If you're new to GitHub Authentication, and want to understand how it works, read the GitHub OAuth Web Application flow:
https://developer.github.com/v3/oauth/#web-application-flow

Or, if you just need to get up and running fast, follow these simple steps:

1. Install hapi-auth-github from NPM

Install the plugin from npm and save it to your package.json:

npm install hapi-auth-github --save

2. Create an App on GitHub

Follow the instructions in: GITHUB-APP-STEP-BY-STEP-GUIDE.md

3. Export the Required Environment Variables

Once you've created your app following the GitHub App Step-by-Step Guide

Export the Environment Variables:

BASE_URL=http://localhost:8000 # same as Authorized JavaScript Origin
GITHUB_CLIENT_ID=YourGitHubClientID
GITHUB_CLIENT_SECRET=SuperSecret
GITHUB_AUTH_REDIRECT_URL=/githubauth
PORT=8000

# Optionals
# (If you are using hapi-auth-jwt2)
JWT_SECRET=ItsNoSecretBecauseYouToldEverybody
# If you are using custom instance of GitHub
GITHUB_HOSTNAME=github.mycompany.com
GITHUB_API_HOSTNAME=api.github.mycompany.com

Notes on Environment Variables:

Tip: If you (or anyone on your team) are new to Environment Variables or need a refresher,
see: https://github.com/dwyl/learn-environment-variables

We named/exported the 5 variables prefixed with GITHUB_ to distinguish them from other services you may be using which may also have an environment variable named CLIENT_ID...

The BASE_URL is required to know which url your app is using. it needs to be identical to the Authorized JavaScript Origin that you set in step 2 above.

The GITHUB_AUTH_REDIRECT_URL is the url (endpoint) where GitHub will send the initial OAuth2 code to confirm your application is real. Make sure that the url is identical to the one you defined when setting up your app on GitHub. e.g: http://localhost:8000/githubauth

The GITHUB_HOSTNAME and GITHUB_API_HOSTNAME let's you define other instance of GitHub e.g. enterprise. Defaults are github.com and api.github.com accordingly.

4. Create Your (Custom) Handler Function

This is where you decide what to do with the person's profile details
once they have authorized your App to use their GitHub details.

Your custom handler should have the following signature:

function custom_handler(request, reply, tokens, profile) {
  // save the profile as a session so you can personalize their experience of your app
  // use the reply() to send a response/view to the visitor
}

The handler function parameters are:

  • request is the hapi request object with all the properties.
  • reply is the standard hapi reply object used to send your response to the client or send a rendered view.
  • tokens are the OAuth2 tokens returned by GitHub for the session see: sample_auth_token.json
  • profile is the person's GitHub profile see: sample_profile.json

For an example custom_handler, see: example/github_oauth_handler.js

5. Register the Plugin into your Hapi.js Server

The final step is to register the plugin into your Hapi.js Server declaring your desired options:

// declare your desired options for the plugin
var opts = {
  handler: require('./github_oauth_handler.js'), // your handler
  SCOPE: 'user' // ask for their public email address
};

server.register([{ register: require('hapi-auth-github'), options:opts }],
 function (err) {
  if(err){
    // handle the error if the plugin failed to load:  
  }
  // the rest of your app ...
});

options explained

  • handler - the handler you defined above in step 4 which is your custom logic for GitHub auth enabled app.
  • SCOPE - these are the permissions your app is requesting.

Implementation Notes:

To run the example you will need an extra environment variable:

BASE_URL=http://localhost:8000 # same as Authorized JavaScript Origin
GITHUB_CLIENT_ID=YourGitHubClientID
GITHUB_CLIENT_SECRET=SuperSecret
GITHUB_AUTH_REDIRECT_URL=/githubauth
PORT=8000
# Optionally (If you are using hapi-auth-jwt2)
JWT_SECRET=ItsNoSecretBecauseYouToldEverybody

Also, if you're wondering what that JWT_SECRET Environment Variables is for,
see: https://github.com/dwyl/learn-json-web-tokens

Background Reading

hapi-auth-github's People

Contributors

iteles avatar jrans avatar mac-s-g avatar nelsonic avatar pdziok avatar perborgen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hapi-auth-github's Issues

Broken link

In step 3, the step-by-step link is suppose to be the same as the one in step 2, right?
Currently, its broken, as GIITHUB has been swapped with GOOGLE.

**
3. Export the Required Environment Variables

Once you've created your app following the GOOGLE-APP-STEP-BY-STEP-GUIDE <---- (broken link)

Let user override GitHub URL

I'm currently using GitHub Enterprise which is hosted on a different host than github.com.

I'd like to be able to override hosts that are currently hardcoded but keep default value if nothing is provided.

Add Simple Example?

Is it enough to point people towards _Tudo_ or do people want a simpler example?

Update (devDependencies) and improve readme where possible

Sadly, the devDependencies have aged on this ... https://david-dm.org/dwyl/hapi-auth-github?type=dev

hapi-auth-github-devdependencies

We need a quick PR to update them. and... if you spot anything unclear in the Readme/Instructions, please update/improve. (thanks!)

Why is codecov badge "unknown"...? can you please investigate? ๐Ÿ˜•
We prefer to use the shields.io version in any case. see the table: https://github.com/dwyl/technology-stack#open-source-projects-we-use

note: after that we will also need to update: https://github.com/nelsonic/hapi-auth-github-example ...

Create Elixir version of this module?

As our tech stack has now evolved github.com/dwyl/technology-stack , I'd like to explore having this module as an elixir module rather than hapi.

The idea is for us to have a login module that we can pick up and run with for future projects without having to rebuild a login for every project.

The key is that the module is:

  • Fully tested
  • Has detailed documentation

This one is next: https://github.com/dwyl/hapi-auth-google

Error: Route missing path (when using latest version of Hapi)

While trying to update devDependencies (for the example/tests) to latest versions of Hapi & Friends for #17
I'm getting the following error when attempting to start the example server: Error: Route missing path
hapi-auth-github-error-route-missing-path

Trying to figure it out now... if anyone has come across it and knows the fix, please let me know! ๐Ÿ‘

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.