resource<\/code>\u5909\u6570\u306b\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u306e\u4e2d\u8eab\u304c\u30bb\u30c3\u30c8\u3055\u308c\u307e\u3059\u306e\u3067\u3001document\u306eID\u4ee5\u5916\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u3068\u4f55\u304b\u6bd4\u8f03\u3059\u308b\u5834\u5408\u306f\u5229\u7528\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u305d\u3046\u3067\u3059\u3002\nrules_version = '2';\nservice cloud.firestore {\n match \/databases\/{database}\/documents {\n match \/users\/{uid} {\n allow create: if request.auth.uid != null;\n allow read, update, delete: if request.auth.uid == uid;\n }\n match \/users\/{uid}\/memos\/{memo} {\n allow read, create, update, delete: if request.auth.uid == uid;\n }\n }\n}<\/code><\/pre>\n<\/li>\n<\/ul>\nUser Document\u304c\u5b58\u5728\u3057\u306a\u304b\u3063\u305f\u3089\u30c7\u30fc\u30bf\u8ffd\u52a0<\/h3>\n
\u8a8d\u8a3c\u304c\u5b8c\u4e86\u3057\u305f\u969b\u306b\u3001User Document\u304c\u5b58\u5728\u3057\u306a\u304b\u3063\u305f\u3089\u8ffd\u52a0\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002\u30dd\u30c3\u30d7\u30a2\u30c3\u30d7\u30da\u30fc\u30b8\u3067\u8a8d\u8a3c\u3092\u884c\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001\/src\/popup<\/code>\u306eApp.vue<\/code>\u306b\u51e6\u7406\u3092\u7d44\u307f\u8fbc\u3093\u3067\u3044\u304d\u307e\u3059\u3002 <\/p>\n\n- \u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u306euid\u3092\u5143\u306bUser Document\u3092\u691c\u7d22\u3057\u3001\u5b58\u5728\u3057\u306a\u304b\u3063\u305f\u3089\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059<\/li>\n
- User\u60c5\u5831\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306e
UserModel<\/code>\u30af\u30e9\u30b9\u3068User Document\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306eUserRepository<\/code>\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u3002\u500b\u4eba\u7684\u306b\u306f\u3053\u306e\u8fba\u306f\u30af\u30e9\u30b9\u5316\u3057\u3066\u304a\u3044\u305f\u65b9\u304c\u8cac\u52d9\u304c\u660e\u78ba\u306b\u306a\u308a\u898b\u901a\u3057\u304c\u3088\u304f\u306a\u308b\u6c17\u304c\u3057\u307e\u3059\u3002\u5c06\u6765\u7684\u306b\u306f\u5225\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u79fb\u52d5\u3059\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093<\/li>\nUserRepository<\/code>\u30af\u30e9\u30b9\u306f\u3001User Document\u3092\u53c2\u7167\u3059\u308b\u305f\u3081\u306eDocumentReference <\/code>\u3092\u901a\u3058\u3066\u3001Document\u3092\u53d6\u5f97\u3057\u305f\u308a\u4fdd\u5b58\u3057\u305f\u308a\u3059\u308b\u95a2\u6570\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002DocumentReference.set()<\/code>\u95a2\u6570\u306f\u3001\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u306a\u3051\u308c\u3070\u8ffd\u52a0\u3001\u5b58\u5728\u3059\u308c\u3070\u66f4\u65b0\u3057\u3066\u304f\u308c\u308b\u306e\u304c\u4fbf\u5229\u3067\u3059<\/li>\n- \n
API\u547c\u3073\u51fa\u3057\u7cfb\u306f\u57fa\u672c\u3069\u308c\u3082\u975e\u540c\u671f\u3067\u52d5\u304f\u305f\u3081\u3001callback\u306enest\u304c\u6c17\u306b\u306a\u3063\u3066\u304f\u308b\u306e\u3067\u3001async\u3001await\u3092\u4f7f\u3063\u3066\u540c\u671f\u7684\u306b\u547c\u3073\u51fa\u3059\u5f62\u306b\u3057\u3066\u3042\u308a\u307e\u3059<\/p>\n
<template>\n <v-app>\n- <template v-if="uid && url">\n- <Memo :uid="uid" :url="url" \/>\n+ <template v-if="user && user.id && url">\n+ <Memo :uid="user.id" :url="url" \/>\n <\/template>\n <template v-else>\n\uff08\u4e2d\u7565\uff09\n import firebase from '..\/plugins\/firebase'\n+import DocumentReference = firebase.firestore.DocumentReference\n+import DocumentData = firebase.firestore.DocumentData\n+import DocumentSnapshot = firebase.firestore.DocumentSnapshot\n\n+class UserModel {\n+ id: string | null = null\n+ defaultMode = 'plane' \/\/ TODO: \u5c06\u6765\u7684\u306bmarkdown\u3084wysiwyg\u3092\u9078\u3079\u308b\u3088\u3046\u306b\n+}\n+\n+class UserRepository {\n+ userRef: DocumentReference<DocumentData>\n+ constructor(uid: string) {\n+ this.userRef = firebase.firestore().collection('users').doc(uid)\n+ }\n+\n+ async get(): Promise<UserModel> {\n+ return this.userRef\n+ .get()\n+ .then((userData: DocumentSnapshot<DocumentData>) => {\n+ const user = new UserModel()\n+ if (userData.exists) {\n+ user.id = userData.id\n+ user.defaultMode = userData.get('defaultMode')\n+ }\n+ return user\n+ })\n+ .catch((error: any) => {\n+ throw new Error(`Error getting user:, ${error}`)\n+ })\n+ }\n+\n+ async save(user: UserModel): Promise<UserModel> {\n+ return this.userRef\n+ .set({\n+ defaultMode: user.defaultMode,\n+ })\n+ .then(() => {\n+ console.log('User successfully saved!')\n+ return this.get()\n+ })\n+ .catch((error: any) => {\n+ throw new Error(`Error saving memo:, ${error}`)\n+ })\n+ }\n+}\n\n @Component({\n components: { Memo },\n })\n export default class App extends Vue {\n- private uid: string | null = null\n+ private user: UserModel | null = null\n private url: string | null = null\n\n- created(): Promise<void> {\n+ async created(): Promise<void> {\n chrome.tabs.getSelected((tab: chrome.tabs.Tab) => {\n if (!tab.url) {\n throw new Error('Could not get tab url.')\n }\n this.url = encodeURIComponent(tab.url.split(\/[?#]\/)[0])\n })\n\n- firebase.auth().onAuthStateChanged((authUser: firebase.User | null) => {\n+ firebase.auth().onAuthStateChanged(async (authUser: firebase.User | null) => {\n if (authUser === null) {\n- this.uid = null\n+ this.user = null\n } else {\n- this.uid = authUser.uid\n+ const userRepository = new UserRepository(authUser.uid)\n+ this.user = await userRepository.get()\n+ if (!this.user.id) {\n+ await userRepository.save(this.user)\n+ }\n+ } \n })<\/code><\/pre>\n<\/li>\n<\/ul>\nMemo Document\u3092\u53d6\u5f97\u304a\u3088\u3073\u4fdd\u5b58<\/h3>\n
\u30e1\u30e2\u753b\u9762Memo.vue<\/code>\u3082\u4f3c\u305f\u3088\u3046\u306a\u611f\u3058\u3067\u51e6\u7406\u3092\u8ffd\u52a0\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\ncreated<\/code>\u30d5\u30c3\u30af\u3067Firestore\u304b\u3089\u30e1\u30e2\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3001\u4fdd\u5b58\u30dc\u30bf\u30f3\u304c\u30af\u30ea\u30c3\u30af\u30af\u30ea\u30c3\u30af\u3055\u308c\u305f\u3089\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/li>\n