質問編集履歴

2

ログイン機能の部分のコード

2020/12/07 08:49

投稿

jime1234567
jime1234567

スコア3

test CHANGED
File without changes
test CHANGED
@@ -110,6 +110,346 @@
110
110
 
111
111
  ![ヘッダー画像](3880bb5af3b6f8c67ea6a3139ee10542.png)
112
112
 
113
+ ### 追記
114
+
115
+
116
+
117
+ 下記がログイン機能の部分のコードになります
118
+
119
+ ```
120
+
121
+ **app/store/index.js**
122
+
123
+
124
+
125
+
126
+
127
+ import moment from '~/plugins/moment'
128
+
129
+
130
+
131
+ export const state = () => ({
132
+
133
+ isLoggedIn: false,
134
+
135
+ user: null
136
+
137
+ })
138
+
139
+
140
+
141
+ export const getters = {
142
+
143
+ isLoggedIn: state => state.isLoggedIn,
144
+
145
+ user: state => (state.user ? Object.assign({ likes: [] }, state.user) : null)
146
+
147
+ }
148
+
149
+
150
+
151
+ export const mutations = {
152
+
153
+ setUser(state, { user }) {
154
+
155
+ if (user.id.match(/_|@|./)) {
156
+
157
+ throw new TypeError('invalid username')
158
+
159
+ }
160
+
161
+ state.user = user
162
+
163
+ state.isLoggedIn = true
164
+
165
+ },
166
+
167
+ updateUser(state, { user }) {
168
+
169
+ state.user = user
170
+
171
+ }
172
+
173
+ }
174
+
175
+
176
+
177
+ export const actions = {
178
+
179
+ async login({ commit }, { id }) {
180
+
181
+ if (id.match(/_|@|./)) {
182
+
183
+ throw new TypeError('invalid username')
184
+
185
+ }
186
+
187
+ const user = await this.$axios.$get(`/users/${id}.json`)
188
+
189
+ console.log(user)
190
+
191
+ if (!user.id) throw new Error('Invalid user')
192
+
193
+ commit('setUser', { user })
194
+
195
+ },
196
+
197
+ async register({ commit }, { id }) {
198
+
199
+ const payload = {}
200
+
201
+ payload[id] = { id }
202
+
203
+ await this.$axios.$patch(`/users.json`, payload)
204
+
205
+ const user = await this.$axios.$get(`/users/${id}.json`)
206
+
207
+ if (!user.id) throw new Error('Invalid user')
208
+
209
+ commit('setUser', { user })
210
+
211
+ },
212
+
213
+ async addLikeLogToUser({ commit }, { user, post }) {
214
+
215
+ user.likes.push({
216
+
217
+ created_at: moment().format(),
218
+
219
+ user_id: user.id,
220
+
221
+ post_id: post.id
222
+
223
+ })
224
+
225
+ const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
226
+
227
+ commit('updateUser', { user: newUser })
228
+
229
+ },
230
+
231
+ async removeLikeLogToUser({ commit }, { user, post }) {
232
+
233
+ user.likes = post.likes.filter(like => like.user_id !== user.id) || []
234
+
235
+ const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
236
+
237
+ commit('updateUser', { user: newUser })
238
+
239
+ }
240
+
241
+ }
242
+
243
+
244
+
245
+ ```
246
+
247
+
248
+
249
+ ```
250
+
251
+ **app/pages/index.vue**
252
+
253
+ <section class="container">
254
+
255
+ <el-card style="flex: 1">
256
+
257
+ <div slot="header" class="clearfix">
258
+
259
+ <span>ログイン</span>
260
+
261
+ </div>
262
+
263
+ <form>
264
+
265
+ <div class="form-content">
266
+
267
+ <span>ユーザー ID</span>
268
+
269
+ <el-input placeholder="" v-model="formData.id" />
270
+
271
+ </div>
272
+
273
+ <div class="form-content">
274
+
275
+ <el-checkbox v-model="isCreateMode">アカウントを作成する</el-checkbox>
276
+
277
+ </div>
278
+
279
+ <div class="text-right">
280
+
281
+ <el-button type="primary" @click="handleClickSubmit">{{buttonText}}</el-button>
282
+
283
+ </div>
284
+
285
+ </form>
286
+
287
+ </el-card>
288
+
289
+ </section>
290
+
291
+ </template>
292
+
293
+
294
+
295
+ <script>
296
+
297
+ import { mapGetters, mapActions } from 'vuex'
298
+
299
+ import Cookies from 'universal-cookie'
300
+
301
+
302
+
303
+ export default {
304
+
305
+ asyncData({ redirect, store }) {
306
+
307
+ if (store.getters['user']) {
308
+
309
+ redirect('/posts/')
310
+
311
+ }
312
+
313
+ return {
314
+
315
+ isCreateMode: false,
316
+
317
+ formData: {
318
+
319
+ id: ''
320
+
321
+ }
322
+
323
+ }
324
+
325
+ },
326
+
327
+ computed: {
328
+
329
+ buttonText() {
330
+
331
+ return this.isCreateMode ? '新規登録' : 'ログイン'
332
+
333
+ },
334
+
335
+ ...mapGetters(['user'])
336
+
337
+ },
338
+
339
+ methods: {
340
+
341
+ async handleClickSubmit() {
342
+
343
+ const cookies = new Cookies()
344
+
345
+ if (this.isCreateMode) {
346
+
347
+ try {
348
+
349
+ await this.register({ ...this.formData })
350
+
351
+ this.$notify({
352
+
353
+ type: 'success',
354
+
355
+ title: 'アカウント作成完了',
356
+
357
+ message: `${this.formData.id} として登録しました`,
358
+
359
+ position: 'bottom-right',
360
+
361
+ duration: 1000
362
+
363
+ })
364
+
365
+ cookies.set('user', JSON.stringify(this.user))
366
+
367
+ this.$router.push('/posts/')
368
+
369
+ } catch (e) {
370
+
371
+ this.$notify.error({
372
+
373
+ title: 'アカウント作成失敗',
374
+
375
+ message: '既に登録されているか、不正なユーザー ID です',
376
+
377
+ position: 'bottom-right',
378
+
379
+ duration: 1000
380
+
381
+ })
382
+
383
+ }
384
+
385
+ } else {
386
+
387
+ try {
388
+
389
+ await this.login({ ...this.formData })
390
+
391
+ this.$notify({
392
+
393
+ type: 'success',
394
+
395
+ title: 'ログイン成功',
396
+
397
+ message: `${this.formData.id} としてログインしました`,
398
+
399
+ position: 'bottom-right',
400
+
401
+ duration: 1000
402
+
403
+ })
404
+
405
+ cookies.set('user', JSON.stringify(this.user))
406
+
407
+ this.$router.push('/posts/')
408
+
409
+ } catch (e) {
410
+
411
+ this.$notify.error({
412
+
413
+ title: 'ログイン失敗',
414
+
415
+ message: '不正なユーザー ID です',
416
+
417
+ position: 'bottom-right',
418
+
419
+ duration: 1000
420
+
421
+ })
422
+
423
+ }
424
+
425
+ }
426
+
427
+ },
428
+
429
+ ...mapActions(['login', 'register'])
430
+
431
+ }
432
+
433
+ }
434
+
435
+ </script>
436
+
437
+
438
+
439
+ <style scoped>
440
+
441
+ .form-content {
442
+
443
+ margin: 16px 0;
444
+
445
+ }
446
+
447
+ </style>
448
+
449
+
450
+
451
+ ```
452
+
113
453
 
114
454
 
115
455
  ### 試したこと

1

ファイル名

2020/12/07 08:49

投稿

jime1234567
jime1234567

スコア3

test CHANGED
File without changes
test CHANGED
@@ -9,6 +9,8 @@
9
9
  ### 該当のソースコード
10
10
 
11
11
  ```
12
+
13
+ **/components/TheHeader.vue**
12
14
 
13
15
  <template>
14
16