Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor for Zeit 2.0 #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## [1.1.0] - 2019-08-07
### Added
- Refactor to support Zeit Now's 2.0 routing system.

## [1.0.0] - 2017-04-01
### Added
- Support for state parameter to protect against cross-site request forgery attacks. Requires users to use the provided `/login` url and moves redirect url from `/` to `/callback`
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ When authentication was successful, the user will be redirected to the `REDIRECT

### Finish setup

To make this work you have to set the authorization callback URL of [your application on GitHub](https://github.com/settings/developers) to whatever URL `now` gave you plus the path `/callback` e.g. `http://localhost:3000/callback`:
To make this work you have to set the authorization callback URL of [your application on GitHub](https://github.com/settings/developers) to whatever URL `now` gave you plus the path `/api/callback` e.g. `http://localhost:3000/api/callback`:

![Authorization callback URL: 'your-url.now.sh'](https://cloud.githubusercontent.com/assets/168870/24585953/9543e03a-178e-11e7-8f10-07be5c10682c.png)
![Authorization callback URL: 'your-url.now.sh'](https://user-images.githubusercontent.com/174475/62680084-3e566780-b96b-11e9-9fb8-986da2356abe.png)

To log people in provide a link to url `now` gave you plus the path `login` e.g. `http://localhost:3000/login` when they click on the link it will redirect to `https://github.com/login/oauth/authorize?client_id=asdf123&state`. (where `client_id` is your GitHub app client id in `.env` and `state` is a randomly generated string). This will redirect them to the GitHub sign in page for your app, which looks like this:
To log people in provide a link to url `now` gave you plus the path `/api/login` e.g. `http://localhost:3000/api/login` when they click on the link it will redirect to `https://github.com/login/oauth/authorize?client_id=asdf123&state`. (where `client_id` is your GitHub app client id in `.env` and `state` is a randomly generated string). This will redirect them to the GitHub sign in page for your app, which looks like this:

![Authorize my app to access your data on GitHub](https://cloud.githubusercontent.com/assets/7525670/22627265/fc50c680-ebbf-11e6-9126-dcdef37d3c3d.png)

Expand Down
19 changes: 12 additions & 7 deletions index.js → api/[action].js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require('dotenv').config()
const querystring = require('querystring')
const axios = require('axios')
const { router, get } = require('microrouter');
const querystring = require('querystring');
const axios = require('axios');
const redirect = require('micro-redirect');
const uid = require('uid-promise');

Expand Down Expand Up @@ -66,7 +65,13 @@ const callback = async (req, res) => {
}
}

module.exports = router(
get('/login', login),
get('/callback', callback)
);
// api/[action].js
// https://micro-github.*USERNAME*.now.sh/api/login
// https://micro-github.*USERNAME*.now.sh/api/callback
module.exports = (req, res) => {
if(req.query.action === "login"){
login(req, res);
}else if(req.query.action === "callback"){
callback(req, res);
}
}
11 changes: 11 additions & 0 deletions now.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name":"micro-github",
"version": 2,
"build": {
"env": {
"GH_CLIENT_ID": "@gh-client-id",
"GH_CLIENT_SECRET": "@gh-client-secret",
"REDIRECT_URL": "@redirect-url"
}
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "micro-github",
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -23,7 +23,6 @@
"dotenv": "^4.0.0",
"micro": "^6.2.1",
"micro-redirect": "^1.0.0",
"microrouter": "^2.0.1",
"uid-promise": "^0.1.0"
},
"devDependencies": {
Expand Down