Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = function (moduleOptions) {

// Add vue-apollo and apollo-client in common bundle
if (!isNuxtVersion2) {
this.addVendor(['vue-apollo', 'js-cookie', 'cookie'])
this.addVendor(['vue-apollo', 'cookie-universal'])
}

// Add graphql loader
Expand Down
31 changes: 12 additions & 19 deletions lib/templates/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@ import Vue from 'vue'
import VueApollo from 'vue-apollo'
import 'isomorphic-fetch'
import { createApolloClient, restartWebsockets } from 'vue-cli-plugin-apollo/graphql-client'
import jsCookie from 'js-cookie'
import cookie from 'cookie'
import Cookie from 'cookie-universal'
import { InMemoryCache } from 'apollo-cache-inmemory'

Vue.use(VueApollo)

export default (ctx, inject) => {
const providerOptions = { clients: {} }
const { app, beforeNuxtRender, req } = ctx
const { app, beforeNuxtRender, req, res } = ctx
const AUTH_TOKEN_NAME = '<%= options.tokenName %>'
const COOKIE_ATTRIBUTES = <%= serialize(options.cookieAttributes) %>
const AUTH_TYPE = '<%= options.authenticationType %> '
const cookies = Cookie(req, res)

// Config
<% Object.keys(options.clientConfigs).forEach((key) => { %>
const <%= key %>TokenName = '<%= options.clientConfigs[key].tokenName %>' || AUTH_TOKEN_NAME
const <%= key %>CookieAttributes = '<%= options.clientConfigs[key].cookieAttributes %>' || COOKIE_ATTRIBUTES

function <%= key %>GetAuth () {
let token
if(process.server){
const cookies = cookie.parse((req && req.headers.cookie) || '')
token = cookies[<%= key %>TokenName]
} else {
token = jsCookie.get(<%= key %>TokenName, <%= key %>CookieAttributes)
}
const token = cookies.get(<%= key %>TokenName)
return token && <%= key %>ClientConfig.validateToken(token) ? AUTH_TYPE + token : ''
}

Expand Down Expand Up @@ -64,7 +58,6 @@ export default (ctx, inject) => {
<%= key %>ClientConfig.ssr = !!process.server
<%= key %>ClientConfig.cache = <%= key %>Cache
<%= key %>ClientConfig.tokenName = <%= key %>TokenName
<%= key %>ClientConfig.cookieAttributes = <%= key %>CookieAttributes

// Create apollo client
let <%= key %>ApolloCreation = createApolloClient({
Expand Down Expand Up @@ -108,10 +101,14 @@ export default (ctx, inject) => {
// Fallback for tokenExpires param
if (typeof cookieAttributes === 'number') cookieAttributes = { expires: cookieAttributes }

if (typeof cookieAttributes.expires === 'number') {
cookieAttributes.expires = new Date(Date.now()+ 86400*1000*cookieAttributes.expires)
}

if (token) {
jsCookie.set(AUTH_TOKEN_NAME, token, cookieAttributes)
cookies.set(AUTH_TOKEN_NAME, token, cookieAttributes)
} else {
jsCookie.remove(AUTH_TOKEN_NAME, cookieAttributes)
cookies.remove(AUTH_TOKEN_NAME, cookieAttributes)
}
if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
try {
Expand All @@ -122,7 +119,7 @@ export default (ctx, inject) => {
}
},
onLogout: async (apolloClient = apolloProvider.defaultClient) => {
jsCookie.remove(AUTH_TOKEN_NAME, COOKIE_ATTRIBUTES)
cookies.remove(AUTH_TOKEN_NAME, COOKIE_ATTRIBUTES)
if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
try {
await apolloClient.resetStore()
Expand All @@ -132,11 +129,7 @@ export default (ctx, inject) => {
}
},
getToken: (tokenName = AUTH_TOKEN_NAME) => {
if(process.server){
const cookies = cookie.parse((req && req.headers.cookie) || '')
return cookies && cookies[tokenName]
}
return jsCookie.get(tokenName, COOKIE_ATTRIBUTES)
return cookies.get(tokenName)
}
})
}
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"access": "public"
},
"scripts": {
"dev": "NODE_ENV=production nuxt test/fixture",
"dev2": "NODE_ENV=production nuxt test/fixture-static-auth",
"dev3": "NODE_ENV=production nuxt test/fixture-multi-endpoints",
"dev": "nuxt test/fixture",
"dev2": "nuxt test/fixture-static-auth",
"dev3": "nuxt test/fixture-multi-endpoints",
"lint": "eslint lib test",
"test": "yarn test:fixtures && yarn test:types",
"test:fixtures": "jest",
Expand All @@ -34,9 +34,8 @@
"collectCoverage": true
},
"dependencies": {
"cookie": "^0.4.0",
"cookie-universal": "^2.0.16",
"isomorphic-fetch": "^2.2.1",
"js-cookie": "^2.2.0",
"vue-apollo": "^3.0.0-rc.2",
"vue-cli-plugin-apollo": "^0.21.0",
"webpack-node-externals": "^1.7.2"
Expand Down
20 changes: 14 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,11 @@
dependencies:
"@types/node" "*"

"@types/cookie@^0.3.1":
version "0.3.3"
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.3.3.tgz#85bc74ba782fb7aa3a514d11767832b0e3bc6803"
integrity sha512-LKVP3cgXBT9RYj+t+9FDKwS5tdI+rPBXaNSkma7hvqy35lc7mAokC2zsqWJH0LaqIt3B962nuYI77hsJoT1gow==

"@types/cors@^2.8.4":
version "2.8.5"
resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.5.tgz#c0c54c4e643e1d943d447292f2baf9dc82cfc8ec"
Expand Down Expand Up @@ -4155,7 +4160,15 @@ cookie-signature@1.0.6:
resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c"
integrity sha1-4wOogrNCzD7oylE6eZmXNNqzriw=

cookie@0.4.0, cookie@^0.4.0:
cookie-universal@^2.0.16:
version "2.0.16"
resolved "https://registry.yarnpkg.com/cookie-universal/-/cookie-universal-2.0.16.tgz#ec8b55789b502a377ef02ad230923c1dfa5c1061"
integrity sha512-EHtQ5Tg3UoUHG7LmeV3rlV3iYthkhUuYZ0y86EseypxGcUuvzxuHExEb6mHKDhDPrIrdewAHdG/aCHuG/T4zEg==
dependencies:
"@types/cookie" "^0.3.1"
cookie "^0.3.1"

cookie@0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
integrity sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==
Expand Down Expand Up @@ -7519,11 +7532,6 @@ jest@latest:
import-local "^2.0.0"
jest-cli "^24.8.0"

js-cookie@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-2.2.0.tgz#1b2c279a6eece380a12168b92485265b35b1effb"
integrity sha1-Gywnmm7s44ChIWi5JIUmWzWx7/s=

js-levenshtein@^1.1.3:
version "1.1.6"
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"
Expand Down