/
index.ts
executable file
·192 lines (179 loc) · 7 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import {
OidcManager,
OidcSettings,
LoginRedirectOptions,
LogoutRedirectOptions,
PopupSignInOptions,
PopupSignOutOptions,
SilentSignInOptions, LogoutResponse,
} from './authentication.model';
export * from './authentication.model';
export class Authentication {
constructor(public webAuthSettings: OidcSettings, public userManager: OidcManager) { }
/**
* To login through cidaas sdk, call **loginWithBrowser()**. This will redirect you to the hosted login page.
* once login successful, it will automatically redirects you to the redirect url whatever you mentioned in the options.
* @example
* ```js
* cidaas.loginWithBrowser();
* ```
*
* To register through cidaas sdk, call **registerWithBrowser()**. This will redirect you to the hosted registration page.
* * @example
* ```js
* cidaas.registerWithBrowser();
* ```
*
* @param {string} view_type either 'login' or 'register'
* @param {LoginRedirectOptions} options optional login options to override the webauth configuration
*/
loginOrRegisterWithBrowser(view_type: string, options?: LoginRedirectOptions) {
if (!this.webAuthSettings.extraQueryParams) {
this.webAuthSettings.extraQueryParams = {};
}
this.webAuthSettings.extraQueryParams.view_type = view_type;
if (this.webAuthSettings.response_type.indexOf("id_token") == -1 && this.webAuthSettings.scope?.indexOf("openid") != -1 && !this.webAuthSettings.extraQueryParams.nonce) {
this.webAuthSettings.extraQueryParams.nonce = new Date().getTime().toString();
}
return this.userManager.signinRedirect({
extraQueryParams: this.webAuthSettings.extraQueryParams,
redirect_uri: this.webAuthSettings.redirect_uri,
...(options && { ...options } || {})
});
};
/**
* Once login successful, it will automatically redirects you to the redirect url whatever you mentioned in the options.
* To complete the login process, call **loginCallback()**. This will parses the access_token, id_token and whatever in hash in the redirect url.
* @example
* ```js
* cidaas.loginCallback().then(function (response) {
* // the response will give you login details.
* }).catch(function(ex) {
* // your failure code here
* });
* ```
* @param {string} url optional url to read sign in state from
*/
loginCallback(url?: string) {
return this.userManager.signinRedirectCallback(url);
}
/**
* To use the **logout()** method, you need set the redirect url, if not it will automatically redirect to the login page
* @example
* ```js
* cidaas.logout();
* ```
* @param {LogoutRedirectOptions} options optional logout options to override webauth configurations
*/
logout(options?: LogoutRedirectOptions) {
return this.userManager.signoutRedirect(options);
}
/**
* **logoutCallback()** will parses the details of userState after logout.
* @example
* ```js
* cidaas.logoutCallback().then(function (response) {
* // the response will give you userState details.
* }).catch(function(ex) {
* // your failure code here
* });
* ```
* @param {string} url optional url to read signout state from,
*/
logoutCallback(url?: string): Promise<LogoutResponse> {
return this.userManager.signoutRedirectCallback(url);
};
/**
* **popupSignIn()** will open the hosted login page in pop up window.
* @example
* ```js
* cidaas.popupSignIn().then(function (response) {
* // the response will give you user details after finishing popupSignInCallback().
* }).catch(function(ex) {
* // your failure code here
* });
* ```
* @param {LogoutRedirectOptions} options optional popup sign-in options to override webauth configurations
*/
popupSignIn(options?: PopupSignInOptions) {
return this.userManager.signinPopup(options);
};
/**
* To complete the popup login process, call **popupSignInCallback()** from the popup login window.
* Popup window will be closed after doing callback
* @example
* ```js
* cidaas.popupSignInCallback();
* ```
* @param {string} url optional url to read sign-in callback state from
* @param {boolean} keepOpen true to keep the popup open even after sign in, else false
*/
popupSignInCallback(url?: string, keepOpen?: boolean) {
return this.userManager.signinPopupCallback(url, keepOpen);
};
/**
* **popupSignOut()** will open the hosted logout page in pop up window.
* @example
* ```js
* cidaas.popupSignOut().then(function() {
* // success callback in main application window after finishing popupSignOutCallback().
* }).catch(function(ex) {
* // your failure code here
* });
* ```
*
* @param {PopupSignOutOptions} options optional options to over-ride logout options using popup window
*/
popupSignOut(options?: PopupSignOutOptions) {
return this.userManager.signoutPopup(options);
};
/**
* calling **popupSignOutCallback()** from the popup window complete popup logout process.
* Popup window won't be closed after doing callback
* @example
* ```js
* cidaas.popupSignOutCallback().then(function() {
* // success callback in popup window after finishing popupSignOutCallback().
* }).catch(function(ex) {
* // your failure code here
* });
* ```
*
* @param {string} url optional url to override to check for sign out state
* @param {boolean} keepOpen true to keep the popup open even after sign out, else false
*/
popupSignOutCallback(url?: string, keepOpen: boolean = true) {
return this.userManager.signoutPopupCallback(url, keepOpen);
};
/**
* **silentSignIn()** will open the hosted login page in an iframe.
* this function could only be called from the same domain. Cross Domain is not supported for security purpose.
* @example
* ```js
* cidaas.silentSignIn().then(function (response) {
* // the response will give you user details.
* }).catch(function(ex) {
* // your failure code here
* });
* ```
* @param {SilentSignInOptions} options options to over-ride the client config for silent sign in
*/
silentSignIn(options?: SilentSignInOptions) {
return this.userManager.signinSilent({
silentRequestTimeoutInSeconds: 60,
...( options && { ...options } || {})
});
};
/**
* To complete the silent login process, call **silentSignInCallback()** from the iframe. This will complete the login process in iframe.
* @example
* ```js
* cidaas.silentSignInCallback();
*
* ```
* @param {string} url optional url to read sign in state from
*/
silentSignInCallback(url?: string) {
return this.userManager.signinSilentCallback(url);
};
}