Code Monkey home page Code Monkey logo

oauth2-using-github's Introduction

GitHub OAuth Example

This is an example Node.js application that demonstrates how to use GitHub's OAuth API to authenticate and fetch data about a user.

Prerequisites

Before running this application, you'll need to create a GitHub OAuth App and obtain a client ID and secret. You can do this by following the instructions in the GitHub documentation.

Getting Started

  1. Clone the repository:

    git clone https://github.com/your-username/your-repo.git

Go to the project directory:

cd your-repo Install the dependencies:

npm install Set the following environment variables:

CLIENT_SECRET: the client secret of your GitHub OAuth App. CLIENT_ID: the client ID of your GitHub OAuth App. REDIRECT_URI: the redirect URI of your GitHub OAuth App. SCOPE: the scopes that your GitHub OAuth App is requesting. Start the server:

npm start The server will start listening on http://localhost:3000.

API

The API provides the following endpoints:

  • GET /: returns the URL to the GitHub OAuth authorize page.
  • GET /redirect?code=<code>: exchanges the authorization code for an access token and sets a session cookie with the access token.
  • GET /profile: returns the authenticated user's GitHub profile data.
  • GET /followers: returns the authenticated user's GitHub followers.
  • GET /repos: returns the authenticated user's GitHub repositories.

Authentication

The server uses the GitHub OAuth API to authenticate users. The GET / endpoint returns the URL to the GitHub OAuth authorize page, where the user can authorize the application to access their GitHub data. After the user authorizes the application, they are redirected to the GET /redirect endpoint, which exchanges the authorization code for an access token and sets a session cookie with the access token.

The GET /profile, GET /followers, and GET /repos endpoints require the user to be authenticated. The server checks for the presence of the session cookie and uses the access token stored in the cookie to authenticate the user. If the user is not authenticated, the server returns an error with the message "Unauthorized".

oauth2-using-github's People

Contributors

omar3ain avatar

Watchers

 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.