-
Notifications
You must be signed in to change notification settings - Fork 15
/
redirect.ts
74 lines (59 loc) 路 1.64 KB
/
redirect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Handler, SalteAuthError, Utils } from '../src/salte-auth';
export class Redirect extends Handler {
public constructor(config?: Redirect.Config) {
super(config);
this.config = Utils.Common.defaults(this.config, {
timeout: 10000
});
}
public get name() {
return 'redirect';
}
public get auto() {
return true;
}
public connected({ action }: Handler.ConnectedOptions) {
if (!action) return;
const 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.
const parsed = Utils.URL.parse(location);
this.navigate(origin);
return parsed;
}
}
public open({ url, timeout = this.config.timeout }: Redirect.OpenOptions): Promise<void> {
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 interface Redirect {
config: Redirect.Config;
}
export declare namespace Redirect {
export interface Config extends Handler.Config {
/**
* The amount of time in ms before any login / logout requests will timeout.
*
* @defaultValue 10000
*/
timeout?: number;
}
export interface OpenOptions extends Handler.OpenOptions {
/**
* Override the configured timeout.
*/
timeout?: number;
}
}