-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
executable file
·128 lines (90 loc) · 3.34 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// hikari dependencies
var bodyParser = require("body-parser");
var errorHandler = require("errorhandler");
var express = require("express");
var fs = require("fs");
var http = require("http");
var methodOverride = require("method-override");
var logger = require("morgan");
var multer = require("multer");
var passport = require("passport");
var FacebookStrategy = require("passport-facebook").Strategy;
var GitHubStrategy = require("passport-github").Strategy;
var path = require("path");
var routes = require("routes");
var sass = require("node-sass-middleware");
var favicon = require("serve-favicon");
var app = express();
// OAuth keys and secrets
eval(fs.readFileSync("./boot/oauth.js") + "");
// Sass compilation
// Environment setup
app.set("port", process.env.PORT || 1343);
app.set("view engine", "html");
app.engine("html", require("hbs").__express);
app.use(favicon(__dirname + "/system/images/favicon.png"));
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, "system")));
app.use(multer());
app.set("views", path.join(__dirname, "views"));
// Development only
if ("development" == app.get("env")) {
// app.use(express.errorHandler());
}
// Authentication with Facebook
passport.use(
new FacebookStrategy({
clientID: fbID,
clientSecret: fbSecret,
callbackURL: "http://localhost:1343/auth/facebook/callback"
// profileFields: ["id", "displayName", "photos"]
},
function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
// should probably disable this and use callbacks...womp, will do later
fs.writeFile("system/account/facebookProfile.json", JSON.stringify(profile));
console.log(profile);
return done(null, profile);
});
}
));
passport.serializeUser(function (user, done) { done(null, user); });
passport.deserializeUser(function (obj, done) { done(null, obj); });
app.get("/auth/facebook", passport.authenticate("facebook"));
app.get("/auth/facebook/callback", passport.authenticate("facebook", { successRedirect: "/s", failureRedirect: "/" }));
app.get("/", function (req, res) { res.render("login.html"); });
app.get("/s", function (req, res) { res.render("index.html"); });
// Authentication with GitHub
passport.use(
new GitHubStrategy({
clientID: githubID,
clientSecret: githubSecret,
callbackURL: "http://localhost:1343/auth/github/callback"
},
function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
// should probably disable this and use callbacks...womp, will do later
fs.writeFile("system/account/githubProfile.json", JSON.stringify(profile));
var userAvatar = profile._json.gravatar_id;
var userName = profile._json.username;
console.log(profile);
return done(null, profile);
});
}
));
app.get("/auth/github", passport.authenticate("github"));
app.get("/auth/github/callback", passport.authenticate("github", { successRedirect: "/s", failureRedirect: "/" }));
// Logout
app.get("/logout", function (req, res) {
req.logout();
res.redirect("/");
});
// Start hikari!
var server = app.listen(app.get("port"), function () {
console.log("hikari initialized on " + server.address().port);
});