Skip to content
Permalink
Browse files

Build github queries

  • Loading branch information...
nlaz committed Mar 1, 2019
1 parent fe1257a commit e5d766b89813fd61ac5e4edc08de0d392bca2478
@@ -30,11 +30,9 @@ router.get("/callback/github", async function(req, res) {
}

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);
req.session.user = user;

return res.redirect("/");
});
@@ -1,17 +1,24 @@
const express = require("express");
const GitHub = require("../services/github");

const router = express.Router();

router.get("/requesting", function(req, res) {
const { search } = req.query;
router.get("/requesting", async 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 });
const results1 = await github.get("/user/starred");
const results2 = search.length > 0 ? await github.get("/search/repositories", { q: search }) : [];

res.render("tutorial/index");
return res.render("tutorial/index", {
tutorial1: results1,
tutorial2: results2,
query: search
});
});

module.exports = router;
@@ -30,7 +30,6 @@ app.set("view engine", "html");
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();
});
@@ -19,7 +19,8 @@ module.exports = (sequelize, DataTypes) => {
const data = await GitHub.get_user_from_token(access_token);

/* Find existing user or create new User instances */
const instance = await this.findOrCreate({
const [instance, created] = await this.findOrCreate({
raw: true,
where: { username: data["login"] },
defaults: {
username: data["login"],
@@ -9,6 +9,7 @@ module.exports.registerRoutes = app => {

module.exports.registerErrorHandlers = app => {
app.use(function(err, req, res, next) {
console.log("ERROR", err.message);
res.status(err.status || 500);
res.render("500", {
message: err.message,
@@ -28,6 +28,14 @@ class GitHub {
return data.access_token;
}

async get(route_url, params = {}) {
const url = api_url + route_url;
params["access_token"] = this.access_token;

const response = await axios.get(url, { params });
return response.data;
}

static async get_user_from_token(access_token) {
/* Fetch user data using the access token. */
const url = api_url + "/user";

This file was deleted.

Oops, something went wrong.
@@ -18,13 +18,16 @@
</div>
</li>
{{#if session.user }}
<li class="nav-item p-2 pr-3">
<img src="{{ session.user.avatar_url }}" class="rounded mr-2" style="width:24px;height:24px;" />
{{ session.user.username }}
</li>
<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">

0 comments on commit e5d766b

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