diff --git a/src/options.js b/src/options.js index 5bdf5ef..49d8226 100644 --- a/src/options.js +++ b/src/options.js @@ -1,3 +1,6 @@ +import { getCookieDomain, getRedirectUri } from './utils.js'; + + /** * Default configuration */ @@ -13,9 +16,10 @@ export default { storageType: 'localStorage', storageNamespace: 'vue-authenticate', cookieStorage: { - domain: window.location.hostname, + domain: getCookieDomain(), path: '/', - secure: false + secure: false, + getCookieFn: null, }, requestDataKey: 'data', responseDataKey: 'data', @@ -55,7 +59,7 @@ export default { name: 'facebook', url: '/auth/facebook', authorizationEndpoint: 'https://www.facebook.com/v2.5/dialog/oauth', - redirectUri: window.location.origin + '/', + redirectUri: getRedirectUri('/'), requiredUrlParams: ['display', 'scope'], scope: ['email'], scopeDelimiter: ',', @@ -68,7 +72,7 @@ export default { name: 'google', url: '/auth/google', authorizationEndpoint: 'https://accounts.google.com/o/oauth2/auth', - redirectUri: window.location.origin, + redirectUri: getRedirectUri(), requiredUrlParams: ['scope'], optionalUrlParams: ['display'], scope: ['profile', 'email'], @@ -83,7 +87,7 @@ export default { name: 'github', url: '/auth/github', authorizationEndpoint: 'https://github.com/login/oauth/authorize', - redirectUri: window.location.origin, + redirectUri: getRedirectUri(), optionalUrlParams: ['scope'], scope: ['user:email'], scopeDelimiter: ' ', @@ -95,7 +99,7 @@ export default { name: 'instagram', url: '/auth/instagram', authorizationEndpoint: 'https://api.instagram.com/oauth/authorize', - redirectUri: window.location.origin, + redirectUri: getRedirectUri(), requiredUrlParams: ['scope'], scope: ['basic'], scopeDelimiter: '+', @@ -107,7 +111,7 @@ export default { name: 'twitter', url: '/auth/twitter', authorizationEndpoint: 'https://api.twitter.com/oauth/authenticate', - redirectUri: window.location.origin, + redirectUri: getRedirectUri(), oauthType: '1.0', popupOptions: { width: 495, height: 645 } }, @@ -116,7 +120,7 @@ export default { name: 'bitbucket', url: '/auth/bitbucket', authorizationEndpoint: 'https://bitbucket.org/site/oauth2/authorize', - redirectUri: window.location.origin + '/', + redirectUri: getRedirectUri('/'), optionalUrlParams: ['scope'], scope: ['email'], scopeDelimiter: ' ', @@ -128,7 +132,7 @@ export default { name: 'linkedin', url: '/auth/linkedin', authorizationEndpoint: 'https://www.linkedin.com/oauth/v2/authorization', - redirectUri: window.location.origin, + redirectUri: getRedirectUri(), requiredUrlParams: ['state'], scope: ['r_emailaddress'], scopeDelimiter: ' ', @@ -141,7 +145,7 @@ export default { name: 'live', url: '/auth/live', authorizationEndpoint: 'https://login.live.com/oauth20_authorize.srf', - redirectUri: window.location.origin, + redirectUri: getRedirectUri(), requiredUrlParams: ['display', 'scope'], scope: ['wl.emails'], scopeDelimiter: ' ', @@ -154,7 +158,7 @@ export default { name: null, url: '/auth/oauth1', authorizationEndpoint: null, - redirectUri: window.location.origin, + redirectUri: getRedirectUri(), oauthType: '1.0', popupOptions: null }, @@ -163,7 +167,7 @@ export default { name: null, url: '/auth/oauth2', clientId: null, - redirectUri: window.location.origin, + redirectUri: getRedirectUri(), authorizationEndpoint: null, defaultUrlParams: ['response_type', 'client_id', 'redirect_uri'], requiredUrlParams: null, diff --git a/src/storage/cookie-storage.js b/src/storage/cookie-storage.js index 4d76e12..4adc9d5 100644 --- a/src/storage/cookie-storage.js +++ b/src/storage/cookie-storage.js @@ -1,16 +1,18 @@ import { objectExtend, formatCookie, + getCookieDomain, parseCookies } from '../utils.js'; class CookieStorage { constructor(defaultOptions) { this._defaultOptions = objectExtend({ - domain: window.location.hostname, + domain: getCookieDomain(), expires: null, path: '/', - secure: false + secure: false, + getCookieFn: this._getCookie, }, defaultOptions); } @@ -21,7 +23,8 @@ class CookieStorage { } getItem(key) { - const cookies = parseCookies(this._getCookie()); + const options = objectExtend({}, this._defaultOptions); + const cookies = parseCookies(options.getCookieFn()); return cookies.hasOwnProperty(key) ? cookies[key] : null; } @@ -46,4 +49,4 @@ class CookieStorage { } } -export default CookieStorage \ No newline at end of file +export default CookieStorage diff --git a/src/utils.js b/src/utils.js index 883999d..d27f1d2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -77,10 +77,10 @@ export function objectExtend(a, b) { /** * Assemble url from two segments - * + * * @author Sahat Yalkabov * @copyright Method taken from https://github.com/sahat/satellizer - * + * * @param {String} baseUrl Base url * @param {String} url URI * @return {String} @@ -102,10 +102,10 @@ export function joinUrl(baseUrl, url) { /** * Get full path based on current location - * + * * @author Sahat Yalkabov * @copyright Method taken from https://github.com/sahat/satellizer - * + * * @param {Location} location * @return {String} */ @@ -118,10 +118,10 @@ export function getFullUrlPath(location) { /** * Parse query string variables - * + * * @author Sahat Yalkabov * @copyright Method taken from https://github.com/sahat/satellizer - * + * * @param {String} Query string * @return {String} */ @@ -143,7 +143,7 @@ export function parseQueryString(str) { * Decode base64 string * @author Sahat Yalkabov * @copyright Method taken from https://github.com/sahat/satellizer - * + * * @param {String} str base64 encoded string * @return {Object} */ @@ -244,3 +244,12 @@ export function formatCookie(key, value, options) { formatOptions(options) ].join(''); }; + +export function getCookieDomain() { + // Directly check typeof as going through isUndefined seems to break in server environment + return typeof window === 'undefined' ? '' : `${window.location.hostname}`; +} + +export function getRedirectUri(path = '') { + return typeof window === 'undefined' ? path : `${window.location.origin}${path}`; +}