-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f19554
commit 0c539e0
Showing
6 changed files
with
167 additions
and
70 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 | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,109 @@ | ||
const express = require("express"); | ||
const axios = require("axios"); | ||
const mongoose = require("mongoose"); | ||
|
||
const app = express(); | ||
require("dotenv").config(); | ||
const PORT = process.env.PORT || 3000; | ||
|
||
mongoose.connect( | ||
`mongodb+srv://fetchcv:${process.env.MONGODB_PASSWORD}@cluster0.e1en0n4.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`, | ||
); | ||
|
||
const userSchema = new mongoose.Schema({ | ||
githubId: String, | ||
}); | ||
|
||
const User = mongoose.model("User", userSchema); | ||
|
||
// Serve static files from the "public" folder | ||
app.use(express.static("public")); | ||
app.set("view engine", "ejs"); | ||
app.set("views", __dirname + "/public"); | ||
|
||
// Define routes and middleware here | ||
|
||
app.get("/", (req, res) => { | ||
res.sendFile(__dirname + "/public/index.html"); | ||
res.sendFile(__dirname + "/public/index.html"); | ||
}); | ||
|
||
app.get("/token/:service", (req, res) => { | ||
const service = req.params.service; | ||
console.log( | ||
service.toUpperCase() + "_TOKEN", | ||
process.env[service.toUpperCase() + "_TOKEN"], | ||
); | ||
res.send(process.env[service.toUpperCase() + "_TOKEN"] || "No token found"); | ||
const service = req.params.service; | ||
console.log( | ||
service.toUpperCase() + "_TOKEN", | ||
process.env[service.toUpperCase() + "_TOKEN"], | ||
); | ||
res.send(process.env[service.toUpperCase() + "_TOKEN"] || "No token found"); | ||
}); | ||
|
||
// Define routes and middleware here | ||
// GitHub OAuth | ||
const clientID = process.env.GITHUB_CLIENT_ID; | ||
const clientSecret = process.env.GITHUB_CLIENT_SECRET; | ||
let github_access_token = "not logged in"; | ||
let github_user_data = {}; | ||
|
||
function loggedIn() { | ||
if (github_access_token === "not logged in") { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
app.get("/gh", (req, res) => { | ||
res.render("pages/index", { client_id: clientID }); | ||
}); | ||
|
||
// Callback | ||
app.get("/auth/github", (req, res) => { | ||
// The req.query object has the query params that were sent to this route. | ||
const requestToken = req.query.code; | ||
|
||
axios({ | ||
method: "post", | ||
url: `https://github.com/login/oauth/access_token?client_id=${clientID}&client_secret=${clientSecret}&code=${requestToken}`, | ||
// Set the content type header, so that we get the response in JSON | ||
headers: { | ||
accept: "application/json", | ||
}, | ||
}).then((response) => { | ||
github_access_token = response.data.access_token; | ||
res.redirect("/github/login"); | ||
}); | ||
}); | ||
|
||
app.get("/github/login", function (req, res) { | ||
axios({ | ||
method: "get", | ||
url: `https://api.github.com/user`, | ||
headers: { | ||
Authorization: "token " + github_access_token, | ||
}, | ||
}).then((response) => { | ||
github_user_data = response.data; | ||
githubOAuthLogin(res); | ||
}); | ||
}); | ||
|
||
async function githubOAuthLogin(res) { | ||
let isAccount = await githubOAuthUserExists(github_user_data.id); | ||
if (isAccount) res.render("pages/success", { userData: github_user_data }); | ||
else createGithubOAuthUser(github_user_data.id, res); | ||
} | ||
|
||
function createGithubOAuthUser(githubId, res) { | ||
const user = new User({ githubId: githubId }); | ||
user.save().then((result) => { | ||
console.log("id is " + result.id); | ||
res.render("pages/new-user", { userData: github_user_data }); | ||
}); | ||
} | ||
|
||
async function githubOAuthUserExists(githubId) { | ||
const user = await User.findOne({ githubId: githubId }); | ||
console.log(user !== null); | ||
return user !== null; | ||
} | ||
|
||
// Connect app | ||
app.listen(PORT, () => { | ||
console.log("Server is running on port 3000"); | ||
console.log("Server is running on port " + PORT); | ||
}); |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Github OAuth</title> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <!-- load fontawesome --> | ||
<style> | ||
body { padding-top:70px; } | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="jumbotron text-center text-primary"> | ||
<h1><span class="fa fa-github"></span> Github OAuth</h1> | ||
<p>Authorize your app with:</p> | ||
<a href="https://github.com/login/oauth/authorize?client_id=<%= client_id %>" class="btn btn-danger"><span class="fa fa-github"></span> Github Login</a> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Github OAuth</title> | ||
</head> | ||
<body> | ||
<p> | ||
<strong>Welcome to FetchCV, </strong> <%= userData.name %>!<br> | ||
</p> | ||
</body> | ||
</html> |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Github OAuth</title> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <!-- load fontawesome --> | ||
<style> | ||
body { padding-top:70px; } | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="jumbotron"> | ||
<h1 class="text-primary text-center"><span class="fa fa-github"></span> Github Information</h1> | ||
<div class="row"> | ||
<div class="col-sm-6"> | ||
<div class="well"> | ||
<p> | ||
<strong>Name</strong>: <%= userData.name %><br> | ||
<strong>Username</strong>: <%= userData.login %><br> | ||
<strong>Company</strong>: <%= userData.company %><br> | ||
<strong>Bio</strong>: <%= userData.bio %> | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
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