-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
151 lines (126 loc) · 3.98 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
146
147
148
149
150
const axios = require('axios')
const dotenv = require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose')
const app = express()
// const port = 5000
const {OAuth2Client} = require('google-auth-library')
const Searchhistory = require('./models/searchhistory')
const bodyParser = require('body-parser')
const session = require('express-session')
const MemoryStore = require('memorystore')(session)
const client = new OAuth2Client(process.env.GOOGLE_AUTH_CLIENT_ID)
const cors = require('cors')
const dburi = process.env.MONGODB_CONNECTION_STRING
mongoose.connect(dburi) // connect to the mogodb database
.then((res) => {
console.log("Database connection successfull")
app.listen(process.env.PORT || 5000, () => {
console.log(`App running`)
})
})
.catch(err => console.log(err))
//cors
app.use((req,res,next) =>{
res.header('Access-Control-Allow-Origin','*')
res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE')
res.header('Access-Control-Allow-Headers','Content-Type')
next()
})
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({
cookie:{maxAge: 86400000 },
store: new MemoryStore({
checkPeriod:86400000
}),
secret:process.env.SESSION_SECRET,
resave:false,
saveUninitialized:false
}))
app.get("/",(req,res) =>{
// console.log("sdjkhfskjdhfksjdhf");
res.send("Server up")
})
async function addtoDatabase(req){ //adds multiple records to the database at once
await Searchhistory.insertMany(req.session.searches)
.then((response) => {
console.log(`Added records`)
req.session.searches = []
})
.catch((err) => console.log(err))
}
app.get('/testcors',(req,res) =>{
var p = {hi:"hi"}
res.send(p)
})
app.get('/history',(req,res) =>{//this can be used to get the history of searches made by the current user
// console.log('srwerwerwer')
if(req.session.user)
{
Searchhistory.find().where('useremail').equals(req.session.user.email)
.then(response => res.send(response))
.catch(err => console.log(err))}
else{
res.send('Authentication required.')
}
})
app.post('/verifyToken',async (req,res) =>{//verify OAuth token for valid user
const {token} = req.body
const ticket = await client.verifyIdToken({
idToken: token,
audience: process.env.GOOGLE_AUTH_CLIENT_ID
});
const { name, email, picture } = ticket.getPayload();
var user = {
name:name,
email:email,
picture:picture
}
req.session.user = user
req.session.searches = []
// console.log(user)
console.log(req.session.user)
res.send(user)
})
app.get('/logout',(req,res)=>{//logout user
if(req.session.user)
{
if(req.session.searches.length != 0)
addtoDatabase(req)
req.session.user = null
req.session.searches =[]
res.send('logout success')}
else
res.send("Logged out already")
}
)
app.get('/getbooklist', (req, res) => {//returns a book list wrt the queryTerm
if(req.session.user)
{const queryTerm = req.query.queryTerm
req.session.searches.push({username:req.session.user.name,useremail:req.session.user.email,queryTerm:queryTerm})
if(req.session.searches.length == 5)
addtoDatabase(req)
// add to database after every 5 searches to avoid multiple hits to database
axios.get('https://www.googleapis.com/books/v1/volumes?q=intitle:'+queryTerm+'&orderBy=relevance&key='+process.env.GOOGLE_BOOKS_API_KEY)
.then(response => {
res.send(response.data.items)
})
.catch(err => console.log(err))}
else{
res.send('Authentication required.')
}
})
app.get('/getbook',(req,res) =>{//get details of a particular bool by volumeId
if(req.session.user){
const volumeId = req.query.volumeId
axios.get(`https://www.googleapis.com/books/v1/volumes/${volumeId}?key=`+process.env.GOOGLE_BOOKS_API_KEY)
.then(response =>
res.send(response.data)
)
.catch(err => console.log(err))
}
else{
res.send('Authentication required')
}
})