Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit 255e49f

Browse files
committed
feat: add logincheck to middleware/authenticated.ts
1 parent 9a02b24 commit 255e49f

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

src/middleware/anonymous.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export default async function({ store, redirect }: any): Promise<void> {
1+
/**
2+
* すでにログインしていたらトップにリダイレクトするミドルウェア
3+
* @param store
4+
* @param redirect
5+
*/
6+
export default async function({ store, redirect }): Promise<void> {
27
console.log('anonymous')
38
if (store.getters['auth/isAuthenticated']) {
49
await redirect('/example')

src/middleware/authenticated.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
export default async function({ store, redirect }: any): Promise<void> {
1+
/**
2+
* ログインしていなかったらログイン画面にリダイレクトするミドルウェア
3+
* @param store
4+
* @param redirect
5+
*/
6+
7+
import { getTokenFromCookie } from '@/utilities/'
8+
import { ILoginCheckPayload, ILoginCheck } from '@/interface/User/ILoginCheck'
9+
10+
export default async function({ store, redirect }): Promise<void> {
211
console.log('authenticated')
12+
13+
const token = getTokenFromCookie()
14+
await store.dispatch('auth/loginCheck', {
15+
token
16+
} as ILoginCheckPayload)
17+
318
if (!store.getters['auth/isAuthenticated']) {
419
await redirect('/example/auth/sign-in')
520
}

src/pages/example/auth/sign-in.vue

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
<template lang="pug">
2-
section
3-
h1.title
4-
| sign-in {{counter}} seconds
2+
.login-form
3+
form(@submit.prevent='login')
4+
p.error(v-if='error') {{ error }}
5+
p
6+
input(type='text' v-model='username' placeholder='username' name='username')
7+
p
8+
input(type='text' v-model='password' placeholder='password' name='password')
9+
.login-btn
10+
button(type='submit') ログイン
511
</template>
612

713
<script lang="ts">
814
import { Component, Vue } from 'nuxt-property-decorator'
9-
import { sleep } from '@/utilities/'
15+
import { ILoginPayload, IUser } from '@/interface/User/ILogin'
1016
1117
@Component({
1218
middleware: 'anonymous'
1319
})
1420
export default class SignIn extends Vue {
15-
public counter: number = 3
16-
17-
public async mounted() {
18-
await sleep(1000)
19-
this.counter = 2
20-
await sleep(1000)
21-
this.counter = 1
22-
await sleep(1000)
23-
this.counter = 0
24-
25-
await this.$router.replace('/example/auth/signed-in?user=hisasann')
26-
}
21+
public username: string = ''
22+
public password: string = ''
23+
public error: string | null = null
2724
2825
public head() {
2926
return {
3027
title: 'sign-in'
3128
}
3229
}
30+
31+
public async login() {
32+
try {
33+
const res: IUser = await this.$store.dispatch('auth/login', {
34+
username: this.username,
35+
password: this.password
36+
} as ILoginPayload)
37+
38+
this.$router.push('/example')
39+
} catch (e) {
40+
this.error = e.message
41+
}
42+
}
3343
}
3444
</script>

src/pages/example/auth/sign-off.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55

66
<script lang="ts">
77
import { Component, Vue } from 'nuxt-property-decorator'
8-
import { unsetToken } from '@/utilities/auth'
8+
import { getTokenFromCookie } from '@/utilities/'
9+
import { ILogoutPayload } from '@/interface/User/ILogout'
910
10-
@Component
11+
@Component({
12+
middleware: 'authenticated'
13+
})
1114
export default class SignOff extends Vue {
1215
public async mounted() {
13-
unsetToken()
14-
await this.$store.dispatch('auth/logout')
16+
await this.$store.dispatch('auth/logout', {
17+
token: getTokenFromCookie()
18+
} as ILogoutPayload)
19+
1520
this.$router.replace('/example')
1621
}
1722

0 commit comments

Comments
 (0)