Skip to content

Commit

Permalink
GitHub OAuth (hidden)
Browse files Browse the repository at this point in the history
  • Loading branch information
hnasheralneam committed Jul 14, 2024
1 parent 2f19554 commit 0c539e0
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 70 deletions.
101 changes: 91 additions & 10 deletions app.js
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);
});
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/FetchCV/special-invention.git"
"url": "git+https://github.com/FetchCV/fetchcv.git"
},
"author": "Ave and Hamza",
"license": "GPL-3.0-or-later",
"bugs": {
"url": "https://github.com/FetchCV/special-invention/issues"
"url": "https://github.com/FetchCV/fetchcv/issues"
},
"homepage": "https://github.com/FetchCV/special-invention#readme",
"homepage": "https://github.com/FetchCV/fetchcv#readme",
"dependencies": {
"axios": "^1.7.2",
"dotenv": "^16.4.5",
"ejs": "^3.1.10",
"express": "^4.18.2",
"mongoose": "^8.5.1",
"node": "^21.6.2",
"tailwind": "^4.0.0"
},
Expand Down
19 changes: 19 additions & 0 deletions public/pages/index.ejs
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>
11 changes: 11 additions & 0 deletions public/pages/new-user.ejs
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>
29 changes: 29 additions & 0 deletions public/pages/success.ejs
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>
68 changes: 11 additions & 57 deletions public/styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,6 @@ video {
position: absolute;
}

.relative {
position: relative;
}

.-left-40 {
left: -10rem;
}
Expand All @@ -574,10 +570,6 @@ video {
bottom: 0px;
}

.right-2 {
right: 0.5rem;
}

.-z-10 {
z-index: -10;
}
Expand All @@ -600,21 +592,11 @@ video {
margin-bottom: 0.5rem;
}

.my-4 {
margin-top: 1rem;
margin-bottom: 1rem;
}

.my-auto {
margin-top: auto;
margin-bottom: auto;
}

.mx-auto {
margin-left: auto;
margin-right: auto;
}

.mb-20 {
margin-bottom: 5rem;
}
Expand Down Expand Up @@ -683,10 +665,6 @@ video {
width: 100vw;
}

.min-w-\[50vw\] {
min-width: 50vw;
}

.max-w-\[45vw\] {
max-width: 45vw;
}
Expand All @@ -699,18 +677,14 @@ video {
justify-content: center;
}

.gap-10 {
gap: 2.5rem;
.gap-16 {
gap: 4rem;
}

.gap-6 {
gap: 1.5rem;
}

.gap-16 {
gap: 4rem;
}

.rounded-full {
border-radius: 9999px;
}
Expand Down Expand Up @@ -839,31 +813,21 @@ video {
padding-bottom: 0.25rem;
}

.py-10 {
padding-top: 2.5rem;
padding-bottom: 2.5rem;
}

.py-2 {
padding-top: 0.5rem;
padding-bottom: 0.5rem;
}

.py-24 {
padding-top: 6rem;
padding-bottom: 6rem;
}

.py-4 {
padding-top: 1rem;
padding-bottom: 1rem;
}

.py-20 {
padding-top: 5rem;
padding-bottom: 5rem;
}

.py-10 {
padding-top: 2.5rem;
padding-bottom: 2.5rem;
}

.text-center {
text-align: center;
}
Expand Down Expand Up @@ -919,11 +883,6 @@ video {
color: rgb(0 0 0 / var(--tw-text-opacity));
}

.text-blue-500 {
--tw-text-opacity: 1;
color: rgb(59 130 246 / var(--tw-text-opacity));
}

.text-private {
--tw-text-opacity: 1;
color: rgb(239 68 68 / var(--tw-text-opacity));
Expand Down Expand Up @@ -1016,29 +975,24 @@ video {
background-color: rgb(24 24 27 / var(--tw-bg-opacity));
}

.dark\:text-blue-300 {
--tw-text-opacity: 1;
color: rgb(147 197 253 / var(--tw-text-opacity));
}

.dark\:text-white {
--tw-text-opacity: 1;
color: rgb(255 255 255 / var(--tw-text-opacity));
}

.dark\:text-zinc-600 {
.dark\:text-zinc-200 {
--tw-text-opacity: 1;
color: rgb(82 82 91 / var(--tw-text-opacity));
color: rgb(228 228 231 / var(--tw-text-opacity));
}

.dark\:text-zinc-300 {
--tw-text-opacity: 1;
color: rgb(212 212 216 / var(--tw-text-opacity));
}

.dark\:text-zinc-200 {
.dark\:text-zinc-600 {
--tw-text-opacity: 1;
color: rgb(228 228 231 / var(--tw-text-opacity));
color: rgb(82 82 91 / var(--tw-text-opacity));
}

.dark\:hover\:bg-zinc-700:hover {
Expand Down

0 comments on commit 0c539e0

Please sign in to comment.