Skip to content

Commit

Permalink
#84 feat: トークンが無効な場合にログイン画面に転送する
Browse files Browse the repository at this point in the history
  • Loading branch information
moriyadetteiu committed Dec 9, 2020
1 parent 16c5598 commit 6a0ec8e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
3 changes: 3 additions & 0 deletions front/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export default {
clientConfigs: {
default: '~/plugins/apollo/client-config.ts',
},
httpLinkOptions: {
credentials: 'omit',
},
},

// Build Configuration (https://go.nuxtjs.dev/config-build)
Expand Down
2 changes: 2 additions & 0 deletions front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"@nuxtjs/axios": "^5.12.2",
"@nuxtjs/pwa": "^3.0.2",
"@types/faker": "^5.1.4",
"apollo-link": "^1.2.14",
"apollo-link-error": "^1.1.13",
"core-js": "^3.6.5",
"faker": "^5.1.0",
"graphql": "^15.4.0",
Expand Down
6 changes: 0 additions & 6 deletions front/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,5 @@ export default {
Logo,
VuetifyLogo,
},
apollo: {
users: {
query: users,
},
},
}
</script>
30 changes: 21 additions & 9 deletions front/plugins/apollo/client-config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { Context } from '@nuxt/types';
import { Context } from '@nuxt/types'
import { ApolloLink } from 'apollo-link'
import { ApolloHelpers } from '@nuxtjs/apollo'
import errorHandler from './error-handler'

export default (context: Context) => {
const IS_USE_MOCK_SERVER: boolean = context.$config.IS_USE_MOCK_SERVER;
const MOCK_HTTP_ENDPOINT: string = context.$config.MOCK_HTTP_ENDPOINT;
const MOCK_BROWSER_HTTP_ENDPOINT: string = context.$config.MOCK_BROWSER_HTTP_ENDPOINT;
const HTTP_ENDPOINT: string = context.$config.HTTP_ENDPOINT;
const BROWSER_HTTP_ENDPOINT: string = context.$config.BROWSER_HTTP_ENDPOINT;
export default (context: Context & { $apolloHelpers: ApolloHelpers }) => {
const IS_USE_MOCK_SERVER: boolean = context.$config.IS_USE_MOCK_SERVER
const MOCK_HTTP_ENDPOINT: string = context.$config.MOCK_HTTP_ENDPOINT
const MOCK_BROWSER_HTTP_ENDPOINT: string =
context.$config.MOCK_BROWSER_HTTP_ENDPOINT
const HTTP_ENDPOINT: string = context.$config.HTTP_ENDPOINT
const BROWSER_HTTP_ENDPOINT: string = context.$config.BROWSER_HTTP_ENDPOINT

const errorLink = errorHandler(context)

return {
httpEndpoint: IS_USE_MOCK_SERVER ? MOCK_HTTP_ENDPOINT : HTTP_ENDPOINT,
browserHttpEndpoint: IS_USE_MOCK_SERVER ? MOCK_BROWSER_HTTP_ENDPOINT : BROWSER_HTTP_ENDPOINT,
browserHttpEndpoint: IS_USE_MOCK_SERVER
? MOCK_BROWSER_HTTP_ENDPOINT
: BROWSER_HTTP_ENDPOINT,
httpLinkOptions: {
credentials: 'omit',
},
link: ApolloLink.from([errorLink]),
}
};
}
25 changes: 25 additions & 0 deletions front/plugins/apollo/error-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Context } from '@nuxt/types'
import { ApolloHelpers } from '@nuxtjs/apollo'
import { GraphQLError } from 'graphql'
import { onError } from 'apollo-link-error'

export default (context: Context & { $apolloHelpers: ApolloHelpers }) => {
return onError(({ graphQLErrors }) => {
if (graphQLErrors) {
graphQLErrors.forEach(async (graphQLError: GraphQLError) => {
const category: string = graphQLError?.extensions?.category ?? ''
const message: string = graphQLError.message ?? ''
if (
category === 'authentication' &&
~message.indexOf('Unauthenticated')
) {
if (!process.server) {
await context.$apolloHelpers.onLogout()
location.reload()
}
await context.redirect('/login')
}
})
}
})
}

0 comments on commit 6a0ec8e

Please sign in to comment.