forked from MLH/mlh-hackathon-nodejs-starter
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add service for github authentication
- Loading branch information
Showing
8 changed files
with
1,740 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,17 +1,36 @@ | |||
const express = require("express"); | const express = require("express"); | ||
|
|
||
const config = require("../../config"); | |||
const User = require("../models/user"); | |||
const GitHub = require("../services/github"); | |||
|
|||
const router = express.Router(); | const router = express.Router(); | ||
|
|
||
router.get("/logout", function(req, res) { | router.get("/logout", function(req, res) { | ||
// TODO | // TODO | ||
}); | }); | ||
|
|
||
router.get("/login/github", function(req, res) { | router.get("/login/github", function(req, res) { | ||
// TODO | const github = new GitHub(config.githubClientId, config.githubClientSecret); | ||
res.redirect(github.authorization_url("public_repo")); | |||
}); | }); | ||
|
|
||
router.get("/callback/github", function(req, res) { | router.get("/callback/github", async function(req, res) { | ||
// TODO | if (!req.query.code) { | ||
return res.render("500"); | |||
} | |||
|
|||
// Fetch user from GitHub OAuth and store in session | |||
const github = new GitHub(config.githubClientId, config.githubClientSecret); | |||
const access_token = await github.get_token(req.query.code); | |||
|
|||
if (!access_token) { | |||
return res.render("404"); | |||
} | |||
|
|||
const user = User.find_or_create_from_token(access_token); | |||
|
|||
return res.redirect("/"); | |||
}); | }); | ||
|
|
||
module.exports = router; | module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,21 @@ | |||
const GitHub = require("../services/github"); | |||
|
|||
class User { | |||
constructor(username, avatar_url, github_id) { | |||
this.username = username; | |||
this.avatar_url = avatar_url; | |||
this.github_id = github_id; | |||
} | |||
|
|||
static async find_or_create_from_token(access_token) { | |||
try { | |||
const data = await GitHub.get_user_from_token(access_token); | |||
} catch (error) { | |||
console.log("ERROR", error); | |||
} | |||
|
|||
/* Find existing user or create new User instances */ | |||
} | |||
} | |||
|
|||
module.exports = User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,41 @@ | |||
const axios = require("axios"); | |||
|
|||
const api_url = "https://api.github.com"; | |||
const authorize_url = "https://github.com/login/oauth/authorize"; | |||
const token_url = "https://github.com/login/oauth/access_token"; | |||
|
|||
class GitHub { | |||
constructor(client_id = "", client_secret = "", access_token = "") { | |||
this.client_id = client_id; | |||
this.client_secret = client_secret; | |||
this.access_token = access_token; | |||
} | |||
|
|||
authorization_url(scope = "") { | |||
return `${authorize_url}?client_id=${this.client_id}&client_secret=${this.client_secret}&scope=${scope}`; | |||
} | |||
|
|||
async get_token(code) { | |||
/* Fetch GitHub Access Token for GitHub OAuth */ | |||
const config = { headers: { Accept: "application/json" } }; | |||
const params = { | |||
code, | |||
client_id: this.client_id, | |||
client_secret: this.client_secret | |||
}; | |||
|
|||
const { data } = await axios.post(token_url, params, config); | |||
return data.access_token; | |||
} | |||
|
|||
static async get_user_from_token(access_token) { | |||
/* Fetch user data using the access token. */ | |||
const url = api_url + "/user"; | |||
const config = { params: { access_token: access_token } }; | |||
|
|||
const response = await axios.get(url, config); | |||
return response.data; | |||
} | |||
} | |||
|
|||
module.exports = GitHub; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,10 @@ | |||
const dotenv = require("dotenv"); | |||
|
|||
dotenv.config({ path: ".env" }); | |||
|
|||
module.exports = { | module.exports = { | ||
port: process.env.PORT || 5000, | port: process.env.PORT || 5000, | ||
env: process.env.NODE_ENV || "development" | env: process.env.NODE_ENV || "development", | ||
githubClientId: process.env.GITHUB_CLIENT_ID || "", | |||
githubClientSecret: process.env.GITHUB_CLIENT_SECRET || "" | |||
}; | }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.