Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

co-auther

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

co-auther - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

src/co-auther/co-auther-ng2.js

6

co-auther.js
"use strict";
var CoAuther = require('./src/co-auther/co-auther');
exports.CoAuther = CoAuther;
var co_auther_1 = require('./src/co-auther/co-auther');
exports.CoAuther = co_auther_1.CoAuther;
var co_auther_ng2_1 = require('./src/co-auther/co-auther-ng2');
exports.CoAutherNg2 = co_auther_ng2_1.CoAutherNg2;
//# sourceMappingURL=co-auther.js.map

@@ -1,2 +0,3 @@

import * as CoAuther from './src/co-auther/co-auther'
export {CoAuther}
import {CoAuther} from './src/co-auther/co-auther'
import {CoAutherNg2} from './src/co-auther/co-auther-ng2'
export {CoAuther, CoAutherNg2}
{
"name": "co-auther",
"version": "1.0.0",
"version": "1.1.0",
"description": "",

@@ -5,0 +5,0 @@ "main": "src/co-auther/co-auther.js",

@@ -33,18 +33,17 @@ # co-auther

import apiService from './apiService'
import {CoAuther} from 'co-auther'
import {CoAutherNg2} from 'co-auther'
...
// The 3 basic routes
[
{path: '/authenticate', component: AuthenticateComponent},
{path: '/logged-in', component: LoggedInComponent},
{path: '/initialRequest', component: InitialRequestComponent}
{path: 'authenticate', component: AuthenticateComponent},
{path: 'logged-in', component: LoggedInComponent},
{path: 'initialRequest', component: InitialRequestComponent}
]
...
constructor () {
CoAuther.initialize(apiService, {
routes: {
loggedIn: 'logged-in',
authenticate: 'authenticate',
initialRequest: 'initial-request'
},
CoAutherNg2.coAuther.init({
apiService,
loggedInRoute: 'logged-in',
authenticateRoute: 'authenticate',
initialRequestRoute: 'initial-request',
authDataKey: 'authData'

@@ -55,6 +54,3 @@ })

logout () {
CoAuther.getCoAuther().logoutWrap()
.then((res) => {
window.location.reload()
})
CoAutherNg2.coAuther.logoutWrap()
}

@@ -66,10 +62,7 @@ ```

```javascript
import {CoAuther} from 'co-auther'
import {CoAutherNg2} from 'co-auther'
...
export class AuthenticateComponent {
login (username, login) {
CoAuther.getCoAuther().loginWrap(username, login)
.then(() => {
this.router.navigate(['logged-in'])
})
CoAuther.coAuther.loginWrap(username, login)
}

@@ -85,10 +78,12 @@ }

let apiService = {
login () {
makeInitialRequest () {
return new Promise((resolve, reject) => {
return myRequest
.then((data) => {
this.router.navigate(['logged-in'])
resolve(data)
})
.catch((err) => {
myCustomErrorHandler(err)
localStorage.remove('authData')
this.router.navigate(['authenticate'])
reject(err)

@@ -95,0 +90,0 @@ })

"use strict";
// config params
var config = {
LOGGED_IN: 'logged-in',
AUTHENTICATE: 'authenticate',
INITIAL_REQUEST: 'initial-request',
AUTH_DATA_KEY: 'authData'
};
var initialRequestFailed = false;
var coAuther;
var getCoAuther = function () {
if (!coAuther) {
throw 'CoAuther has not been initialized yet';
var CoAuther = (function () {
function CoAuther(loggedInRoute, authenticateRoute, initialRequestRoute, authDataKey, debugMode) {
if (loggedInRoute === void 0) { loggedInRoute = 'logged-in'; }
if (authenticateRoute === void 0) { authenticateRoute = 'authenticate'; }
if (initialRequestRoute === void 0) { initialRequestRoute = 'initial-request'; }
if (authDataKey === void 0) { authDataKey = 'authData'; }
if (debugMode === void 0) { debugMode = false; }
this.loggedInRoute = loggedInRoute;
this.authenticateRoute = authenticateRoute;
this.initialRequestRoute = initialRequestRoute;
this.authDataKey = authDataKey;
this.debugMode = debugMode;
this.initialRequestFailed = false;
this.initialRequestPending = false;
this.initialDataLoaded = false;
}
else {
return coAuther;
}
};
exports.getCoAuther = getCoAuther;
// determine where to route to based on requested route and authentication state
var initialRequestPending = false;
function activationHelper(destinationRequested) {
var destinationResult = null;
var authData = localStorage.getItem(config.AUTH_DATA_KEY);
var initialDataLoaded = getCoAuther().isInitialDataLoaded();
// authData and initialRequest done, suggest LOGGED_IN
if (authData && initialDataLoaded) {
destinationResult = config.LOGGED_IN;
}
else if (!authData && !initialRequestPending) {
destinationResult = config.AUTHENTICATE;
}
else {
destinationResult = config.INITIAL_REQUEST;
if (!initialRequestPending && !initialRequestFailed) {
initialRequestPending = true;
getCoAuther().makeInitialRequestWrap()
.then(function () {
// initial request successful, suggest LOGGED_IN
initialRequestPending = false;
destinationResult = config.LOGGED_IN;
})
.catch(function (err) {
// initial request failed, suggest AUTHENTICATE
initialRequestPending = false;
initialRequestFailed = true;
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.');
}
}
return destinationResult;
}
exports.activationHelper = activationHelper;
function CoAuther(apiService) {
var initialDataLoaded = false;
function isInitialDataLoaded() {
return initialDataLoaded;
}
function loginWrap() {
// set options after constructor (for angular 2 reasons)
CoAuther.prototype.init = function (options) {
this.apiService = options.apiService;
this.loggedInRoute = options.loggedInRoute || this.loggedInRoute;
this.authenticateRoute = options.authenticateRoute || this.authenticateRoute;
this.initialRequestRoute = options.initialRequestRoute || this.initialRequestRoute;
this.authDataKey = options.authDataKey || this.authDataKey;
this.debugMode = options.debugMode || this.debugMode;
};
CoAuther.prototype.loginWrap = function () {
var args = [];

@@ -69,6 +33,6 @@ for (var _i = 0; _i < arguments.length; _i++) {

// if initial request failed before, consider this a retry
initialRequestFailed = false;
return apiService.login.apply(apiService, args);
}
function logoutWrap() {
this.initialRequestFailed = false;
return this.apiService.login.apply(this.apiService, args);
};
CoAuther.prototype.logoutWrap = function () {
var args = [];

@@ -78,36 +42,54 @@ for (var _i = 0; _i < arguments.length; _i++) {

}
return apiService.logout.apply(apiService, args);
}
function makeInitialRequestWrap() {
return apiService.makeInitialRequest()
return this.apiService.logout.apply(this.apiService, args);
};
CoAuther.prototype.makeInitialRequestWrap = function () {
var _this = this;
return this.apiService.makeInitialRequest()
.then(function () {
// flag for initial data
initialDataLoaded = true;
_this.initialDataLoaded = true;
});
}
return {
loginWrap: loginWrap,
logoutWrap: logoutWrap,
makeInitialRequestWrap: makeInitialRequestWrap,
isInitialDataLoaded: isInitialDataLoaded
};
}
function initialize(apiService, newConfig) {
coAuther = CoAuther(apiService);
if (newConfig.authDataKey) {
config.AUTH_DATA_KEY = newConfig.authDataKey;
}
if (newConfig.routes) {
if (newConfig.routes.loggedIn) {
config.LOGGED_IN = newConfig.routes.loggedIn;
CoAuther.prototype.activationHelper = function (destinationRequested) {
var _this = this;
var destinationResult = null;
var authData = localStorage.getItem(this.authDataKey);
// authData and initialRequest done, suggest LOGGED_IN
if (authData && this.initialDataLoaded) {
destinationResult = this.loggedInRoute;
}
if (newConfig.routes.authenticate) {
config.AUTHENTICATE = newConfig.routes.authenticate;
else if (!authData && !this.initialRequestPending) {
destinationResult = this.authenticateRoute;
}
if (newConfig.routes.initialRequest) {
config.INITIAL_REQUEST = newConfig.routes.initialRequest;
else {
destinationResult = this.initialRequestRoute;
if (!this.initialRequestPending && !this.initialRequestFailed) {
this.initialRequestPending = true;
this.makeInitialRequestWrap()
.then(function () {
// initial request successful, suggest LOGGED_IN
_this.initialRequestPending = false;
destinationResult = _this.loggedInRoute;
})
.catch(function (err) {
// initial request failed, suggest AUTHENTICATE
_this.initialRequestPending = false;
_this.initialRequestFailed = true;
destinationResult = _this.authenticateRoute;
});
}
else if (this.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.');
}
}
}
}
exports.initialize = initialize;
if (this.debugMode) {
console.log('[co-auther] destinationRequested: ' + destinationRequested);
console.log('[co-auther] destinationResult: ' + destinationResult);
}
return destinationResult;
};
return CoAuther;
}());
exports.CoAuther = CoAuther;
//# sourceMappingURL=co-auther.js.map

@@ -1,120 +0,89 @@

// config params
let config = {
LOGGED_IN: 'logged-in',
AUTHENTICATE: 'authenticate',
INITIAL_REQUEST: 'initial-request',
AUTH_DATA_KEY: 'authData'
}
export class CoAuther {
private initialRequestFailed = false;
private initialRequestPending = false;
private initialDataLoaded = false;
let initialRequestFailed = false
let coAuther
// config
private apiService;
constructor (
private loggedInRoute = 'logged-in',
private authenticateRoute = 'authenticate',
private initialRequestRoute = 'initial-request',
private authDataKey = 'authData',
private debugMode = false
) {}
let getCoAuther = function () {
if (!coAuther) {
throw 'CoAuther has not been initialized yet'
} else {
return coAuther
}
}
// set options after constructor (for angular 2 reasons)
public init (options) {
this.apiService = options.apiService
// determine where to route to based on requested route and authentication state
var initialRequestPending = false
function activationHelper (destinationRequested) {
let destinationResult = null
let authData = localStorage.getItem(config.AUTH_DATA_KEY)
let initialDataLoaded = getCoAuther().isInitialDataLoaded()
// authData and initialRequest done, suggest LOGGED_IN
if (authData && initialDataLoaded) {
destinationResult = config.LOGGED_IN
// no authData and no initialRequest pending, suggest AUTHENTICATE
} else if (!authData && !initialRequestPending) {
destinationResult = config.AUTHENTICATE
// authData is available, suggest INITIAL_REQUEST
} else {
destinationResult = config.INITIAL_REQUEST
if (!initialRequestPending && !initialRequestFailed) {
initialRequestPending = true
getCoAuther().makeInitialRequestWrap()
.then(() => {
// initial request successful, suggest LOGGED_IN
initialRequestPending = false
destinationResult = config.LOGGED_IN
})
.catch((err) => {
// initial request failed, suggest AUTHENTICATE
initialRequestPending = false
initialRequestFailed = true
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.')
}
this.loggedInRoute = options.loggedInRoute || this.loggedInRoute
this.authenticateRoute = options.authenticateRoute || this.authenticateRoute
this.initialRequestRoute = options.initialRequestRoute || this.initialRequestRoute
this.authDataKey = options.authDataKey || this.authDataKey
this.debugMode = options.debugMode || this.debugMode
}
return destinationResult
}
function CoAuther (apiService) {
let initialDataLoaded = false
function isInitialDataLoaded () {
return initialDataLoaded
}
function loginWrap (...args) {
public loginWrap (...args) {
// if initial request failed before, consider this a retry
initialRequestFailed = false
return apiService.login.apply(apiService, args)
this.initialRequestFailed = false
return this.apiService.login.apply(this.apiService, args)
}
function logoutWrap (...args) {
return apiService.logout.apply(apiService, args)
public logoutWrap (...args) {
return this.apiService.logout.apply(this.apiService, args)
}
function makeInitialRequestWrap () {
return apiService.makeInitialRequest()
public makeInitialRequestWrap () {
return this.apiService.makeInitialRequest()
.then(() => {
// flag for initial data
initialDataLoaded = true
this.initialDataLoaded = true
})
}
return {
loginWrap,
logoutWrap,
makeInitialRequestWrap,
isInitialDataLoaded
}
}
public activationHelper (destinationRequested) {
let destinationResult = null
let authData = localStorage.getItem(this.authDataKey)
function initialize (apiService, newConfig) {
coAuther = CoAuther(apiService)
// authData and initialRequest done, suggest LOGGED_IN
if (authData && this.initialDataLoaded) {
destinationResult = this.loggedInRoute
if (newConfig.authDataKey) {
config.AUTH_DATA_KEY = newConfig.authDataKey
}
if (newConfig.routes) {
if (newConfig.routes.loggedIn) {
config.LOGGED_IN = newConfig.routes.loggedIn
// no authData and no initialRequest pending, suggest AUTHENTICATE
} else if (!authData && !this.initialRequestPending) {
destinationResult = this.authenticateRoute
// authData is available, suggest INITIAL_REQUEST
} else {
destinationResult = this.initialRequestRoute
if (!this.initialRequestPending && !this.initialRequestFailed) {
this.initialRequestPending = true
this.makeInitialRequestWrap()
.then(() => {
// initial request successful, suggest LOGGED_IN
this.initialRequestPending = false
destinationResult = this.loggedInRoute
})
.catch((err) => {
// initial request failed, suggest AUTHENTICATE
this.initialRequestPending = false
this.initialRequestFailed = true
destinationResult = this.authenticateRoute
})
} else if (this.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 (newConfig.routes.authenticate) {
config.AUTHENTICATE = newConfig.routes.authenticate
if (this.debugMode) {
console.log('[co-auther] destinationRequested: ' + destinationRequested)
console.log('[co-auther] destinationResult: ' + destinationResult)
}
if (newConfig.routes.initialRequest) {
config.INITIAL_REQUEST = newConfig.routes.initialRequest
}
return destinationResult
}
}
export {
initialize,
getCoAuther,
activationHelper
}
}

@@ -15,22 +15,16 @@ "use strict";

// API and authentication services
var api_service_1 = require('../api-service');
var CoAuther = require('../../co-auther/co-auther');
var api_service_1 = require('../services/api-service');
var co_auther_1 = require('../../../co-auther');
var AppComponent = (function () {
function AppComponent(router, apiService) {
function AppComponent(router, apiService, coAutherNg2) {
this.router = router;
this.apiService = apiService;
CoAuther.initialize(apiService, {
routes: {
loggedIn: 'logged-in',
authenticate: 'authenticate',
initialRequest: 'initial-request'
},
authDataKey: 'authData'
this.coAutherNg2 = coAutherNg2;
coAutherNg2.init({
apiService: apiService,
debugMode: true
});
}
AppComponent.prototype.logout = function () {
CoAuther.getCoAuther().logoutWrap().then(function (res) {
localStorage.removeItem('authData');
window.location.reload();
});
this.coAutherNg2.coAuther.logoutWrap();
};

@@ -43,3 +37,3 @@ AppComponent = __decorate([

}),
__metadata('design:paramtypes', [router_1.Router, api_service_1.ApiService])
__metadata('design:paramtypes', [router_1.Router, api_service_1.ApiService, co_auther_1.CoAutherNg2])
], AppComponent);

@@ -46,0 +40,0 @@ return AppComponent;

@@ -11,4 +11,4 @@ // Angular

// API and authentication services
import {ApiService} from '../api-service'
import * as CoAuther from '../../co-auther/co-auther'
import {ApiService} from '../services/api-service'
import {CoAutherNg2} from '../../../co-auther'

@@ -34,11 +34,8 @@ @Component({

private router: Router,
private apiService: ApiService
private apiService: ApiService,
private coAutherNg2: CoAutherNg2
) {
CoAuther.initialize(apiService, {
routes: {
loggedIn: 'logged-in',
authenticate: 'authenticate',
initialRequest: 'initial-request'
},
authDataKey: 'authData'
coAutherNg2.init({
apiService,
debugMode: true
})

@@ -48,7 +45,4 @@ }

logout () {
CoAuther.getCoAuther().logoutWrap().then(res => {
localStorage.removeItem('authData')
window.location.reload()
})
this.coAutherNg2.coAuther.logoutWrap()
}
}

@@ -13,15 +13,10 @@ "use strict";

var router_1 = require('@angular/router');
var CoAuther = require('../../../co-auther/co-auther');
var co_auther_1 = require('../../../../co-auther');
var AuthenticateComponent = (function () {
function AuthenticateComponent(router) {
function AuthenticateComponent(router, coAutherNg2) {
this.router = router;
this.coAutherNg2 = coAutherNg2;
}
AuthenticateComponent.prototype.login = function (username, password) {
var _this = this;
CoAuther.getCoAuther().loginWrap(username, password)
.then(function (res) {
console.log('Authentication went fine, go to initial-request route');
localStorage.setItem('authData', res);
_this.router.navigate(['/initial-request']);
});
this.coAutherNg2.coAuther.loginWrap(username, password);
};

@@ -33,3 +28,3 @@ AuthenticateComponent = __decorate([

}),
__metadata('design:paramtypes', [router_1.Router])
__metadata('design:paramtypes', [router_1.Router, co_auther_1.CoAutherNg2])
], AuthenticateComponent);

@@ -36,0 +31,0 @@ return AuthenticateComponent;

import {Component} from '@angular/core'
import {Router} from '@angular/router'
import * as CoAuther from '../../../co-auther/co-auther'
import {CoAutherNg2} from '../../../../co-auther'

@@ -14,12 +14,10 @@ @Component({

export class AuthenticateComponent {
constructor (private router: Router) {}
constructor (
private router: Router,
private coAutherNg2: CoAutherNg2
) {}
login (username, password) {
CoAuther.getCoAuther().loginWrap(username, password)
.then(res => {
console.log('Authentication went fine, go to initial-request route')
localStorage.setItem('authData', res)
this.router.navigate(['/initial-request'])
})
this.coAutherNg2.coAuther.loginWrap(username, password)
}
}

@@ -12,6 +12,7 @@ "use strict";

var core_1 = require('@angular/core');
var co_auther_1 = require('../../co-auther/co-auther');
var co_auther_1 = require('../../../co-auther');
var Observable_1 = require('rxjs/Observable');
var CoAutherGuard = (function () {
function CoAutherGuard() {
function CoAutherGuard(coAutherNg2) {
this.coAutherNg2 = coAutherNg2;
}

@@ -21,3 +22,3 @@ CoAutherGuard.prototype.canActivate = function (route) {

var routeRequest = route.url[0].path;
var routeResponse = co_auther_1.activationHelper(routeRequest);
var routeResponse = this.coAutherNg2.coAuther.activationHelper(routeRequest);
return Observable_1.Observable.from([routeRequest === routeResponse]);

@@ -27,3 +28,3 @@ };

core_1.Injectable(),
__metadata('design:paramtypes', [])
__metadata('design:paramtypes', [co_auther_1.CoAutherNg2])
], CoAutherGuard);

@@ -30,0 +31,0 @@ return CoAutherGuard;

import {Injectable} from '@angular/core'
import {CanActivate} from '@angular/router'
import {activationHelper} from '../../co-auther/co-auther'
import {CoAutherNg2} from '../../../co-auther'
import {Observable} from 'rxjs/Observable'

@@ -8,8 +8,9 @@

export class CoAutherGuard implements CanActivate {
constructor (private coAutherNg2: CoAutherNg2) {}
canActivate (route) {
// figure out if the requested route can be routed to
let routeRequest = route.url[0].path
let routeResponse = activationHelper(routeRequest)
let routeResponse = this.coAutherNg2.coAuther.activationHelper(routeRequest)
return Observable.from([routeRequest === routeResponse])
}
}

@@ -18,3 +18,3 @@ "use strict";

DummyComponent.prototype.ngOnInit = function () {
this.router.navigate(['authenticate']);
this.router.navigateByUrl('authenticate');
};

@@ -21,0 +21,0 @@ DummyComponent = __decorate([

@@ -13,4 +13,4 @@ import {Component} from '@angular/core'

ngOnInit () {
this.router.navigate(['authenticate'])
this.router.navigateByUrl('authenticate')
}
}

@@ -6,9 +6,11 @@ "use strict";

var common_1 = require('@angular/common');
var api_service_1 = require('./api-service');
var api_service_1 = require('./services/api-service');
var co_auther_1 = require('../../co-auther');
platform_browser_dynamic_1.bootstrap(app_component_1.AppComponent, [
app_routes_1.APP_ROUTER_PROVIDERS,
{ provide: common_1.LocationStrategy, useClass: common_1.HashLocationStrategy },
api_service_1.ApiService
api_service_1.ApiService,
co_auther_1.CoAutherNg2
])
.catch(function (err) { return console.log(err); });
//# sourceMappingURL=main.js.map

@@ -5,3 +5,4 @@ import {bootstrap} from '@angular/platform-browser-dynamic'

import {LocationStrategy, HashLocationStrategy} from '@angular/common'
import {ApiService} from './api-service'
import {ApiService} from './services/api-service'
import {CoAutherNg2} from '../../co-auther'

@@ -11,4 +12,5 @@ bootstrap(AppComponent, [

{provide: LocationStrategy, useClass: HashLocationStrategy},
ApiService
ApiService,
CoAutherNg2
])
.catch(err => console.log(err))

@@ -5,3 +5,2 @@ System.config({

map: {
'src': 'src',
'@angular': '/node_modules/@angular',

@@ -11,2 +10,3 @@ 'rxjs': 'node_modules/rxjs'

packages: {
'': { defaultExtension: 'js' },
'src': { defaultExtension: 'js' },

@@ -13,0 +13,0 @@ 'rxjs': { defaultExtension: 'js' },

@@ -6,3 +6,2 @@ declare var System

map: {
'src': 'src',
'@angular': '/node_modules/@angular',

@@ -12,2 +11,3 @@ 'rxjs': 'node_modules/rxjs'

packages: {
'': {defaultExtension: 'js'},
'src': {defaultExtension: 'js'},

@@ -14,0 +14,0 @@ 'rxjs': {defaultExtension: 'js'},

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc