-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
71 lines (58 loc) · 2.11 KB
/
index.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
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const express = require('express')
const bp = require('body-parser')
const cors = require('cors')({ origin: true })
const firebase = require('firebase-functions')
const serviceAccount = require('./service_account.json')
const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG)
adminConfig.credential = admin.credential.cert(serviceAccount)
admin.initializeApp(adminConfig)
const Member = require('./src/api/members/members.model')
const MemberService = require('./src/api/members/members.service')
const MemberController = require('./src/api/members/members.controller')
const SdcController = require('./src/api/sdc/sdc.controller')
const eventsApp = require('./src/api/events/events.router')
const environmentService = require('./src/api/environment/index')
if (firebase.config().environment.env !== 'production')
console.log(`Running in ${firebase.config().environment.env}`)
const api = express()
api.use(cors)
api.use(bp.json())
api.use(bp.urlencoded({ extended: false }))
api.use('/members', MemberController)
api.use('/sdc', SdcController)
api.use('/events', eventsApp)
api.use('/environment', environmentService)
module.exports.app = api
module.exports.api = functions.https.onRequest(api)
// Creates a new user in the database after every login
module.exports.createNewUser = functions.auth.user().onCreate(async (user) => {
const svc = new MemberService()
const newMember = new Member()
// eslint-disable-next-line prefer-const
let [firstName, ...lastName] = user.displayName.split(' ')
lastName = lastName.join(' ')
newMember.firstName = firstName
newMember.lastName = lastName
try {
await svc.createEntity(user.uid, newMember)
return 0
} catch (err) {
console.error(err)
return 1
}
})
// Deletes user in the database if they ever get deleted
module.exports.deleteExistingUser = functions.auth
.user()
.onDelete(async (user) => {
const svc = new MemberService()
try {
await svc.deleteEntityById(user.uid)
return 0
} catch (err) {
console.error(err)
return 1
}
})