Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "co-auther", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "src/co-auther/co-auther.js", |
@@ -69,3 +69,3 @@ # co-auther | ||
... | ||
@CanActivate(() => activationHelper('authenticate')) | ||
@CanActivate(() => activationHelper('Authenticate')) | ||
export class AuthenticateCmp { | ||
@@ -83,3 +83,3 @@ login (username, login) { | ||
... | ||
@CanActivate(() => activationHelper('initialRequest')) | ||
@CanActivate(() => activationHelper('InitialRequest')) | ||
``` | ||
@@ -92,3 +92,3 @@ | ||
... | ||
@CanActivate(() => activationHelper('loggedIn')) | ||
@CanActivate(() => activationHelper('LoggedIn')) | ||
``` | ||
@@ -103,2 +103,23 @@ | ||
... | ||
``` | ||
## NOTE: Error handling in apiService | ||
When writing the error handling in the apiService you will want to use the .catch() of the promises. The problem is that once you've caught a rejected promise, it will bubble up as a resolved promise, so the parent will get a .then(). You can fix it by creating a new promise in your apiService and "rethrow" the error. In the file "example-common/api-service.ts" there is an example of this for the "initialRequest" function. | ||
```javascript | ||
let apiService = { | ||
login () { | ||
return new Promise((resolve, reject) => { | ||
return myRequest | ||
.then((data) => { | ||
resolve(data) | ||
}) | ||
.catch((err) => { | ||
myCustomErrorHandler(err) | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
} | ||
``` |
@@ -48,3 +48,4 @@ var dontTouchLocalStorage = false; | ||
initialRequestPending = true; | ||
getCoAuther().makeInitialRequestWrap().then(function () { | ||
getCoAuther().makeInitialRequestWrap() | ||
.then(function () { | ||
initialRequestPending = false; | ||
@@ -56,2 +57,8 @@ // initialRequest done, move on to logged in | ||
return routeFunction(config.LOGGED_IN); | ||
}) | ||
.catch(function () { | ||
initialRequestPending = false; | ||
// initial request failed, clear auth data from login and go to authenticate | ||
clearAuthData(); | ||
return routeFunction(config.AUTHENTICATE); | ||
}); | ||
@@ -75,3 +82,2 @@ } | ||
} | ||
exports.goToTerminal = goToTerminal; | ||
function CoAuther(apiService) { | ||
@@ -88,3 +94,3 @@ var initialDataLoaded = false; | ||
} | ||
apiService.login.apply(apiService, args) | ||
return apiService.login.apply(apiService, args) | ||
.then(function (res) { | ||
@@ -94,6 +100,2 @@ // authData has arrived, go make initial request | ||
routeFunction(config.INITIAL_REQUEST); | ||
}) | ||
.catch(function (err) { | ||
// Login failed, handle | ||
console.log('login fail'); | ||
}); | ||
@@ -106,29 +108,19 @@ } | ||
} | ||
apiService.logout.apply(apiService, args) | ||
return apiService.logout.apply(apiService, args) | ||
.then(function () { | ||
clearAuthData(); | ||
// Logged out, reload page | ||
window.location.reload(); | ||
}); | ||
} | ||
function makeInitialRequestWrap() { | ||
return new Promise(function (resolve, reject) { | ||
apiService.makeInitialRequest() | ||
.then(function () { | ||
// Flag for intial data | ||
initialDataLoaded = true; | ||
resolve(); | ||
}) | ||
.catch(function (error) { | ||
// TODO handle failed initial request | ||
clearAuthData(); | ||
reject(); | ||
}); | ||
return apiService.makeInitialRequest() | ||
.then(function (data) { | ||
// Flag for intial data | ||
initialDataLoaded = true; | ||
}); | ||
} | ||
return { | ||
loginWrap: loginWrap, | ||
logoutWrap: logoutWrap, | ||
makeInitialRequestWrap: makeInitialRequestWrap, | ||
loginWrap: loginWrap, | ||
getAuthData: getAuthData, | ||
logoutWrap: logoutWrap, | ||
isInitialDataLoaded: isInitialDataLoaded | ||
@@ -135,0 +127,0 @@ }; |
@@ -46,10 +46,17 @@ let dontTouchLocalStorage = false | ||
initialRequestPending = true | ||
getCoAuther().makeInitialRequestWrap().then(() => { | ||
initialRequestPending = false | ||
// initialRequest done, move on to logged in | ||
if (terminalRoute) { | ||
return goToTerminal() | ||
} | ||
return routeFunction(config.LOGGED_IN) | ||
}) | ||
getCoAuther().makeInitialRequestWrap() | ||
.then(() => { | ||
initialRequestPending = false | ||
// initialRequest done, move on to logged in | ||
if (terminalRoute) { | ||
return goToTerminal() | ||
} | ||
return routeFunction(config.LOGGED_IN) | ||
}) | ||
.catch(() => { | ||
initialRequestPending = false | ||
// initial request failed, clear auth data from login and go to authenticate | ||
clearAuthData() | ||
return routeFunction(config.AUTHENTICATE) | ||
}) | ||
} | ||
@@ -80,3 +87,3 @@ } | ||
function loginWrap (...args) { | ||
apiService.login.apply(apiService, args) | ||
return apiService.login.apply(apiService, args) | ||
.then((res) => { | ||
@@ -87,14 +94,8 @@ // authData has arrived, go make initial request | ||
}) | ||
.catch((err) => { | ||
// Login failed, handle | ||
console.log('login fail') | ||
}) | ||
} | ||
function logoutWrap (...args) { | ||
apiService.logout.apply(apiService, args) | ||
return apiService.logout.apply(apiService, args) | ||
.then(() => { | ||
clearAuthData() | ||
// Logged out, reload page | ||
window.location.reload() | ||
}) | ||
@@ -104,22 +105,14 @@ } | ||
function makeInitialRequestWrap () { | ||
return new Promise((resolve, reject) => { | ||
apiService.makeInitialRequest() | ||
.then(() => { | ||
// Flag for intial data | ||
initialDataLoaded = true | ||
resolve() | ||
}) | ||
.catch((error) => { | ||
// TODO handle failed initial request | ||
clearAuthData() | ||
reject() | ||
}) | ||
}) | ||
return apiService.makeInitialRequest() | ||
.then((data) => { | ||
// Flag for intial data | ||
initialDataLoaded = true | ||
}) | ||
} | ||
return { | ||
loginWrap, | ||
logoutWrap, | ||
makeInitialRequestWrap, | ||
loginWrap, | ||
getAuthData, | ||
logoutWrap, | ||
isInitialDataLoaded | ||
@@ -175,4 +168,3 @@ } | ||
activationHelper, | ||
setTerminal, | ||
goToTerminal | ||
setTerminal | ||
} |
@@ -10,4 +10,16 @@ // Simple example of an authentication API service | ||
makeInitialRequest: function () { | ||
// Some timeout just to show the loading route. | ||
return mockRequest('Initial request', 1000); | ||
// Here's an example of how to handle errors in the apiService. Since coAuther | ||
// needs to get the error in the .catch() it's necessary to create a new promise | ||
// and "rethrow" the error again. | ||
return new Promise(function (resolve, reject) { | ||
// Some timeout just to show the loading route. | ||
return mockRequest('Initial request', 500) | ||
.then(function (data) { | ||
resolve(data); | ||
}) | ||
.catch(function (err) { | ||
customErrorHandler(err); | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
@@ -31,2 +43,5 @@ }; | ||
} | ||
function customErrorHandler(err) { | ||
console.log('do amazing stuff with this error:', err); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -33,0 +48,0 @@ exports.default = apiService; |
@@ -13,4 +13,17 @@ // Simple example of an authentication API service | ||
makeInitialRequest () { | ||
// Some timeout just to show the loading route. | ||
return mockRequest('Initial request', 1000) | ||
// Here's an example of how to handle errors in the apiService. Since coAuther | ||
// needs to get the error in the .catch() it's necessary to create a new promise | ||
// and "rethrow" the error again. | ||
return new Promise((resolve, reject) => { | ||
// Some timeout just to show the loading route. | ||
return mockRequest('Initial request', 500) | ||
.then((data) => { | ||
resolve(data) | ||
}) | ||
.catch((err) => { | ||
customErrorHandler(err) | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
@@ -35,2 +48,6 @@ } | ||
function customErrorHandler (err) { | ||
console.log('do amazing stuff with this error:', err) | ||
} | ||
export default apiService |
@@ -17,3 +17,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
AuthenticateCmp.prototype.login = function (username, password) { | ||
co_auther_1.getCoAuther().loginWrap(username, password); | ||
co_auther_1.getCoAuther().loginWrap(username, password) | ||
.then(function (successMsg) { | ||
console.log(successMsg); | ||
}) | ||
.catch(function (errMsg) { | ||
console.log(errMsg); | ||
}); | ||
}; | ||
@@ -20,0 +26,0 @@ AuthenticateCmp = __decorate([ |
@@ -29,3 +29,9 @@ import {Component} from 'angular2/core' | ||
getCoAuther().loginWrap(username, password) | ||
.then((successMsg) => { | ||
console.log(successMsg) | ||
}) | ||
.catch((errMsg) => { | ||
console.log(errMsg) | ||
}) | ||
} | ||
} |
@@ -19,3 +19,3 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
} | ||
LoggedInCmp.prototype.logOut = function ($event) { | ||
LoggedInCmp.prototype.logout = function ($event) { | ||
$event.preventDefault(); | ||
@@ -32,6 +32,5 @@ co_auther_1.getCoAuther().logoutWrap(); | ||
directives: [router_2.ROUTER_DIRECTIVES], | ||
template: "\n <nav class=\"navbar navbar-dark navbar-fixed-top bg-inverse\">\n <button type=\"button\" class=\"navbar-toggler hidden-sm-up\" data-toggle=\"collapse\" data-target=\"#navbar\" aria-expanded=\"false\" aria-controls=\"navbar\">\n <span class=\"sr-only\">Toggle navigation</span>\n <span class=\"icon-bar\"></span>\n <span class=\"icon-bar\"></span>\n <span class=\"icon-bar\"></span>\n </button>\n <a class=\"navbar-brand\" href=\"#\">Project name</a>\n <div id=\"navbar\">\n <nav class=\"nav navbar-nav pull-xs-left\">\n <a class=\"nav-item nav-link\" href=\"#\" (click)=\"logOut($event)\">Logout</a>\n </nav>\n <form class=\"pull-xs-right\">\n <input type=\"text\" class=\"form-control\" placeholder=\"Search...\">\n </form>\n </div>\n </nav>\n\n <div class=\"container-fluid\">\n <div class=\"row\">\n <div class=\"col-sm-3 col-md-2 sidebar\">\n <ul class=\"nav nav-sidebar\">\n <li class=\"active\"><a [routerLink]=\"['/LoggedIn/Dashboard']\">Dashboard<span class=\"sr-only\">(current)</span></a></li>\n <li><a [routerLink]=\"['/LoggedIn/Settings']\">Settings</a></li>\n </ul>\n <ul class=\"nav nav-sidebar\">\n <li><a href=\"\">Dummy item 1</a></li>\n <li><a href=\"\">Dummy item 2</a></li>\n </ul>\n </div>\n <div class=\"col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main\">\n <router-outlet></router-outlet>\n </div>\n </div>\n </div>\n " | ||
template: "\n <nav class=\"navbar navbar-dark navbar-fixed-top bg-inverse\">\n <button type=\"button\" class=\"navbar-toggler hidden-sm-up\" data-toggle=\"collapse\" data-target=\"#navbar\" aria-expanded=\"false\" aria-controls=\"navbar\">\n <span class=\"sr-only\">Toggle navigation</span>\n <span class=\"icon-bar\"></span>\n <span class=\"icon-bar\"></span>\n <span class=\"icon-bar\"></span>\n </button>\n <a class=\"navbar-brand\" href=\"#\">Project name</a>\n <div id=\"navbar\">\n <nav class=\"nav navbar-nav pull-xs-left\">\n <a class=\"nav-item nav-link\" href=\"#\" (click)=\"logout($event)\">Logout</a>\n </nav>\n <form class=\"pull-xs-right\">\n <input type=\"text\" class=\"form-control\" placeholder=\"Search...\">\n </form>\n </div>\n </nav>\n\n <div class=\"container-fluid\">\n <div class=\"row\">\n <div class=\"col-sm-3 col-md-2 sidebar\">\n <ul class=\"nav nav-sidebar\">\n <li class=\"active\"><a [routerLink]=\"['/LoggedIn/Dashboard']\">Dashboard<span class=\"sr-only\">(current)</span></a></li>\n <li><a [routerLink]=\"['/LoggedIn/Settings']\">Settings</a></li>\n </ul>\n <ul class=\"nav nav-sidebar\">\n <li><a href=\"\">Dummy item 1</a></li>\n <li><a href=\"\">Dummy item 2</a></li>\n </ul>\n </div>\n <div class=\"col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main\">\n <router-outlet></router-outlet>\n </div>\n </div>\n </div>\n " | ||
}), | ||
router_1.CanActivate(function (next, previous) { | ||
console.log('next:', next ? '"' + next.urlPath + '"' : null, 'previous:', previous ? '"' + previous.urlPath + '"' : null); | ||
router_1.CanActivate(function () { | ||
return co_auther_1.activationHelper('LoggedIn'); | ||
@@ -38,0 +37,0 @@ }), |
@@ -26,3 +26,3 @@ import {Component} from 'angular2/core' | ||
<nav class="nav navbar-nav pull-xs-left"> | ||
<a class="nav-item nav-link" href="#" (click)="logOut($event)">Logout</a> | ||
<a class="nav-item nav-link" href="#" (click)="logout($event)">Logout</a> | ||
</nav> | ||
@@ -54,8 +54,7 @@ <form class="pull-xs-right"> | ||
}) | ||
@CanActivate((next, previous) => { | ||
console.log('next:', next ? '"' + next.urlPath + '"' : null, 'previous:', previous ? '"' + previous.urlPath + '"' : null) | ||
@CanActivate(() => { | ||
return activationHelper('LoggedIn') | ||
}) | ||
export class LoggedInCmp { | ||
logOut ($event) { | ||
logout ($event) { | ||
$event.preventDefault() | ||
@@ -62,0 +61,0 @@ getCoAuther().logoutWrap() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
204978
5248
121