Skip to content
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

Don't reference window in default options if it doesn't exist #73

Open
wants to merge 6 commits 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
28 changes: 16 additions & 12 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { getCookieDomain, getRedirectUri } from './utils.js';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two functions are contextually highly connected only to options.jsfile and nowhere else. There is no reason they should be located in utils.js file. Please move them back in options.js file.



/**
* Default configuration
*/
Expand All @@ -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',
Expand Down Expand Up @@ -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: ',',
Expand All @@ -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'],
Expand All @@ -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: ' ',
Expand All @@ -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: '+',
Expand All @@ -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 }
},
Expand All @@ -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: ' ',
Expand All @@ -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: ' ',
Expand All @@ -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: ' ',
Expand All @@ -154,7 +158,7 @@ export default {
name: null,
url: '/auth/oauth1',
authorizationEndpoint: null,
redirectUri: window.location.origin,
redirectUri: getRedirectUri(),
oauthType: '1.0',
popupOptions: null
},
Expand All @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions src/storage/cookie-storage.js
Original file line number Diff line number Diff line change
@@ -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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have time to think about this logic these past few days, but my first impression is that I don't like it too much. The main reason is that the ability to authenticate a user on the server side is by using cookies. I will have to think a bit more about this.

For now, I suggest that you make it compilable in SSR, and later we can try to think about a solution to persist authentication state over SSR.

}, defaultOptions);
}

Expand All @@ -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;
}

Expand All @@ -46,4 +49,4 @@ class CookieStorage {
}
}

export default CookieStorage
export default CookieStorage
23 changes: 16 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ export function objectExtend(a, b) {

/**
* Assemble url from two segments
*
*
* @author Sahat Yalkabov <https://github.com/sahat>
* @copyright Method taken from https://github.com/sahat/satellizer
*
*
* @param {String} baseUrl Base url
* @param {String} url URI
* @return {String}
Expand All @@ -102,10 +102,10 @@ export function joinUrl(baseUrl, url) {

/**
* Get full path based on current location
*
*
* @author Sahat Yalkabov <https://github.com/sahat>
* @copyright Method taken from https://github.com/sahat/satellizer
*
*
* @param {Location} location
* @return {String}
*/
Expand All @@ -118,10 +118,10 @@ export function getFullUrlPath(location) {

/**
* Parse query string variables
*
*
* @author Sahat Yalkabov <https://github.com/sahat>
* @copyright Method taken from https://github.com/sahat/satellizer
*
*
* @param {String} Query string
* @return {String}
*/
Expand All @@ -143,7 +143,7 @@ export function parseQueryString(str) {
* Decode base64 string
* @author Sahat Yalkabov <https://github.com/sahat>
* @copyright Method taken from https://github.com/sahat/satellizer
*
*
* @param {String} str base64 encoded string
* @return {Object}
*/
Expand Down Expand Up @@ -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}`;
}