From 0c539e020e8e8a6e5dea10f8071d3d90e3e8114e Mon Sep 17 00:00:00 2001 From: Hamza Date: Sun, 14 Jul 2024 14:00:07 -0400 Subject: [PATCH] GitHub OAuth (hidden) --- app.js | 101 ++++++++++++++++++++++++++++++++++---- package.json | 9 ++-- public/pages/index.ejs | 19 +++++++ public/pages/new-user.ejs | 11 +++++ public/pages/success.ejs | 29 +++++++++++ public/styles/styles.css | 68 +++++-------------------- 6 files changed, 167 insertions(+), 70 deletions(-) create mode 100644 public/pages/index.ejs create mode 100644 public/pages/new-user.ejs create mode 100644 public/pages/success.ejs diff --git a/app.js b/app.js index c6cdce1..5e45c9f 100644 --- a/app.js +++ b/app.js @@ -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); }); diff --git a/package.json b/package.json index 95ff90e..f2e8cef 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/public/pages/index.ejs b/public/pages/index.ejs new file mode 100644 index 0000000..78b1660 --- /dev/null +++ b/public/pages/index.ejs @@ -0,0 +1,19 @@ + + + + Github OAuth + + + + +
+
+

Github OAuth

+

Authorize your app with:

+ Github Login +
+
+ + diff --git a/public/pages/new-user.ejs b/public/pages/new-user.ejs new file mode 100644 index 0000000..14cb230 --- /dev/null +++ b/public/pages/new-user.ejs @@ -0,0 +1,11 @@ + + + + Github OAuth + + +

+ Welcome to FetchCV, <%= userData.name %>!
+

+ + diff --git a/public/pages/success.ejs b/public/pages/success.ejs new file mode 100644 index 0000000..6271743 --- /dev/null +++ b/public/pages/success.ejs @@ -0,0 +1,29 @@ + + + + Github OAuth + + + + +
+
+

Github Information

+
+
+
+

+ Name: <%= userData.name %>
+ Username: <%= userData.login %>
+ Company: <%= userData.company %>
+ Bio: <%= userData.bio %> +

+
+
+
+
+
+ + diff --git a/public/styles/styles.css b/public/styles/styles.css index bdc5b0c..0fbc240 100644 --- a/public/styles/styles.css +++ b/public/styles/styles.css @@ -562,10 +562,6 @@ video { position: absolute; } -.relative { - position: relative; -} - .-left-40 { left: -10rem; } @@ -574,10 +570,6 @@ video { bottom: 0px; } -.right-2 { - right: 0.5rem; -} - .-z-10 { z-index: -10; } @@ -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; } @@ -683,10 +665,6 @@ video { width: 100vw; } -.min-w-\[50vw\] { - min-width: 50vw; -} - .max-w-\[45vw\] { max-width: 45vw; } @@ -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; } @@ -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; } @@ -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)); @@ -1016,19 +975,14 @@ 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 { @@ -1036,9 +990,9 @@ video { 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 {