Skip to content

Commit

Permalink
twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
vitvuive committed Aug 31, 2021
1 parent 68f9996 commit 3836f52
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ typings/

# next.js build output
.next


notes-fs-data
notes.level
chap07.sqlite3
notes-sequelize.sqlite3
2 changes: 2 additions & 0 deletions app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { default as cookieParser } from "cookie-parser";
import { default as bodyParser } from "body-parser";
import * as http from "http";
import { default as rfs } from "rotating-file-stream";
import dotenv from "dotenv/config.js";

import { approotdir } from "./approotdir.mjs";
import { default as DBG } from "debug";
const debug = DBG("notes:debug");
Expand Down
106 changes: 106 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"cookie-parser": "~1.4.4",
"cross-env": "^7.0.3",
"debug": "~2.6.9",
"dotenv": "8.2.x",
"express": "~4.16.1",
"express-session": "1.17.x",
"feather-icons": "4.25.x",
Expand All @@ -39,6 +40,7 @@
"mysql": "^2.18.1",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"passport-twitter": "^1.0.4",
"popper.js": "1.16.x",
"rotating-file-stream": "^2.1.5",
"sequelize": "^6.6.5",
Expand Down
10 changes: 10 additions & 0 deletions partials/header.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
{{else}}
<div class="collapse navbar-collapse" id="navbarLogIn">
<a class="btn btn-primary" href="/users/login">Log in</a>


{{#if twitterLogin}}
<a class="nav-item nav-link btn btn-dark col-auto"
href="/users/auth/twitter">
<img width="15px"
src="/assets/vendor/twitter/Twitter_SocialIcon_Rounded_Square_Color.png"/>
Log in with Twitter</a>
{{/if}}

</div>
{{/if}}

Expand Down
3 changes: 3 additions & 0 deletions routes/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { default as express } from "express";
import { NotesStore as notes } from "../app.mjs";
import { twitterLogin } from "./users.mjs";

export const router = express.Router();

/* GET home page. */
Expand All @@ -16,6 +18,7 @@ router.get("/", async (req, res, next) => {
title: "Notes",
notelist: notelist,
user: req.user ? req.user : undefined,
twitterLogin: twitterLogin,
});
} catch (err) {
next(err);
Expand Down
5 changes: 5 additions & 0 deletions routes/notes.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { default as express } from "express";
import { NotesStore as notes } from "../app.mjs";
import { ensureAuthenticated } from "./users.mjs";
import { twitterLogin } from "./users.mjs";

export const router = express.Router();
// Add Note.
Expand All @@ -11,6 +12,7 @@ router.get("/add", ensureAuthenticated, (req, res, next) => {
notekey: "",
user: req.user,
note: undefined,
twitterLogin: twitterLogin,
});
});

Expand Down Expand Up @@ -46,6 +48,7 @@ router.get("/view", async (req, res, next) => {
notekey: req.query.key,
user: req.user ? req.user : undefined,
note: note,
twitterLogin: twitterLogin,
});
} catch (err) {
next(err);
Expand All @@ -62,6 +65,7 @@ router.get("/edit", ensureAuthenticated, async (req, res, next) => {
notekey: req.query.key,
user: req.user,
note: note,
twitterLogin: twitterLogin,
});
} catch (err) {
next(err);
Expand All @@ -77,6 +81,7 @@ router.get("/destroy", ensureAuthenticated, async (req, res, next) => {
notekey: req.query.key,
user: req.user,
note: note,
twitterLogin: twitterLogin,
});
} catch (err) {
next(err);
Expand Down
60 changes: 60 additions & 0 deletions routes/users.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,64 @@ import { default as express } from "express";
import { default as passport } from "passport";
import { default as passportLocal } from "passport-local";
const LocalStrategy = passportLocal.Strategy;

import passportTwitter from "passport-twitter";
const TwitterStrategy = passportTwitter.Strategy;

import * as usersModel from "../models/users-superagent.mjs";
import { sessionCookieName } from "../app.mjs";
export const router = express.Router();
import DBG from "debug";
const debug = DBG("notes:router-users");
const error = DBG("notes:error-users");

// tiwter login
const twittercallback = process.env.TWITTER_CALLBACK_HOST
? process.env.TWITTER_CALLBACK_HOST
: "http://localhost:3000";

export var twitterLogin;

if (
typeof process.env.TWITTER_CONSUMER_KEY !== "undefined" &&
process.env.TWITTER_CONSUMER_KEY !== "" &&
typeof process.env.TWITTER_CONSUMER_SECRET !== "undefined" &&
process.env.TWITTER_CONSUMER_SECRET !== ""
) {
passport.use(
new TwitterStrategy(
{
consumerKey: process.env.TWITTER_CONSUMER_KEY,
consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
callbackURL: `${twittercallback}/users/auth/twitter/callback`,
},
async function (token, tokenSecret, profile, done) {
try {
done(
null,
await usersModel.findOrCreate({
id: profile.username,
username: profile.username,
password: "",
provider: profile.provider,
familyName: profile.displayName,
givenName: "",
middleName: "",
photos: profile.photos,
emails: profile.emails,
})
);
} catch (err) {
done(err);
}
}
)
);
twitterLogin = true;
} else {
twitterLogin = false;
}

export function initPassport(app) {
app.use(passport.initialize());
app.use(passport.session());
Expand Down Expand Up @@ -53,6 +104,15 @@ router.get("/logout", function (req, res, next) {
}
});

router.get("/auth/twitter", passport.authenticate("twitter"));
router.get(
"/auth/twitter/callback",
passport.authenticate("twitter", {
successRedirect: "/",
failureRedirect: "/users/login",
})
);

passport.use(
new LocalStrategy(async (username, password, done) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion sessions/NqncvvVx_ypVcX6Cuf7_5ReNTs_vOI5Z.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"__lastAccess":1630422101094}
{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"__lastAccess":1630423698887}

0 comments on commit 3836f52

Please sign in to comment.