-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserController.js
198 lines (165 loc) · 4.37 KB
/
userController.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
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
198
import User from "../models/userModel.js";
import bcrypt from 'bcrypt'
import jwt from 'jsonwebtoken'
import Photo from '../models/photoModels.js'
//temelde baasit yapiyi kurdiuk geleni req.body attik sonra res statiu sile geri gonderdik
//bunu yaptiktan sonra register els action yapmayi unutmaaction user/regisetr methof post
const createUser = async (req, res) => {
try {
const user = await User.create(req.body);
console.log(user)
res.status(201).json({user:user._id})
} catch (error) {
let errors2 = {}
if (error.code === 11000) {
errors2.email = 'The Email is already registered';
}
if(error.name === 'ValidationError'){
Object.keys(error.errors).forEach((key)=>{
errors2[key]=error.errors[key].message
})
}
res.status(400).json(errors2)
}
};
const loginUser= async (req, res) => {
try {
const {username,password} =req.body
console.log(username,password,"reeeee")
const user =await User.findOne({username:username})
let same =false
if(user){
same = await bcrypt.compare(password,user.password)
}else{
return res.status(500).json({
succed: false,
error:'login anschauen'
});
}
if(same){
// .send('dddddddddd') bunu ilk deneme icin
//token uretmeyi burrda yeniden yaapriz
/**
* json({
user,
token:createToken(user._id)
*/
//sonra yenii token yapama
const token = createToken(user._id)
res.cookie('jwt',token, {
httOnly:true,
maxAge:1000*60*60*24
})
/**
* res.status(400).json({
user
})
*/
//giris yaptiktan sonra
res.redirect('/users/dashboard')
}
else{
res.status(500).json({
succed: false,
error:'login password'
});
}
} catch (error) {
res.status(500).json({
succed: false,
error,
});
}
};
//buradada jwebtokeni kullamiyityor burda once userid alsin biy id saklamaj istiyoruy
//returnyapizoru sonra sign kullaniyoruy sign bak
//uc parametre aliyor ilk once ne kullancagiy sonra sifreme sonra sure
//sonra login girisye gonderroy
const createToken=(userId)=>{
return jwt.sign({userId},process.env.JWT_SECRET,{
expiresIn:'1d'
})
}
const getDashboardPage = async (req, res) => {
const photos = await Photo.find({ user: res.locals.user._id });
const user = await User.findById({_id:res.locals.user._id}).populate([
'followings',
'followers'
])
res.render('dashboard', {
link: 'dashboard',
photos,
user,
});
}
const getAllUsers = async(req,res)=>{
try {
const users = await User.find({ _id:{$ne:res.locals.user._id}})
res.status(200).render('users',{
users,
link: 'users',
})
} catch (err) {
}
}
const getAUsers = async(req,res)=>{
try {
const user = await User.findById({ _id: req.params.id })
const inFollowers = user.followers.some((follower) => {
return follower.equals(res.locals.user._id);
});
const photos = await Photo.find({user:user._id})
res.status(200).render('user',{
user,
photos,
inFollowers,
link: 'user',
})
} catch (err) {
}
}
//buraada once takip yaptik yeni bir user ve bunun andUpdate id ekleriz ve push yapariz sonra new olarak dondurur
//ikinci userde biyim takip ettigimiz icin
const follow = async(req,res)=>{
try {
let user =await User.findByIdAndUpdate(
{
_id:req.params.id
},
{
$push:{followers:res.locals.user._id}
},{
new:true
}
)
user= await User.findByIdAndUpdate(
{_id:res.locals.user._id},
{$push:{followings :req.params.id}},
{new:true}
)
res.status(200).redirect(`/users/${req.params.id}`);
} catch (err) {
}
}
const unfollow = async(req,res)=>{
try {
let user =await User.findByIdAndUpdate(
{
_id:req.params.id
},
{
$pull:{followers:res.locals.user._id}
},{
new:true
}
)
user= await User.findByIdAndUpdate(
{_id:res.locals.user._id},
{$pull:{followings :req.params.id}},
{new:true}
)
res.status(200).redirect(`/users/${req.params.id}`);
} catch (err) {
}
}
export { createUser,loginUser,getDashboardPage,getAllUsers ,getAUsers,follow,unfollow};