-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
222 lines (194 loc) · 5.77 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require('dotenv').config()
const Hapi = require("hapi")
const Boom = require("boom")
const hapiJWT = require("hapi-auth-jwt2")
const { Sequelize, Op } = require('sequelize')
const sequelize = new Sequelize(process.env.DATABASE_URL, { logging: process.env.DATABASE_SHOW_LOG === "true" })
const firebaseAdmin = require("firebase-admin")
const Services = require("./Services")
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(require("./project-hifive-firebase-adminsdk.json")),
})
const User = sequelize.define("users", {
authID: {
type: Sequelize.STRING, // ID given by Firebase when authenticating
primaryKey: true
},
authorized: {
type: Sequelize.BOOLEAN,
defaultValue: false // false for hugger but true for huggy
},
birthdate: Sequelize.BIGINT,
maxchild: {
type: Sequelize.INTEGER,
defaultValue: 3
},
name: Sequelize.STRING,
picture: Sequelize.STRING,
sex: Sequelize.STRING,
story: Sequelize.STRING,
type: Sequelize.STRING,
deviceToken: Sequelize.STRING
})
const Chat = sequelize.define("chats", {
user1: Sequelize.STRING,
user2: Sequelize.STRING
})
const Message = sequelize.define("messages", {
message: Sequelize.STRING
})
Chat.belongsTo(User, { foreignKey: "user1", as: "hugger" })
Chat.belongsTo(User, { foreignKey: "user2", as: "huggy" })
Chat.hasMany(Message, {foreignKey: "chat_id"})
Message.belongsTo(Chat, {foreignKey: "chat_id"})
User.hasMany(Message, {foreignKey: "sender_id"})
Message.belongsTo(User, {foreignKey: "sender_id"})
User.sync()
Chat.sync()
Message.sync()
const server = new Hapi.Server()
server.connection({
port: process.env.PORT,
routes: {
cors: false
}
})
server.register([
require('./hapi-firebase-auth'),
{
register: require('./live'),
options: {
firebaseAdmin: firebaseAdmin
}
}
], (err) => {
if(err) throw err
server.auth.strategy('firebase', 'firebase', { firebaseAdmin })
server.route({
method: 'GET',
path: '/auth',
config: {
auth: 'firebase'
},
handler: (req, reply) => {
reply({ text: "Authentication OK." })
}
})
server.route({
method: 'POST',
path: '/user',
config: {
auth: {
strategy: 'firebase',
mode: 'optional'
}
},
handler: (req, reply) => {
const data = req.payload
User.build(data)
.save()
.then((res) => {
Services.assignHuggerToHuggy(data)
reply(true)
})
.catch((err) => console.log(err))
}
})
server.route({
method: 'GET',
path: '/user/exists/{id}',
config: {
auth: 'firebase'
},
handler: async (req, reply) => {
const user = await User.findOne({ where: { authID: req.params.id } })
const res = {}
res[req.params.id] = !!user
console.log(res)
reply(res)
}
})
server.route({
method: 'GET',
path: '/user/me',
config: {
auth: 'firebase'
},
handler: async (req, reply) => {
const user = await User.findOne({ where: { authID: req.auth.credentials.user_id } })
reply(JSON.stringify(user))
}
})
server.route({
method: 'GET',
path: '/user/{id}',
config: {
auth: 'firebase'
},
handler: async (req, reply) => {
try{
// Check if requester has access to requested by looking for chat relation
// If true, link is proved so we grant access to profile
const chatExists = await Chat.findOne({
where: {
[Op.or]: [
{
[Op.and]: [
{ user1: req.auth.credentials.user_id },
{ user2: req.params.id }
]
},
{
[Op.and]: [
{ user2: req.auth.credentials.user_id },
{ user1: req.params.id },
]
}
]
}
})
if(chatExists){ // TODO: query by authID only
const user = await User.findOne({ where: { [Op.or]: [{ authID: req.params.id }, { id: req.params.id }] } })
reply(JSON.stringify(user)) // TODO: This is not secure, some fields should not be shared publicly
}else{
reply(Boom.unauthorized())
}
}catch(err){
console.log(err)
reply(Boom.internal())
}
}
})
server.route({
method: 'POST',
path: '/user/edit',
config: {
auth: 'firebase'
},
handler: (req, reply) => {
// TODO
}
})
server.start(() => console.log("Server up and running on port " + process.env.PORT))
})
/*
SOLID API:
- /edit
- Update and send to Firebase
LIVE API:
- Mood update
- emit when huggy
- subscribe and receive when hugger
- Chat
- subscribe to rooms
- update accordingly
- save in database
- save in memcache
///
Microservices :
- Push Notifications
- Firebase Storage
- quickActions Triggerer (text analysis)
- chatbot
*/
// Services.assignHuggerToHuggy({ type: "huggy", authID: "I6aQREjHKINZlkF8ljGmEIB2bv73"})