-
Notifications
You must be signed in to change notification settings - Fork 0
Backend fix #98
Backend fix #98
Changes from 3 commits
76ffccc
3d4cfe9
94206da
0cd617b
875fffb
dec9f23
76111c4
6ef7dc2
e7c23f8
2a409c2
82aa73e
cbd51ed
c505de1
ba2552f
0e67989
e989147
5ec6597
609879e
4e74168
ad5b6c2
12f378a
1bf7b78
6b13b89
fd5ef9e
5f505f6
4942529
e087793
18c9cfa
8bbcf40
7efbd41
75c2b1f
98ce96b
72ccc2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import * as functions from 'firebase-functions'; | ||
import * as express from 'express'; | ||
|
||
|
||
const admin = require('firebase-admin'); | ||
admin.initializeApp(functions.config().firebase); | ||
|
||
const bodyParser = require('body-parser'); | ||
const moment = require('moment'); | ||
const db = admin.database(); | ||
const fdb = admin.firestore(); | ||
const ref = db.ref('server/account-data/'); | ||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
|
||
async function Verification(req: express.Request, resp: express.Response, next: () => void) { | ||
// req.headers.authorization のオブジェクトが未定義となるためにts-ignore | ||
// @ts-ignore | ||
// AuthorizationヘッダーはBearer <id_token>の形式のため、id_tokenを取り出すために7文字目以降の文字列を切り出している | ||
const tokenstr = req.headers.authorization.toString().slice(7); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. iine |
||
console.log(tokenstr); | ||
const token = await admin.auth().verifyIdToken(tokenstr); | ||
const uid=token.uid; | ||
console.log(uid); | ||
ref.child('users/' + uid).once("value", (snapshot: { exists: () => any; }) => { | ||
if (snapshot.exists()) { | ||
next(); | ||
} else { | ||
console.log('Error: Id token does not match \'query uid\' '); | ||
resp.status(401).send('Unauthorized'); | ||
} | ||
}); | ||
} | ||
|
||
app.use(Verification); | ||
|
||
export const RegisterLog = functions.auth.user().onCreate((user) => { | ||
console.log('Hello ' + user.displayName + ' logged in' + 'called by TS'); | ||
|
||
// データベースに書き込む。一意に定まるユーザのuidを主キーとして設定し、メアドと名前を格納する。 | ||
ref.child('users/' + user.uid).set({ | ||
mail: user.email, | ||
name: user.displayName | ||
}); | ||
return 0; | ||
}); | ||
|
||
export const UnRegisterLog = functions.auth.user().onDelete((user) => { | ||
console.log('Hello ' + user.displayName + ' account deleted ' + 'called by TS'); | ||
|
||
ref.child('users/' + user.uid).remove(); | ||
|
||
return 0; | ||
}); | ||
|
||
// build multiple CRUD interfaces: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメント内容と表す行合ってない様な・・・ |
||
app.get('/class_data', async (req: functions.Request, resp: express.Response) => { | ||
console.log('subject_query= ' + req.query['class_name']); | ||
const err = ""; | ||
const db_data = await fdb.collection('ClassSummary').doc(req.query['class_name']).get(); | ||
const record = db_data.data(); | ||
if (err !== "") { | ||
console.log('class not found probably wrong or empty query'); | ||
resp.status(404).send('Not Found'); | ||
} else { | ||
resp.send(JSON.stringify(record)); | ||
} | ||
}); | ||
|
||
|
||
app.post('/class_data', async (req: functions.Request, resp: express.Response) => { | ||
console.log('json received'); | ||
const body = req.body; | ||
|
||
// Check the validity of the token | ||
|
||
const uid = admin.auth.decodedToken(body.token).uid; | ||
if (!uid) { | ||
resp.status(401).send('Unauthorized'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この辺もreturn 入れ忘れてない? |
||
return 0; | ||
} else { | ||
console.log(uid); | ||
} | ||
|
||
const doc = await fdb.collection('ClassSummary').doc(body.name).collection('comment').doc(body.made_by).get(); | ||
const class_created_time = doc.data().created_at || moment().add(9, 'h').format(); | ||
|
||
const data = { | ||
'name': body.name, | ||
'faculty': body.faculty, | ||
'department': body.department, | ||
'fav_amount': 0, | ||
'grade': body.grade, | ||
'professor': body.professor, | ||
'is_random': body.is_random, | ||
'rating': 0, | ||
'term': body.term, | ||
'edited_by': body.edited_by, | ||
'created_at': class_created_time, | ||
'updated_at': moment().add(9, 'h').format(), | ||
}; | ||
let err = ""; | ||
await fdb.collection('ClassSummary').doc(body.name).set(data).catch((e: string) => err = e); | ||
|
||
if (err !== "") { | ||
console.log('An error occurred. Class data cannot add in database' + err); | ||
resp.status(500).send('Internal Server Error'); | ||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kanade9 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. voidの認識なので |
||
} else { | ||
console.log(data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. early returnしているのでここのelseはいらないと思う! |
||
resp.status(200).send(JSON.stringify({'status': 'OK'})); | ||
return 0; | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. memo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kanade9 この辺見てるけど変更箇所として見る部分は合ってる?
これresponse送るところ |
||
|
||
app.get('/comment', async (req: functions.Request, resp: express.Response) => { | ||
console.log('subject_query= ' + req.query['class_name'] + ' uid=' + req.query['uid']); | ||
let err = ""; | ||
const qss = await fdb.collection('ClassSummary') | ||
.doc(req.query['class_name']) | ||
.collection('comment') | ||
.doc(req.query['uid']).get().catch((e: string) => err = e); | ||
|
||
if (err !== "") { | ||
console.log('class not found probably wrong or empty query'); | ||
resp.status(404).send('Not Found'); | ||
return 0; | ||
} else if (!qss.data()) { | ||
console.log('No comment were found match with ' + req.query['class_name'] + ' and this uid'); | ||
resp.status(404).send('Not Found'); | ||
return 0; | ||
|
||
} else { | ||
resp.status(200).send(JSON.stringify(qss.data())); | ||
return 0; | ||
} | ||
}); | ||
|
||
app.post('/comment', async (req: functions.Request, resp: express.Response) => { | ||
console.log('json received'); | ||
const body = req.body; | ||
const doc = await fdb.collection('ClassSummary').doc(body.name).collection('comment').doc(body.made_by).get(); | ||
const created_time = doc.data().created_at || moment().add(9, 'h').format(); | ||
const data = { | ||
// nameは授業名です。titleはコメントのタイトル ex. 神授業です!!等 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. いいね |
||
'name': body.name, | ||
'title': body.title, | ||
'comment': body.comment, | ||
'created_at': created_time, | ||
'updated_at': moment().add(9, 'h').format(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
'edited_by': body.edited_by, | ||
'image': body.image, | ||
'is_recommend': body.is_recommend | ||
}; | ||
// IDでなくユーザのuidを用いてデータベースに格納する | ||
let err = ""; | ||
await fdb.collection('ClassSummary') | ||
.doc(body.name).collection('comment') | ||
.doc(body.made_by).set(data).data().catch((e: string) => err = e); | ||
|
||
if (err !== "") { | ||
console.log('An error occurred. Comment cannot add in database'); | ||
resp.status(500).send('Internal Server Error'); | ||
|
||
} else { | ||
console.log(data); | ||
resp.status(200).send(JSON.stringify({'status': 'OK'})); | ||
} | ||
}); | ||
|
||
|
||
app.delete('/comment', async (req: functions.Request, resp: express.Response) => { | ||
console.log(req.query['class_name'], '+', req.query['uid']); | ||
let err = ""; | ||
await fdb.collection('ClassSummary') | ||
.doc(req.query['class_name']) | ||
.collection('comment') | ||
.doc(req.query['uid']).delete().catch((e: string) => err = e); | ||
|
||
if (err !== "") { | ||
console.log('An error occurred. Comment cannot delete from database'); | ||
resp.status(500).send('Internal Server Error'); | ||
} else { | ||
resp.status(200); | ||
} | ||
}); | ||
|
||
exports.api = functions.https.onRequest(app); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,183 +1 @@ | ||
import * as functions from 'firebase-functions'; | ||
import * as express from 'express'; | ||
|
||
const bodyParser = require('body-parser'); | ||
|
||
const admin = require('firebase-admin'); | ||
admin.initializeApp(functions.config().firebase); | ||
|
||
const db = admin.database(); | ||
const fdb = admin.firestore(); | ||
|
||
const ref = db.ref('server/account-data/'); | ||
|
||
const app = express(); | ||
// const commentData = express(); | ||
const moment = require('moment'); | ||
|
||
app.use(bodyParser.json()); | ||
|
||
async function Verification(req: express.Request, resp: express.Response, next: () => void) { | ||
// req.headers.authorization のオブジェクトが未定義となるためにts-ignore | ||
// @ts-ignore | ||
|
||
// AuthorizationヘッダーはBearer <id_token>の形式のため、id_tokenを取り出すために7文字目以降の文字列を切り出している | ||
const tokenstr = req.headers.authorization.toString().slice(7); | ||
|
||
try { | ||
const token = await admin.auth().verifyIdToken(tokenstr); | ||
|
||
if (token.uid === req.query['uid']) { | ||
next(); | ||
} else { | ||
console.log('Error: Id token does not match \'query uid\' '); | ||
resp.status(401).send('Unauthorized'); | ||
} | ||
} catch (exception) { | ||
console.log('Error: Firebase ID token has kid claim which does not correspond to a known public key. so get a fresh token from your client app and try again'); | ||
resp.status(401).send('Unauthorized'); | ||
} | ||
} | ||
|
||
app.use(Verification); | ||
|
||
export const WelcomeLog = functions.auth.user().onCreate((user) => { | ||
console.log('Hello ' + user.displayName + ' logged in' + 'called by TS'); | ||
|
||
// データベースに書き込む。一意に定まるユーザのuidを主キーとして設定し、メアドと名前を格納する。 | ||
ref.child('users/' + user.uid).set({ | ||
mail: user.email, | ||
name: user.displayName | ||
}); | ||
return 0; | ||
}); | ||
|
||
export const DeleteLog = functions.auth.user().onDelete((user) => { | ||
console.log('Hello ' + user.displayName + ' account deleted ' + 'called by TS'); | ||
|
||
ref.child('users/' + user.uid).remove(); | ||
|
||
return 0; | ||
}); | ||
|
||
// build multiple CRUD interfaces: | ||
app.get('/class_data', async (req: functions.Request, resp: express.Response) => { | ||
console.log('subject_query= ' + req.query['class_name']); | ||
try { | ||
const documentSnapshot = await fdb.collection('ClassSummary').doc(req.query['class_name']).get(); | ||
const record = documentSnapshot.data(); | ||
if (!record) { | ||
console.log('class not found probably wrong or empty query'); | ||
resp.status(404).send('Not Found'); | ||
} | ||
resp.send(JSON.stringify(record)); | ||
} catch (exception) { | ||
console.log('class not found probably wrong or empty query'); | ||
resp.status(404).send('Not Found'); | ||
} | ||
}); | ||
|
||
app.post('/class_data', async (req: functions.Request, resp: express.Response) => { | ||
console.log('json received'); | ||
const body = req.body; | ||
|
||
// Check the validity of the token | ||
const uid = admin.auth.decodedToken(body.token).uid; | ||
if (!uid) { | ||
resp.status(401).send('Unauthorized'); | ||
} else { | ||
console.log(uid); | ||
} | ||
|
||
let class_created_time = null; | ||
const doc = await fdb.collection('ClassSummary').doc(body.name).collection('comment').doc(body.made_by).get(); | ||
if (doc.exists) { | ||
class_created_time = doc.data().created_at; | ||
} else { | ||
class_created_time = moment().add(9, 'h').format(); | ||
} | ||
const data = { | ||
'name': body.name, | ||
'faculty': body.faculty, | ||
'department': body.department, | ||
'fav_amount': body.fav_amount, | ||
'grade': body.grade, | ||
'professor': body.professor, | ||
'is_random': body.is_random, | ||
'rating': body.rating, | ||
'term': body.term, | ||
'edited_by': body.edited_by, | ||
'created_at': class_created_time, | ||
'updated_at': moment().add(9, 'h').format(), | ||
}; | ||
try { | ||
await fdb.collection('ClassSummary').doc(body.name).set(data); | ||
console.log(data); | ||
resp.status(200).send(JSON.stringify({'status': 'OK'})); | ||
} catch (exception) { | ||
console.log('An error occurred. Class data cannot add in database'); | ||
resp.status(500).send('Internal Server Error'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. メモ |
||
}); | ||
|
||
app.get('/comment', async (req: functions.Request, resp: express.Response) => { | ||
console.log('subject_query= ' + req.query['class_name'] + ' uid=' + req.query['uid']); | ||
try { | ||
const qss = await fdb.collection('ClassSummary').doc(req.query['class_name']).collection('comment').doc(req.query['uid']).get(); | ||
if (!qss.data()) { | ||
console.log('No comment were found match with ' + req.query['class_name'] + ' and this uid'); | ||
resp.status(404).send('Not Found'); | ||
} | ||
resp.status(200).send(JSON.stringify(qss.data())); | ||
} catch (exception) { | ||
console.log('class not found probably wrong or empty query'); | ||
resp.status(404).send('Not Found'); | ||
} | ||
}); | ||
|
||
app.post('/comment', async (req: functions.Request, resp: express.Response) => { | ||
console.log('json received'); | ||
const body = req.body; | ||
let created_time = null; | ||
const doc = await fdb.collection('ClassSummary').doc(body.name).collection('comment').doc(body.made_by).get(); | ||
if (doc.exists) { | ||
created_time = doc.data().created_at; | ||
} else { | ||
created_time = moment().add(9, 'h').format(); | ||
} | ||
|
||
const data = { | ||
'name': body.name, | ||
'title': body.title, | ||
'comment': body.comment, | ||
'created_at': created_time, | ||
'updated_at': moment().add(9, 'h').format(), | ||
'edited_by': body.edited_by, | ||
'image': body.image, | ||
'is_recommend': body.is_recommend | ||
}; | ||
// IDでなくユーザのuidを用いてデータベースに格納する | ||
try { | ||
await fdb.collection('ClassSummary').doc(body.name).collection('comment').doc(body.made_by).set(data); | ||
console.log(data); | ||
resp.status(200).send(JSON.stringify({'status': 'OK'})); | ||
} catch (exception) { | ||
console.log('An error occurred. Comment cannot add in database'); | ||
resp.status(500).send('Internal Server Error'); | ||
} | ||
}); | ||
|
||
|
||
app.delete('/comment', async (req: functions.Request, resp: express.Response) => { | ||
console.log(req.query['class_name'], '+', req.query['uid']); | ||
try { | ||
await fdb.collection('ClassSummary').doc(req.query['class_name']).collection('comment').doc(req.query['uid']).delete(); | ||
resp.status(200); | ||
} catch (exception) { | ||
console.log('An error occurred. Comment cannot delete from database'); | ||
resp.status(500).send('Internal Server Error'); | ||
} | ||
}); | ||
|
||
// Expose Express API as a single Cloud Function: | ||
exports.app = functions.https.onRequest(app); | ||
export * from "./api" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここのコメント消しておいてください (ts-ignore使われていた行は消えましたよね?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いや、最終的には消えませんでした。ので残しています
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここのts-ignoreはどこにかかっているのです?
![image](https://user-images.githubusercontent.com/31179220/74925534-b53ee380-5417-11ea-9758-744d37be79a6.png)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ああ、21行目か・・・空行消しておいて欲しいかなぁ・・・