vue-jwt-auth
Advanced tools
Comparing version 0.8.1 to 0.9.0
@@ -19,3 +19,3 @@ { | ||
}, | ||
"version": "0.8.1" | ||
"version": "0.9.0" | ||
} |
@@ -90,15 +90,15 @@ # vue-jwt-auth | ||
`**true**` | ||
**`true`** | ||
User must be authenticated (no roles are checked). | ||
`**false**` | ||
**`false`** | ||
If the user is logged in then this route will be unavailable. Useful for login/register type pages to be unaccessible once the user is logged in. | ||
`**undefined**` | ||
**`undefined`** | ||
Public, no checks required. | ||
`**Array**` or `**String**` | ||
**`Array`** or **`String`** | ||
@@ -112,3 +112,3 @@ The user must be logged in. Additionally the string or array will be checked against the users roles. | ||
`**ready()**` | ||
**`ready()`** | ||
@@ -119,3 +119,3 @@ When the app boots the `vue-jwt-auth` plugin will fire off a check for a token and subsequent user fetch. In the first instance of this the app will be set to "ready". | ||
`**check()**` | ||
**`check()`** | ||
@@ -131,3 +131,3 @@ Check if the user is logged in. It can also be used to check if the user has a specific role. | ||
`**user()**` | ||
**`user()`** | ||
@@ -137,3 +137,3 @@ Fetch the user object stored in the `$auth` instance. | ||
`**login()**` | ||
**`login()`** | ||
@@ -158,3 +158,3 @@ Login the user which accepts four parameters. | ||
`**facebook()**` `**google()**` | ||
**`facebook()`** **`google()`** | ||
@@ -193,3 +193,3 @@ These follows the same format as the `$auth.login()` method. | ||
`**logout()**` | ||
**`logout()`** | ||
@@ -212,15 +212,23 @@ Log the user out. | ||
`**loginAs(data, redirectUrl, options)**` | ||
**`loginAs(data, redirectUrl, options)`** | ||
Login as another user. | ||
`**logoutAs(redirectUrl)**` | ||
**`logoutAs(redirectUrl)`** | ||
Logout or other user. Will revert to previously logged in user. | ||
`**other()**` | ||
**`other()`** | ||
Check if we are currently logged in as another user. | ||
**`getAuthHeader()`** | ||
Returns the auth header. Useful when integration third part libraries. | ||
**`version()`** | ||
Returns current version. | ||
## Options | ||
@@ -230,10 +238,14 @@ | ||
`**authType**` | ||
**`authType`** | ||
**default:** 'bearer' | ||
Set the auth type. Only `bearer` is supported at this time. | ||
**`tokenType`** | ||
`**fetchUrl**` | ||
**default:** 'jwt' | ||
If set to 'jwt' will check expiry time for refreshes, otherwise no other implementation exists and it will just send the token directly. | ||
**`fetchUrl`** | ||
**default:** '/auth/user' | ||
@@ -243,3 +255,3 @@ | ||
`**tokenUrl**` | ||
**`tokenUrl`** | ||
@@ -250,3 +262,3 @@ **default:** '/auth/token' | ||
`**loginUrl**` | ||
**`loginUrl`** | ||
@@ -257,3 +269,3 @@ **default:** '/auth/login' | ||
`**registerUrl**` | ||
**`registerUrl`** | ||
@@ -264,3 +276,3 @@ **default:** '/auth/register' | ||
`**loginRedirect**` | ||
**`loginRedirect`** | ||
@@ -271,3 +283,3 @@ **default:** '/login' | ||
`**notFoundRedirect**` | ||
**`notFoundRedirect`** | ||
@@ -278,3 +290,3 @@ **default:** '/404' | ||
`**forbiddenRedirect**` | ||
**`forbiddenRedirect`** | ||
@@ -285,3 +297,3 @@ **default:** '/403' | ||
`**rolesVar**` | ||
**`rolesVar`** | ||
@@ -292,3 +304,3 @@ **default:** 'roles' | ||
`**tokenVar**` | ||
**`tokenVar`** | ||
@@ -299,3 +311,3 @@ **default:** 'token' | ||
`**tokenName**` | ||
**`tokenName`** | ||
@@ -306,4 +318,10 @@ **default:** 'jwt-auth-token' | ||
`**cookieDomain**` | ||
**`tokenTimeoutOffset`** | ||
**default:** 5000 | ||
Offset for token timeout (for some buffer). | ||
**`cookieDomain`** | ||
**default:** _cookieDomain | ||
@@ -313,3 +331,3 @@ | ||
`**userData**` | ||
**`userData`** | ||
@@ -320,3 +338,3 @@ **default:** _userData | ||
`**beforeEach**` | ||
**`beforeEach`** | ||
@@ -327,4 +345,10 @@ **default:** _beforeEach | ||
`**facebookUrl**` | ||
**`invalidToken`** | ||
**default:** _invalidToken | ||
Method to run if a `401` is hit during an API request. By default will run logout. | ||
**`facebookUrl`** | ||
**default:** '/auth/facebook' | ||
@@ -334,3 +358,3 @@ | ||
`**facebookAppId**` | ||
**`facebookAppId`** | ||
@@ -341,3 +365,3 @@ **default:** '' | ||
`**facebookScope**` | ||
**`facebookScope`** | ||
@@ -348,3 +372,3 @@ **default:** 'email' | ||
`**facebookRedirect**` | ||
**`facebookRedirect`** | ||
@@ -355,3 +379,3 @@ **default:** _getUrl() + '/login/facebook' | ||
`**googleUrl**` | ||
**`googleUrl`** | ||
@@ -362,3 +386,3 @@ **default:** '/auth/google' | ||
`**googleAppId**` | ||
**`googleAppId`** | ||
@@ -369,3 +393,3 @@ **default:** '' | ||
`**googleScope**` | ||
**`googleScope`** | ||
@@ -376,3 +400,3 @@ **default:** 'https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read' | ||
`**googleRedirect**` | ||
**`googleRedirect`** | ||
@@ -379,0 +403,0 @@ **default:** _getUrl() + '/login/google' |
@@ -26,2 +26,4 @@ module.exports = (function () { | ||
function _userData (res) { | ||
res = res.json(); | ||
return res.data || res | ||
@@ -71,2 +73,33 @@ } | ||
function _http (options) { | ||
// Version 0.8.0 | ||
this.$http[options.method || 'get'](options.url, options.data).then(options.success, options.error); | ||
} | ||
function _interceptor (Vue, req, res) { | ||
// Version 0.8.0 | ||
Vue.http.interceptors.push((request, next) => { | ||
if (req) { req.call(this, request); } | ||
next((response) => { | ||
if (res) { res.call(this, response); } | ||
}); | ||
}); | ||
// // Version before Promise implementation. | ||
// else { | ||
// Vue.http.interceptors.push({ | ||
// request (request) { | ||
// if (req) { req.call(this, request); } | ||
// }, | ||
// response (response) { | ||
// if (res) { res.call(this, response); } | ||
// } | ||
// }); | ||
// } | ||
} | ||
// Remember Me | ||
@@ -110,17 +143,21 @@ | ||
function _refreshToken () { | ||
var _this = this | ||
var _this = this; | ||
if (_getToken.call(this)) { | ||
this.$http.get(this.getOption('tokenUrl'), function (res) { | ||
var tokenJSON = _decodeToken(_getToken.call(_this)), | ||
expireTime = _getTokenExpirationDate(tokenJSON).valueOf(), | ||
nowTime = new Date().valueOf(), | ||
offsetTime = this.getOption('tokenTimeoutOffset'), | ||
timeout = expireTime - nowTime - offsetTime; | ||
if (_getToken.call(this) && this.getOption('tokenType') === 'jwt') { | ||
_http.call(this, { | ||
url: this.getOption('tokenUrl'), | ||
method: 'get', | ||
success: () => { | ||
var tokenJSON = _decodeToken(_getToken.call(_this)), | ||
expireTime = _getTokenExpirationDate(tokenJSON).valueOf(), | ||
nowTime = new Date().valueOf(), | ||
offsetTime = this.getOption('tokenTimeoutOffset'), | ||
timeout = expireTime - nowTime - offsetTime; | ||
clearTimeout(_tokenRefreshTimeout) | ||
clearTimeout(_tokenRefreshTimeout); | ||
_tokenRefreshTimeout = setTimeout(function () { | ||
_refreshToken.call(_this) | ||
}, timeout) | ||
_tokenRefreshTimeout = setTimeout(function () { | ||
_refreshToken.call(_this); | ||
}, timeout); | ||
} | ||
}); | ||
@@ -187,27 +224,31 @@ } | ||
this.$http.post(path, data, function (res) { | ||
var _this = this | ||
_http.call(this, { | ||
url: path, | ||
method: 'post', | ||
data: data, | ||
success: (res) => { | ||
var _this = this | ||
_setRememberMeCookie.call(this, rememberMe) | ||
_setRememberMeCookie.call(this, rememberMe); | ||
_setToken.call(this, res[this.getOption('tokenVar')]) | ||
_setToken.call(this, res.json()[this.getOption('tokenVar')]); | ||
this.authenticated = null | ||
this.authenticated = null; | ||
this.fetch(function () { | ||
if (options.success) { | ||
options.success.call(_this, res) | ||
} | ||
this.fetch(function () { | ||
if (options.success) { | ||
options.success.call(_this, res); | ||
} | ||
if (redirectUrl && _this.check()) { | ||
_this.$router.go(redirectUrl) | ||
} | ||
}) | ||
}, { | ||
error (res) { | ||
if (redirectUrl && _this.check()) { | ||
_this.$router.go(redirectUrl); | ||
} | ||
}); | ||
}, | ||
error: (res) => { | ||
if (options.error) { | ||
options.error.call(this, res) | ||
options.error.call(this, res); | ||
} | ||
} | ||
}) | ||
}); | ||
} | ||
@@ -251,15 +292,18 @@ | ||
this.$http.get(this.getOption('fetchUrl'), function (res) { | ||
this.authenticated = true | ||
this.data = this.getOption('userData').call(this, res) | ||
this.loaded = true | ||
_http.call(this, { | ||
url: this.getOption('fetchUrl'), | ||
method: 'get', | ||
success: (res) => { | ||
this.authenticated = true; | ||
this.data = this.getOption('userData').call(this, res); | ||
this.loaded = true; | ||
return cb() | ||
}, { | ||
error () { | ||
this.loaded = true | ||
return cb(); | ||
}, | ||
error: () => { | ||
this.loaded = true; | ||
return cb() | ||
return cb(); | ||
} | ||
}) | ||
}); | ||
} | ||
@@ -272,2 +316,3 @@ | ||
authType: 'bearer', | ||
tokenType: 'jwt', | ||
@@ -358,4 +403,2 @@ fetchUrl: 'auth/user', | ||
check (role) { | ||
var token = _getToken.call(this) | ||
if (this.data !== null) { | ||
@@ -411,23 +454,27 @@ if (role) { | ||
this.$http.post(this.getOption('loginAsUrl'), data, function (res) { | ||
var _this = this | ||
_http.call(this, { | ||
url: this.getOption('loginAsUrl'), | ||
method: 'post', | ||
data: data, | ||
success: (res) => { | ||
var _this = this; | ||
localStorage.setItem('login-as-' + this.getOption('tokenName'), res[this.getOption('tokenVar')]) | ||
localStorage.setItem('login-as-' + this.getOption('tokenName'), res.json()[this.getOption('tokenVar')]); | ||
_fetch.call(this, function () { | ||
if (options.success) { | ||
options.success.call(this, res) | ||
} | ||
_fetch.call(this, function () { | ||
if (options.success) { | ||
options.success.call(this, res); | ||
} | ||
if (redirectUrl && _this.check()) { | ||
_this.$router.go(redirectUrl) | ||
} | ||
}) | ||
}, { | ||
error (res) { | ||
if (redirectUrl && _this.check()) { | ||
_this.$router.go(redirectUrl); | ||
} | ||
}) | ||
}, | ||
error: (res) => { | ||
if (options.error) { | ||
options.error.call(this, res) | ||
options.error.call(this, res); | ||
} | ||
} | ||
}) | ||
}); | ||
}, | ||
@@ -451,2 +498,18 @@ | ||
return localStorage.getItem('login-as-' + this.getOption('tokenName')) | ||
}, | ||
// Token | ||
getAuthHeader() { | ||
var token = _getToken.call(this); | ||
if (token && this.getOption('authType') === 'bearer') { | ||
return 'Bearer: ' + token; | ||
} | ||
return false; | ||
}, | ||
version() { | ||
return '0.9.0'; | ||
} | ||
@@ -481,36 +544,28 @@ } | ||
// Set interceptors. | ||
Vue.http.interceptors.push({ | ||
// Send auth token on each request. | ||
request (req) { | ||
var token = _getToken.call(auth) | ||
_interceptor(Vue, (req) => { | ||
var token = _getToken.call(auth) | ||
if (token && auth.getOption('authType') === 'bearer') { | ||
req.headers.Authorization = 'Bearer: ' + token | ||
} | ||
return req | ||
}, | ||
if (token && auth.getOption('authType') === 'bearer') { | ||
req.headers.Authorization = 'Bearer: ' + token | ||
} | ||
}, | ||
// Reset auth token if provided in response. | ||
response (res) { | ||
var authorization = res.headers('Authorization'), | ||
invalidTokenMethod = auth.getOption('invalidToken') | ||
// Reset auth token if provided in response. | ||
(res) => { | ||
var authorization = res.headers.Authorization, | ||
invalidTokenMethod = auth.getOption('invalidToken') | ||
if (authorization) { | ||
authorization = authorization.split(' ') | ||
if (authorization) { | ||
authorization = authorization.split(' ') | ||
if (authorization[1]) { | ||
_setToken.call(auth, authorization[1]) | ||
} | ||
if (authorization[1]) { | ||
_setToken.call(auth, authorization[1]) | ||
} | ||
} | ||
if (invalidTokenMethod) { | ||
invalidTokenMethod.bind(auth)(res) | ||
} | ||
return res | ||
if (invalidTokenMethod) { | ||
invalidTokenMethod.bind(auth)(res) | ||
} | ||
}) | ||
}); | ||
} | ||
})() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
426
376
26036
3