Comparing version 0.1.10 to 1.0.0
{ | ||
"name": "co-auther", | ||
"version": "0.1.10", | ||
"version": "1.0.0", | ||
"description": "", | ||
@@ -30,4 +30,3 @@ "main": "src/co-auther/co-auther.js", | ||
"@angular/platform-browser-dynamic": "2.0.0-rc.2", | ||
"@angular/router": "3.0.0-alpha.6", | ||
"@angular/router-deprecated": "2.0.0-rc.2", | ||
"@angular/router": "3.0.0-alpha.7", | ||
"bootstrap": "git+https://git@github.com/twbs/bootstrap.git#v4-dev", | ||
@@ -34,0 +33,0 @@ "es6-shim": "0.35.0", |
@@ -17,7 +17,2 @@ # co-auther | ||
## Running tests | ||
`npm test` | ||
## Using module | ||
@@ -42,7 +37,7 @@ | ||
// The 3 basic routes | ||
@RouteConfig([ | ||
{path: '/authenticate', as: 'Authenticate', component: AuthenticateCmp, useAsDefault: true}, | ||
{path: '/loggedIn', as: 'LoggedIn', component: LoggedInCmp}, | ||
{path: '/initialRequest', as: 'InitialRequest', component: InitialRequestCmp} | ||
]) | ||
[ | ||
{path: '/authenticate', component: AuthenticateComponent}, | ||
{path: '/logged-in', component: LoggedInComponent}, | ||
{path: '/initialRequest', component: InitialRequestComponent} | ||
] | ||
... | ||
@@ -52,13 +47,15 @@ constructor () { | ||
routes: { | ||
loggedIn: 'LoggedIn', | ||
authenticate: 'Authenticate', | ||
initialRequest: 'InitialRequest' | ||
loggedIn: 'logged-in', | ||
authenticate: 'authenticate', | ||
initialRequest: 'initial-request' | ||
}, | ||
authData: 'authData' | ||
}, (routePath) => { // Register a routing function | ||
this.router.navigate(['/' + routePath]) | ||
authDataKey: 'authData' | ||
}) | ||
} | ||
logout () { | ||
CoAuther.getCoAuther().logoutWrap() | ||
.then((res) => { | ||
window.location.reload() | ||
}) | ||
} | ||
@@ -72,6 +69,8 @@ ``` | ||
... | ||
@CanActivate(() => activationHelper('Authenticate')) | ||
export class AuthenticateCmp { | ||
export class AuthenticateComponent { | ||
login (username, login) { | ||
CoAuther.getCoAuther().loginWrap(username, login) | ||
.then(() => { | ||
this.router.navigate(['logged-in']) | ||
}) | ||
} | ||
@@ -81,27 +80,2 @@ } | ||
Use the activationHelper in the initialRequest route: | ||
```javascript | ||
import {CoAuther} from 'co-auther' | ||
... | ||
@CanActivate(() => CoAuther.activationHelper('InitialRequest')) | ||
``` | ||
And finally use activationHelper in loggedIn route: | ||
```javascript | ||
import {CoAuther} from 'co-auther' | ||
... | ||
@CanActivate(() => CoAuther.activationHelper('LoggedIn')) | ||
``` | ||
In order to remember which terminal route you were aiming for when accessing the GUI, you need this 'hack' in the terminal routes: | ||
```javascript | ||
import {CoAuther} from 'co-auther' | ||
... | ||
@CanActivate(CoAuther.setTerminal) | ||
... | ||
``` | ||
## NOTE: Error handling in apiService | ||
@@ -108,0 +82,0 @@ |
"use strict"; | ||
// Config params | ||
// config params | ||
var config = { | ||
LOGGED_IN: 'loggedIn', | ||
LOGGED_IN: 'logged-in', | ||
AUTHENTICATE: 'authenticate', | ||
INITIAL_REQUEST: 'initialRequest', | ||
AUTH_DATA: 'authData', | ||
dontTouchLocalStorage: true | ||
INITIAL_REQUEST: 'initial-request', | ||
AUTH_DATA_KEY: 'authData' | ||
}; | ||
var terminalRoute = null; | ||
var initialRequestFailed = false; | ||
var coAuther; | ||
// Basic routing function | ||
function basicRouting(afterHash) { | ||
var loc = window.location; | ||
window.location.href = loc.protocol + "//" + loc.host + loc.pathname + "#/" + afterHash; | ||
} | ||
// Basic default route function, should be overridden | ||
var routeFunction = function (afterHash) { | ||
basicRouting(afterHash); | ||
}; | ||
var getCoAuther = function () { | ||
@@ -31,21 +20,17 @@ if (!coAuther) { | ||
exports.getCoAuther = getCoAuther; | ||
// Determine if a route canActivate or not | ||
// determine where to route to based on requested route and authentication state | ||
var initialRequestPending = false; | ||
function activationHelper(destinationRequested) { | ||
var canActivate = false; | ||
var destinationResult = null; | ||
var authData = getCoAuther().getAuthData(); | ||
var authData = localStorage.getItem(config.AUTH_DATA_KEY); | ||
var initialDataLoaded = getCoAuther().isInitialDataLoaded(); | ||
// authData and initialRequest done, you are logged in | ||
// authData and initialRequest done, suggest LOGGED_IN | ||
if (authData && initialDataLoaded) { | ||
destinationResult = config.LOGGED_IN; | ||
canActivate = destinationRequested === destinationResult; | ||
} | ||
else if (!authData && !initialRequestPending) { | ||
destinationResult = config.AUTHENTICATE; | ||
canActivate = destinationRequested === destinationResult; | ||
} | ||
else { | ||
destinationResult = config.INITIAL_REQUEST; | ||
canActivate = destinationRequested === destinationResult; | ||
if (!initialRequestPending && !initialRequestFailed) { | ||
@@ -55,37 +40,21 @@ initialRequestPending = true; | ||
.then(function () { | ||
// initial request successful, suggest LOGGED_IN | ||
initialRequestPending = false; | ||
// initialRequest done, move on to logged in | ||
if (terminalRoute) { | ||
return goToTerminal(); | ||
} | ||
return routeFunction(config.LOGGED_IN); | ||
destinationResult = config.LOGGED_IN; | ||
}) | ||
.catch(function (err) { | ||
// initial request failed, clear auth data from login and go to authenticate | ||
// initial request failed, suggest AUTHENTICATE | ||
initialRequestPending = false; | ||
initialRequestFailed = true; | ||
clearAuthData(); | ||
return routeFunction(config.AUTHENTICATE); | ||
destinationResult = config.AUTHENTICATE; | ||
}); | ||
} | ||
else if (initialRequestFailed && authData) { | ||
// initial request failed, you need to clear authData | ||
console.error('Initial request promise was rejected. You have manual authData management and need to clear authData from localStorage manually.'); | ||
} | ||
} | ||
if (!canActivate) { | ||
routeFunction(destinationResult); | ||
return canActivate; | ||
} | ||
return canActivate; | ||
return destinationResult; | ||
} | ||
exports.activationHelper = activationHelper; | ||
// terminal memory | ||
function setTerminal() { | ||
terminalRoute = window.location.hash.substring(2); | ||
return true; | ||
} | ||
exports.setTerminal = setTerminal; | ||
function goToTerminal() { | ||
basicRouting(terminalRoute); | ||
} | ||
function CoAuther(apiService) { | ||
@@ -96,3 +65,2 @@ var initialDataLoaded = false; | ||
} | ||
var initialRequestRes; | ||
function loginWrap() { | ||
@@ -103,9 +71,5 @@ var args = []; | ||
} | ||
initialRequestFailed = false; // reset this one | ||
return apiService.login.apply(apiService, args) | ||
.then(function (res) { | ||
// authData has arrived, go make initial request | ||
setAuthData(res); | ||
routeFunction(config.INITIAL_REQUEST); | ||
}); | ||
// if initial request failed before, consider this a retry | ||
initialRequestFailed = false; | ||
return apiService.login.apply(apiService, args); | ||
} | ||
@@ -117,11 +81,8 @@ function logoutWrap() { | ||
} | ||
return apiService.logout.apply(apiService, args) | ||
.then(function () { | ||
clearAuthData(); | ||
}); | ||
return apiService.logout.apply(apiService, args); | ||
} | ||
function makeInitialRequestWrap() { | ||
return apiService.makeInitialRequest() | ||
.then(function (data) { | ||
// Flag for intial data | ||
.then(function () { | ||
// flag for initial data | ||
initialDataLoaded = true; | ||
@@ -134,15 +95,10 @@ }); | ||
makeInitialRequestWrap: makeInitialRequestWrap, | ||
getAuthData: getAuthData, | ||
isInitialDataLoaded: isInitialDataLoaded | ||
}; | ||
} | ||
function initialize(apiService, newConfig, newRouteFunction) { | ||
function initialize(apiService, newConfig) { | ||
coAuther = CoAuther(apiService); | ||
if (newConfig.authData) { | ||
config.AUTH_DATA = newConfig.authData; | ||
if (newConfig.authDataKey) { | ||
config.AUTH_DATA_KEY = newConfig.authDataKey; | ||
} | ||
// If someone set the value specifically | ||
if (newConfig.dontTouchLocalStorage === false || true) { | ||
config.dontTouchLocalStorage = newConfig.dontTouchLocalStorage; | ||
} | ||
if (newConfig.routes) { | ||
@@ -159,20 +115,4 @@ if (newConfig.routes.loggedIn) { | ||
} | ||
if (newRouteFunction) { | ||
routeFunction = newRouteFunction; | ||
} | ||
} | ||
exports.initialize = initialize; | ||
function clearAuthData() { | ||
if (!config.dontTouchLocalStorage) { | ||
localStorage.removeItem(config.AUTH_DATA); | ||
} | ||
} | ||
function getAuthData() { | ||
return localStorage.getItem(config.AUTH_DATA); | ||
} | ||
function setAuthData(authData) { | ||
if (!config.dontTouchLocalStorage) { | ||
localStorage.setItem(config.AUTH_DATA, authData); | ||
} | ||
} | ||
//# sourceMappingURL=co-auther.js.map |
@@ -1,23 +0,12 @@ | ||
// Config params | ||
// config params | ||
let config = { | ||
LOGGED_IN: 'loggedIn', | ||
LOGGED_IN: 'logged-in', | ||
AUTHENTICATE: 'authenticate', | ||
INITIAL_REQUEST: 'initialRequest', | ||
AUTH_DATA: 'authData', | ||
dontTouchLocalStorage: true | ||
INITIAL_REQUEST: 'initial-request', | ||
AUTH_DATA_KEY: 'authData' | ||
} | ||
let terminalRoute = null | ||
let initialRequestFailed = false | ||
let coAuther | ||
// Basic routing function | ||
function basicRouting (afterHash) { | ||
let loc = window.location | ||
window.location.href = `${loc.protocol}//${loc.host}${loc.pathname}#/${afterHash}` | ||
} | ||
// Basic default route function, should be overridden | ||
let routeFunction = (afterHash) => { | ||
basicRouting(afterHash) | ||
} | ||
let getCoAuther = function () { | ||
@@ -31,24 +20,20 @@ if (!coAuther) { | ||
// Determine if a route canActivate or not | ||
// determine where to route to based on requested route and authentication state | ||
var initialRequestPending = false | ||
function activationHelper (destinationRequested): boolean { | ||
let canActivate = false | ||
function activationHelper (destinationRequested) { | ||
let destinationResult = null | ||
let authData = getCoAuther().getAuthData() | ||
let authData = localStorage.getItem(config.AUTH_DATA_KEY) | ||
let initialDataLoaded = getCoAuther().isInitialDataLoaded() | ||
// authData and initialRequest done, you are logged in | ||
// authData and initialRequest done, suggest LOGGED_IN | ||
if (authData && initialDataLoaded) { | ||
destinationResult = config.LOGGED_IN | ||
canActivate = destinationRequested === destinationResult | ||
// no authData and no initialRequest pending, go to authentication page | ||
// no authData and no initialRequest pending, suggest AUTHENTICATE | ||
} else if (!authData && !initialRequestPending) { | ||
destinationResult = config.AUTHENTICATE | ||
canActivate = destinationRequested === destinationResult | ||
// there is authData, go make initial request | ||
// authData is available, suggest INITIAL_REQUEST | ||
} else { | ||
destinationResult = config.INITIAL_REQUEST | ||
canActivate = destinationRequested === destinationResult | ||
if (!initialRequestPending && !initialRequestFailed) { | ||
@@ -58,52 +43,35 @@ initialRequestPending = true | ||
.then(() => { | ||
// initial request successful, suggest LOGGED_IN | ||
initialRequestPending = false | ||
// initialRequest done, move on to logged in | ||
if (terminalRoute) { | ||
return goToTerminal() | ||
} | ||
return routeFunction(config.LOGGED_IN) | ||
destinationResult = config.LOGGED_IN | ||
}) | ||
.catch((err) => { | ||
// initial request failed, clear auth data from login and go to authenticate | ||
// initial request failed, suggest AUTHENTICATE | ||
initialRequestPending = false | ||
initialRequestFailed = true | ||
clearAuthData() | ||
return routeFunction(config.AUTHENTICATE) | ||
destinationResult = config.AUTHENTICATE | ||
}) | ||
} else if (initialRequestFailed && authData) { | ||
// initial request failed, you need to clear authData | ||
console.error('Initial request promise was rejected. You have manual authData management and need to clear authData from localStorage manually.') | ||
} | ||
} | ||
if (!canActivate) { | ||
routeFunction(destinationResult) | ||
return canActivate | ||
} | ||
return canActivate | ||
} | ||
// terminal memory | ||
function setTerminal () { | ||
terminalRoute = window.location.hash.substring(2) | ||
return true | ||
return destinationResult | ||
} | ||
function goToTerminal () { | ||
basicRouting(terminalRoute) | ||
} | ||
function CoAuther (apiService) { | ||
let initialDataLoaded = false | ||
function isInitialDataLoaded () { | ||
return initialDataLoaded | ||
} | ||
let initialRequestRes | ||
function loginWrap (...args) { | ||
initialRequestFailed = false // reset this one | ||
// if initial request failed before, consider this a retry | ||
initialRequestFailed = false | ||
return apiService.login.apply(apiService, args) | ||
.then((res) => { | ||
// authData has arrived, go make initial request | ||
setAuthData(res) | ||
routeFunction(config.INITIAL_REQUEST) | ||
}) | ||
} | ||
@@ -113,5 +81,2 @@ | ||
return apiService.logout.apply(apiService, args) | ||
.then(() => { | ||
clearAuthData() | ||
}) | ||
} | ||
@@ -121,4 +86,4 @@ | ||
return apiService.makeInitialRequest() | ||
.then((data) => { | ||
// Flag for intial data | ||
.then(() => { | ||
// flag for initial data | ||
initialDataLoaded = true | ||
@@ -132,3 +97,2 @@ }) | ||
makeInitialRequestWrap, | ||
getAuthData, | ||
isInitialDataLoaded | ||
@@ -138,11 +102,8 @@ } | ||
function initialize (apiService, newConfig, newRouteFunction?) { | ||
function initialize (apiService, newConfig) { | ||
coAuther = CoAuther(apiService) | ||
if (newConfig.authData) { | ||
config.AUTH_DATA = newConfig.authData | ||
if (newConfig.authDataKey) { | ||
config.AUTH_DATA_KEY = newConfig.authDataKey | ||
} | ||
// If someone set the value specifically | ||
if (newConfig.dontTouchLocalStorage === false || true) { | ||
config.dontTouchLocalStorage = newConfig.dontTouchLocalStorage | ||
} | ||
if (newConfig.routes) { | ||
@@ -159,28 +120,8 @@ if (newConfig.routes.loggedIn) { | ||
} | ||
if (newRouteFunction) { | ||
routeFunction = newRouteFunction | ||
} | ||
} | ||
function clearAuthData () { | ||
if (!config.dontTouchLocalStorage) { | ||
localStorage.removeItem(config.AUTH_DATA) | ||
} | ||
} | ||
function getAuthData () { | ||
return localStorage.getItem(config.AUTH_DATA) | ||
} | ||
function setAuthData (authData) { | ||
if (!config.dontTouchLocalStorage) { | ||
localStorage.setItem(config.AUTH_DATA, authData); | ||
} | ||
} | ||
export { | ||
initialize, | ||
getCoAuther, | ||
activationHelper, | ||
setTerminal | ||
activationHelper | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
20
1
1
62570
77
1095
95
3