Skip to content

Improved SSR support + Docs #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,86 @@ Vue.use(VueAuthenticate, {

```

### SSR Support

You can use this library on the server side without problems by following the steps below.

1. You must configure the storage as 'cookieStorage', adding to your VueAuthenticate configuration `storageType:' cookieStorage'`.
2. You have to stop using Vue.use(VueAuthenticate) globally, instead, you have to use `VueAuthenticate.factory()`
3. On the server you have to build your own storage (cookie storage will not work) or use the StorageMemory.
4. You have to consider having an instance of axios for each of the requests.

Here an example with plugins of [!UVue](https://github.com/universal-vue/uvue)

Axios Plugin:

```javascript
import axios from 'axios';

import cookies from 'js-cookie'; // only browser

/**
* This plugin create an axios HTTP client to do request.
*/

export default {
async beforeCreate(context, inject) {
// Create axios client
const httpClient = axios.create({
// Change API url: depends on server side or client side
baseURL: '/api/v1',
});

// Inject httpClient eveywhere
inject('http', httpClient);
inject('axios', httpClient);

// You can use it everywhere in your app:
// - In UVue context: `context.$http.get(...)`
// - In your components: `this.$http.get(...)`
// - In your store actions: `this.$http.get(...)`
},
};

```

VueAuthenticate Plugin

```javascript
import VueAuthenticate from 'vue-authenticate/dist/vue-authenticate';

const vueAuthenticateConfig = {
// ...
tokenPrefix: undefined,
tokenName: 'token',
storageType: 'cookieStorage', // Important.
// ...
};

export default {
async beforeCreate(context, inject) {
let vueAuthInstance;
if (process.client) { // browser
vueAuthInstance = VueAuthenticate.factory(context.$http, vueAuthenticateConfig);
} else { // server
const vueConfig = { ...vueAuthenticateConfig, storageType: 'memoryStorage' }; // Override with memory storage
const { tokenName } = vueConfig; // get the token name
const accessToken = context.req.cookies[tokenName]; // Get the access token from request

vueAuthInstance = VueAuthenticate.factory(context.$http, vueConfig);
vueAuthInstance.storage.setItem(tokenName, accessToken); // set the current access token
}

inject('auth', vueAuthInstance); // Inject auth eveywhere

// You can use it everywhere in your app:
// - In UVue context: `context.$auth.isAuthenticated(...)`
// - In your components: `this.$auth.isAuthenticated(...)`
// - In your store actions: `this.$auth.isAuthenticated(...)`
},
};
```

## License

The MIT License (MIT)
Expand Down
4 changes: 2 additions & 2 deletions dist/vue-authenticate.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,11 @@ var fakeWindow = {
},
};

var $document = (typeof document !== undefined)
var $document = (typeof document !== 'undefined')
? document
: fakeDocument;

var $window = (typeof window !== undefined)
var $window = (typeof window !== 'undefined')
? window
: fakeWindow;

Expand Down
4 changes: 2 additions & 2 deletions dist/vue-authenticate.es2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,11 @@ var fakeWindow = {
},
};

var $document = (typeof document !== undefined)
var $document = (typeof document !== 'undefined')
? document
: fakeDocument;

var $window = (typeof window !== undefined)
var $window = (typeof window !== 'undefined')
? window
: fakeWindow;

Expand Down
4 changes: 2 additions & 2 deletions dist/vue-authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ var fakeWindow = {
},
};

var $document = (typeof document !== undefined)
var $document = (typeof document !== 'undefined')
? document
: fakeDocument;

var $window = (typeof window !== undefined)
var $window = (typeof window !== 'undefined')
? window
: fakeWindow;

Expand Down
2 changes: 1 addition & 1 deletion dist/vue-authenticate.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions example/vue-authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ var fakeWindow = {
},
};

var $document = (typeof document !== undefined)
var $document = (typeof document !== 'undefined')
? document
: fakeDocument;

var $window = (typeof window !== undefined)
var $window = (typeof window !== 'undefined')
? window
: fakeWindow;

Expand Down
Loading