userController.js
4.73 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import axios from "axios";
import passport from "passport";
import User from "../models/User";
import fetch from "node-fetch";
const getQuote = async (req, res) => {
const url = "http://quotes.stormconsultancy.co.uk/random.json";
const quoteData = await axios.get(url).then(function (response) {
return response.data;
});
const quote = quoteData.quote;
const author = quoteData.author;
return { quote, author };
};
export const handleHome = async (req, res) => {
const quote = await getQuote();
res.render("home", {
pageTitle: "Home",
quote: quote.quote,
author: quote.author,
});
};
export const getUserDetail = async (req, res) => {
try {
const id = req.params.id;
const quote = await getQuote();
const user = await User.findById(id);
const totalCon = await getContributions(user.githubName);
res.render("userDetail", {
pageTitle: "User Detail",
quote: quote.quote,
author: quote.author,
user,
<<<<<<< HEAD
totalContributions: totalCon
=======
totalContributions: totalCon,
>>>>>>> 3dd97787bdb67559e6cc61cb60fb395606d3d6d5
});
} catch (error) {
console.log(error);
res.redirect("/");
}
};
export const getEditProfile = async (req, res) => {
const {
user: { _id: id },
} = req;
try {
const user = await User.findById(id);
if (user.id !== id) {
throw Error();
} else {
res.render("editProfile", { pageTitle: "Edit Profile", user });
}
} catch (error) {
console.log(error);
}
};
export const postEditProfile = async (req, res) => {
const {
user: { _id: id },
body: { name, email, school, blogUrl, tech, career, introduction },
file,
} = req;
const isHeroku = process.env.NODE_ENV === "production";
try {
const updatedUser = await User.findByIdAndUpdate(
id,
{
avatarUrl: file ? (isHeroku ? file.location : file.path) : req.session.passport.user.avatarUrl,
name,
email,
school,
blogUrl,
tech: User.formatTech(tech),
career: User.formatCareer(career),
introduction,
},
{
new: true,
}
);
req.session.passport.user = updatedUser;
//console.log(updatedUser);
res.redirect(`/users/${id}`);
} catch (error) {
console.log(error);
res.redirect("/");
}
};
export const getJoin = (req, res) => {
res.render("join", { pageTitle: "Join" });
};
export const getLogin = (req, res) => {
res.render("login", { pageTitle: "Login" });
};
export const handleUsers = (req, res) => {
res.render("users", { pageTitle: "Users" });
};
export const githubLogin = passport.authenticate("github", {
scope: ["user:email"],
});
export const githubLoginCallback = async (_, __, profile, done) => {
const {
_json: {
id: githubId,
login: githubName,
avatar_url: avatarUrl,
html_url: githubUrl,
name,
email,
},
} = profile;
try {
const user = await User.findOne({ githubId });
if (user) {
(user.githubId = githubId), (user.githubName = githubName);
await user.save();
return done(null, user);
} else {
const newUser = await User.create({
githubId,
githubName,
avatarUrl,
githubUrl,
name,
email
});
return done(null, newUser);
}
} catch (error) {
return done(error);
}
};
export const postGithubLogin = (req, res) => {
const userId = req.user.id;
res.redirect(`/users/${userId}`);
};
export const logout = (req, res) => {
req.logout();
res.redirect("/");
};
<<<<<<< HEAD
const getContributions = async (username) => {
=======
const getContributions = async(username) =>{
>>>>>>> 3dd97787bdb67559e6cc61cb60fb395606d3d6d5
const token = process.env.GH_SECRET_SH;
const headers = {
Authorization: `bearer ${token}`,
};
const body = {
query: `query {user(login: "${username}") {contributionsCollection {contributionCalendar {totalContributions}}}}`,
};
const response = await fetch("https://api.github.com/graphql", {
method: "POST",
body: JSON.stringify(body),
headers: headers,
});
const totalContributions = await response.json();
const total =
totalContributions.data.user.contributionsCollection.contributionCalendar
.totalContributions;
return total;
};
export const getRepos = async(req,res) =>{
try{
const githubNickname = req.headers.nickname
const URL = `https://api.github.com/users/${githubNickname}/repos?sort=updated&per_page=2`;
const response = await fetch(URL,{
headers: {
authorization: `token ${process.env.GH_SECRET_SH}`
}
}).then(function (response) {
return response.json();
}).then(function (data) {
return res.send(data);
});
}catch(error){
console.log(error);
}
}