-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
54 lines (44 loc) · 977 Bytes
/
user.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
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let User = new Schema({
_id: {
type: String,
required: true
},
name: {
type: String
},
picture: {
type: String
},
email: {
type: String
},
lastLogin: {
type: Date,
default: Date.now
},
dateAdded: {
type: Date,
default: Date.now
}
});
const Model = mongoose.model('User', User)
const login = function(req, res) {
const filter = { _id: req.body.sub};
const newDoc = {
name: req.body.name,
picture: req.body.picture,
email: req.body.email,
lastLogin: Date.now()
};
Model.findOneAndUpdate(filter, newDoc, {new: true, upsert: true, setDefaultsOnInsert: true }).then(doc => {
res.json(doc);
}).catch(err => {
console.log(err);
res.status(400).send('Upsert Error');
});
}
module.exports = {
login
};