This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
forked from KhushilMistry/DAWoC-Backend-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
145 lines (124 loc) · 4.08 KB
/
server.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var express = require('express');
var passport = require('passport');
var cors = require('cors');
var mongoose = require('mongoose');
const axios = require('axios');
const async = require('async');
var curl = require('curlrequest');
var User = require('./model/user');
var Project = require('./model/project');
var app = express();
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://KhushilMistry:[email protected]:31315/interview_experience');
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(require('morgan')('combined'));
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use(cors());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.setHeader("Access-Control-Allow-Headers", "X-ACCESS_TOKEN, Access-Control-Allow-Origin, Authorization, Origin, x-requested-with, Content-Type, Content-Range, Content-Disposition, Content-Description");
next();
});
app.post('/github',
function (req, res, next) {
const data1 = req.query;
console.log(req.query);
const data = {
code: data1[0],
client_id: 'Iv1.2c25f6b5db9121be',
client_secret: '101bc18ad084407c59030e2c5ea281926a7e44c3',
}
async.parallel([
function (callback) {
axios({
method: 'post',
url: 'https://github.com/login/oauth/access_token',
params: data
}).then(function (response) {
callback(false, response);
});
}
], function (error, result) {
res.json(result[0].data);
});
});
app.post('/user',
function (req, res, next) {
const data = req.query[0];
const url = 'https://api.github.com/user?' + data;
var options = {
url: url
};
curl.request(options, function (err, resp) {
if (err) {
console.log(err);
}
var response = JSON.parse(resp);
User.findOne({ 'id': response.id }, function (err, user) {
if (err)
res.json(err);
if (user) {
Project.find({}, function (err, projects) {
res.json({ user: user, projects : projects });
});
} else {
var newUser = new User();
newUser.id = response.id;
newUser.username = response.login;
newUser.name = response.name;
newUser.email = response.email;
newUser.image = response.avatar_url;
newUser.repos = response.repos_url;
// save our user into the database
newUser.save(function (err) {
if (err)
throw err;
}).then(function () {
Project.find({}, function (err, projects) {
res.json({ user: newUser, projects : projects });
});
});
}
});
});
});
app.post('/admin', function (req, res, next) {
if (req.query.email === 'dscdaiict' && req.query.password === 'muthuda11ct') {
User.find({}, function (err, users) {
console.log(users);
Project.find({}, function (err, projects) {
res.json({ admin: true, users: users, projects : projects });
});
});
}
else {
res.json({ admin: false, users: '' });
}
});
app.post('/project', function (req, res, next) {
console.log('Hello');
var newProject = new Project();
newProject.name = req.query.name;
newProject.desc = req.query.desc;
newProject.mentor = req.query.mentor;
newProject.language = req.query.language;
newProject.link = req.query.link;
// save our user into the database
newProject.save(function (err) {
if (err)
throw err;
}).then(function () {
console.log('Here !');
Project.find({}, function (err, projects) {
res.json({ projects : projects });
});
});
});
app.listen(process.env.PORT || 3000)