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

Commit f44dd80

Browse files
committed
feat: add axios common event
1 parent c9405fa commit f44dd80

File tree

10 files changed

+74
-78
lines changed

10 files changed

+74
-78
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = {
2828
rules: {
2929
'dot-notation': 'off',
3030
'no-console': 'off',
31+
'@typescript-eslint/no-empty-interface': 'off',
3132
'@typescript-eslint/no-object-literal-type-assertion': 'off',
3233
'@typescript-eslint/interface-name-prefix': 'off',
3334
'@typescript-eslint/no-unused-vars': 'off',

nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module.exports = {
5353
** Plugins to load before mounting the App
5454
*/
5555
plugins: [
56+
'@/plugins/axios.ts',
5657
'@/plugins/constants-inject.ts',
5758
'@/plugins/env-inject.ts',
5859
'@/plugins/vue-lazyload.ts'

src/interface/User/ILoginCheck.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/**
22
* LoginCheck インターフェイス
33
*/
4-
export interface ILoginCheckPayload {
5-
token: string
6-
}
4+
export interface ILoginCheckPayload {}
75

86
export interface ILoginCheck {
97
loggedIn: boolean

src/interface/User/ILogout.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/**
22
* Logout インターフェイス
33
*/
4-
export interface ILogoutPayload {
5-
token: string
6-
}
4+
export interface ILogoutPayload {}
75

86
export interface ILogout {
97
loggedIn: boolean

src/layouts/error.vue

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
<template>
2-
<div class="container">
3-
<h1 v-if="error.statusCode === 404">
4-
ページが見つかりません
5-
</h1>
6-
<h1 v-else>
7-
エラーが発生しました
8-
</h1>
9-
<nuxt-link to="/example">
10-
ホーム
11-
</nuxt-link>
12-
</div>
1+
<template lang="pug">
2+
.container
3+
h1(v-if='error.statusCode === 404')
4+
| ページが見つかりません
5+
h1(v-else='')
6+
| エラーが発生しました {{ error.message }}
7+
nuxt-link(to='/example')
8+
| ホーム
139
</template>
14-
<script>
15-
export default {
16-
props: {
17-
error: undefined
18-
}
10+
11+
<script lang="ts">
12+
import { Component, Prop, Vue } from 'nuxt-property-decorator'
13+
14+
@Component
15+
export default class Error extends Vue {
16+
@Prop({ type: Object })
17+
public error: any
1918
}
2019
</script>

src/middleware/authenticated.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44
* @param redirect
55
*/
66

7-
import { getTokenFromCookie } from '@/utilities/'
8-
import { ILoginCheckPayload, ILoginCheck } from '@/interface/User/ILoginCheck'
7+
import { ILoginCheckPayload } from '@/interface/User/ILoginCheck'
98

109
export default async function({ store, redirect }): Promise<void> {
1110
console.log('authenticated')
1211

13-
const token = getTokenFromCookie()
14-
await store.dispatch('auth/loginCheck', {
15-
token
16-
} as ILoginCheckPayload)
12+
await store.dispatch('auth/loginCheck', {} as ILoginCheckPayload)
1713

1814
if (!store.getters['auth/isAuthenticated']) {
1915
await redirect('/example/auth/sign-in')

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55

66
<script lang="ts">
77
import { Component, Vue } from 'nuxt-property-decorator'
8-
import { getTokenFromCookie } from '@/utilities/'
98
import { ILogoutPayload } from '@/interface/User/ILogout'
109
1110
@Component({
1211
middleware: 'authenticated'
1312
})
1413
export default class SignOff extends Vue {
1514
public async mounted() {
16-
await this.$store.dispatch('auth/logout', {
17-
token: getTokenFromCookie()
18-
} as ILogoutPayload)
15+
await this.$store.dispatch('auth/logout', {} as ILogoutPayload)
1916
2017
this.$router.replace('/example')
2118
}

src/plugins/axios.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @file Expand axios
3+
*/
4+
5+
import { AxiosError, AxiosRequestConfig } from 'axios'
6+
import Vue from 'vue'
7+
import { getTokenFromCookie } from '@/utilities/'
8+
9+
export default ({ $axios, app, req, error }) => {
10+
$axios.onRequest((config: AxiosRequestConfig) => {
11+
console.log('$axios.onRequest')
12+
const token = getTokenFromCookie(req)
13+
14+
if (token) {
15+
config.headers['access-token'] = token
16+
}
17+
})
18+
19+
$axios.onResponse(response => {
20+
console.log('$axios.onResponse')
21+
})
22+
23+
$axios.onResponseError((response: AxiosError) => {
24+
console.log('$axios.onResponseError')
25+
// 通信エラー
26+
if (!response.response) {
27+
return
28+
}
29+
30+
const { status } = response.response
31+
32+
if (status === 401) {
33+
const message = '401 error'
34+
35+
error({ statusCode: 401, message })
36+
}
37+
})
38+
}

src/store/auth.ts

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,10 @@ export const actions = {
8888
* login
8989
* @param state
9090
* @param commit
91-
* @param token
91+
* @param payload
9292
*/
9393
async login(
9494
this: Vue,
95-
// @ts-ignore
9695
{ state, commit }: any,
9796
payload: ILoginPayload
9897
): Promise<IUser | void> {
@@ -108,13 +107,7 @@ export const actions = {
108107
commit('updateBusyStatus', ['login', true])
109108

110109
try {
111-
const {
112-
data,
113-
headers,
114-
status,
115-
statusText,
116-
config
117-
} = await this.$axios.post<IUser>(
110+
const { data, headers } = await this.$axios.post<IUser>(
118111
this.$C.API_ENDPOINT.LOGIN,
119112
{
120113
username: payload.username,
@@ -144,10 +137,10 @@ export const actions = {
144137
* logout
145138
* @param state
146139
* @param commit
140+
* @param payload
147141
*/
148142
async logout(
149143
this: Vue,
150-
// @ts-ignore
151144
{ state, commit }: any,
152145
payload: ILogoutPayload
153146
): Promise<ILogout | void> {
@@ -161,19 +154,13 @@ export const actions = {
161154
commit('updateBusyStatus', ['logout', true])
162155

163156
try {
164-
const token: string = payload.token
165-
const {
166-
data,
167-
headers,
168-
status,
169-
statusText,
170-
config
171-
} = await this.$axios.post<ILogout>(this.$C.API_ENDPOINT.LOGOUT, {}, {
172-
headers: {
173-
'access-token': token
174-
},
175-
cancelToken: cancelToken.getToken(payload)
176-
} as AxiosRequestConfig)
157+
const { data } = await this.$axios.post<ILogout>(
158+
this.$C.API_ENDPOINT.LOGOUT,
159+
{},
160+
{
161+
cancelToken: cancelToken.getToken(payload)
162+
} as AxiosRequestConfig
163+
)
177164

178165
unsetToken()
179166

@@ -192,11 +179,10 @@ export const actions = {
192179
* loginCheck
193180
* @param state
194181
* @param commit
195-
* @param token
182+
* @param payload
196183
*/
197184
async loginCheck(
198185
this: Vue,
199-
// @ts-ignore
200186
{ state, commit }: any,
201187
payload: ILoginCheckPayload
202188
): Promise<ILoginCheck | void> {
@@ -205,23 +191,10 @@ export const actions = {
205191
commit('updateBusyStatus', ['loginCheck', true])
206192

207193
try {
208-
const postHeaders = {}
209-
const token: string | undefined = payload.token
210-
if (token) {
211-
postHeaders['access-token'] = token
212-
}
213-
214-
const {
215-
data,
216-
headers,
217-
status,
218-
statusText,
219-
config
220-
} = await this.$axios.post<ILoginCheck>(
194+
const { data, headers } = await this.$axios.post<ILoginCheck>(
221195
this.$C.API_ENDPOINT.LOGIN_CHECK,
222196
{},
223197
{
224-
headers: postHeaders,
225198
cancelToken: cancelToken.getToken(payload)
226199
} as AxiosRequestConfig
227200
)

src/store/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ActionContext } from 'vuex'
22
import { ILoginCheckPayload } from '@/interface/User/ILoginCheck'
3-
import { getTokenFromCookie } from '@/utilities/'
43

54
/**
65
* store 用インターフェイス
@@ -59,11 +58,7 @@ export const actions = {
5958
await console.log('nuxtServerInit')
6059
commit('setIsServerInitCalled')
6160

62-
const token = getTokenFromCookie(req)
63-
64-
await dispatch('auth/loginCheck', {
65-
token
66-
} as ILoginCheckPayload)
61+
await dispatch('auth/loginCheck', {} as ILoginCheckPayload)
6762
},
6863
/**
6964
* クライアント初期化時の処理

0 commit comments

Comments
 (0)