@salte-auth/redirect
Version:
A Salte Auth handler for authenticating via Redirect!
57 lines (48 loc) • 1.19 kB
JavaScript
import { Handler, Utils, SalteAuthError } from '@salte-auth/salte-auth';
class Redirect extends Handler {
constructor(config) {
super(config);
this.config = Utils.Common.defaults(this.config, {
timeout: 10000
});
}
get name() {
return 'redirect';
}
get auto() {
return true;
}
connected(_ref) {
var {
action
} = _ref;
if (!action) return;
var origin = this.storage.get('origin');
if (!origin) return;
this.storage.delete('origin');
if (action === 'login') {
// Does it make sense to navigate on 'logout'?
// NOTE: This order, matters since navigate modifies the location.
var parsed = Utils.URL.parse(location);
this.navigate(origin);
return parsed;
}
}
open(_ref2) {
var {
url,
timeout = this.config.timeout
} = _ref2;
this.storage.set('origin', location.href);
this.navigate(url);
return new Promise((_resolve, reject) => {
setTimeout(() => {
reject(new SalteAuthError({
code: 'redirect_timeout',
message: "Timed out while redirecting."
}));
}, timeout);
});
}
}
export { Redirect };