Skip to content
Permalink
Browse files

Add global session object

  • Loading branch information...
nlaz committed Feb 28, 2019
1 parent b3eec46 commit fe1257ac492e33d7b58b983839b4b0498c337346
Showing with 47 additions and 11 deletions.
  1. +10 −4 app/controllers/auth.js
  2. +8 −0 app/controllers/tutorial.js
  3. +11 −1 app/index.js
  4. +1 −1 app/services/github.js
  5. +15 −5 app/templates/partials/header.html
  6. +1 −0 config/index.js
  7. +1 −0 package.json
@@ -7,11 +7,12 @@ const GitHub = require("../services/github");
const router = express.Router();

router.get("/logout", function(req, res) {
// TODO
req.session.destroy();
res.redirect("/");
});

router.get("/login/github", function(req, res) {
const github = new GitHub(config.githubClientId, config.githubClientSecret);
const github = new GitHub({ client_id: config.githubClientId, client_secret: config.githubClientSecret });
res.redirect(github.authorization_url("public_repo"));
});

@@ -21,14 +22,19 @@ router.get("/callback/github", async function(req, res) {
}

// Fetch user from GitHub OAuth and store in session
const github = new GitHub(config.githubClientId, config.githubClientSecret);
const github = new GitHub({ client_id: config.githubClientId, client_secret: config.githubClientSecret });
const access_token = await github.get_token(req.query.code);

if (!access_token) {
return res.render("404");
}

const user = models.User.find_or_create_from_token(access_token);
const user = await models.User.find_or_create_from_token(access_token);
console.log("user", user);

req.session.access_token = access_token;
req.session.user_id = user.id;
console.log("🍎", req.session.user_id);

return res.redirect("/");
});
@@ -3,6 +3,14 @@ const express = require("express");
const router = express.Router();

router.get("/requesting", function(req, res) {
const { search } = req.query;

if (!req.session.access_token) {
return res.render("tutorial/index");
}

const github = new GitHub({ access_token: req.session.access_token });

res.render("tutorial/index");
});

@@ -1,6 +1,7 @@
const express = require("express");
const compression = require("compression");
const session = require("express-session");
const handlebars = require("express-handlebars");
const compression = require("compression");
const logger = require("morgan");

const config = require("../config");
@@ -25,6 +26,15 @@ app.engine("html", handlebars({ defaultLayout: "layout", extname: ".html" }));
app.set("views", "app/templates");
app.set("view engine", "html");

// Set up session middleware
const options = { secret: config.secretKey, saveUninitialized: true, resave: true };
app.use(session(options));
app.use(function(req, res, next) {
// console.log(req.session);
res.locals.session = req.session;
next();
});

// Set up the routes for the static assets.
app.use(express.static(paths.staticEntry));

@@ -5,7 +5,7 @@ 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 = "") {
constructor({ client_id = "", client_secret = "", access_token = "" }) {
this.client_id = client_id;
this.client_secret = client_secret;
this.access_token = access_token;
@@ -17,11 +17,21 @@
</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link text-inherit" href="/auth/login/github">
Login with GitHub
</a>
</li>
{{#if session.user }}
<li class="nav-item">
<a class="nav-link text-inherit" href="/auth/logout">
Logout
</a>
</li>
{{/if}}
{{ session.user }}
{{#unless session.user }}
<li class="nav-item">
<a class="nav-link text-inherit" href="/auth/login/github">
Login with GitHub
</a>
</li>
{{/unless }}
</ul>
</div>
</div>
@@ -5,6 +5,7 @@ dotenv.config({ path: ".env" });
module.exports = {
port: process.env.PORT || 5000,
env: process.env.NODE_ENV || "development",
secretKey: process.env.SECRET_KEY || "octocat",
githubClientId: process.env.GITHUB_CLIENT_ID || "",
githubClientSecret: process.env.GITHUB_CLIENT_SECRET || ""
};
@@ -12,6 +12,7 @@
"dotenv": "^6.2.0",
"express": "^4.16.4",
"express-handlebars": "^3.0.2",
"express-session": "^1.15.6",
"morgan": "^1.9.1",
"pg": "^7.8.1",
"pg-hstore": "^2.3.2",

0 comments on commit fe1257a

Please sign in to comment.
You can’t perform that action at this time.