1+ var abp = abp || { } ;
2+ ( function ( ) {
3+
4+ /* Swagger */
5+
6+ abp . swagger = abp . swagger || { } ;
7+
8+ abp . swagger . addAuthToken = function ( ) {
9+ var authToken = abp . auth . getToken ( ) ;
10+ if ( ! authToken ) {
11+ return false ;
12+ }
13+
14+ var cookieAuth = new SwaggerClient . ApiKeyAuthorization ( abp . auth . tokenHeaderName , 'Bearer ' + authToken , 'header' ) ;
15+ swaggerUi . api . clientAuthorizations . add ( 'bearerAuth' , cookieAuth ) ;
16+ return true ;
17+ }
18+
19+ abp . swagger . addCsrfToken = function ( ) {
20+ var csrfToken = abp . security . antiForgery . getToken ( ) ;
21+ if ( ! csrfToken ) {
22+ return false ;
23+ }
24+ var csrfCookieAuth = new SwaggerClient . ApiKeyAuthorization ( abp . security . antiForgery . tokenHeaderName , csrfToken , 'header' ) ;
25+ swaggerUi . api . clientAuthorizations . add ( abp . security . antiForgery . tokenHeaderName , csrfCookieAuth ) ;
26+ return true ;
27+ }
28+
29+ function loginUserInternal ( tenantId , callback ) {
30+ var usernameOrEmailAddress = document . getElementById ( 'userName' ) . value ;
31+ if ( ! usernameOrEmailAddress ) {
32+ alert ( 'Username or Email Address is required, please try with a valid value !' ) ;
33+ return false ;
34+ }
35+
36+ var password = document . getElementById ( 'password' ) . value ;
37+ if ( ! password ) {
38+ alert ( 'Password is required, please try with a valid value !' ) ;
39+ return false ;
40+ }
41+
42+ var xhr = new XMLHttpRequest ( ) ;
43+
44+ xhr . onreadystatechange = function ( ) {
45+ if ( xhr . readyState === XMLHttpRequest . DONE ) {
46+ if ( xhr . status === 200 ) {
47+ var responseJSON = JSON . parse ( xhr . responseText ) ;
48+ var result = responseJSON . result ;
49+ var expireDate = new Date ( Date . now ( ) + ( result . expireInSeconds * 1000 ) ) ;
50+ abp . auth . setToken ( result . accessToken , expireDate ) ;
51+ callback ( ) ;
52+ } else {
53+ alert ( 'Login failed !' ) ;
54+ }
55+ }
56+ } ;
57+
58+ xhr . open ( 'POST' , '/api/TokenAuth/Authenticate' , true ) ;
59+ xhr . setRequestHeader ( 'Abp.TenantId' , tenantId ) ;
60+ xhr . setRequestHeader ( 'Content-type' , 'application/json' ) ;
61+ xhr . send ( "{" + "usernameOrEmailAddress:'" + usernameOrEmailAddress + "'," + "password:'" + password + "'}" ) ;
62+ } ;
63+
64+ abp . swagger . login = function ( callback ) {
65+ //Get TenantId first
66+ var tenancyName = document . getElementById ( 'tenancyName' ) . value ;
67+
68+ if ( tenancyName ) {
69+ var xhrTenancyName = new XMLHttpRequest ( ) ;
70+ xhrTenancyName . onreadystatechange = function ( ) {
71+ if ( xhrTenancyName . readyState === XMLHttpRequest . DONE && xhrTenancyName . status === 200 ) {
72+ var responseJSON = JSON . parse ( xhrTenancyName . responseText ) ;
73+ var result = responseJSON . result ;
74+ if ( result . state === 1 ) { // Tenant exists and active.
75+ loginUserInternal ( result . tenantId , callback ) ; // Login for tenant
76+ } else {
77+ alert ( 'There is no such tenant or tenant is not active !' ) ;
78+ }
79+ }
80+ } ;
81+
82+ xhrTenancyName . open ( 'POST' , '/api/services/app/Account/IsTenantAvailable' , true ) ;
83+ xhrTenancyName . setRequestHeader ( 'Content-type' , 'application/json' ) ;
84+ xhrTenancyName . send ( "{" + "tenancyName:'" + tenancyName + "'}" ) ;
85+ } else {
86+ loginUserInternal ( null , callback ) ; // Login for host
87+ }
88+ } ;
89+
90+ abp . swagger . logout = function ( ) {
91+ abp . auth . clearToken ( ) ;
92+ }
93+
94+ abp . swagger . closeAuthDialog = function ( ) {
95+ if ( document . getElementById ( 'abp-auth-dialog' ) ) {
96+ document . getElementsByClassName ( "swagger-ui" ) [ 1 ] . removeChild ( document . getElementById ( 'abp-auth-dialog' ) ) ;
97+ }
98+ }
99+
100+ abp . swagger . openAuthDialog = function ( loginCallback ) {
101+ abp . swagger . closeAuthDialog ( ) ;
102+
103+ var abpAuthDialog = document . createElement ( 'div' ) ;
104+ abpAuthDialog . className = 'dialog-ux' ;
105+ abpAuthDialog . id = 'abp-auth-dialog' ;
106+
107+ document . getElementsByClassName ( "swagger-ui" ) [ 1 ] . appendChild ( abpAuthDialog ) ;
108+
109+ // -- backdrop-ux
110+ var backdropUx = document . createElement ( 'div' ) ;
111+ backdropUx . className = 'backdrop-ux' ;
112+ abpAuthDialog . appendChild ( backdropUx ) ;
113+
114+ // -- modal-ux
115+ var modalUx = document . createElement ( 'div' ) ;
116+ modalUx . className = 'modal-ux' ;
117+ abpAuthDialog . appendChild ( modalUx ) ;
118+
119+ // -- -- modal-dialog-ux
120+ var modalDialogUx = document . createElement ( 'div' ) ;
121+ modalDialogUx . className = 'modal-dialog-ux' ;
122+ modalUx . appendChild ( modalDialogUx ) ;
123+
124+ // -- -- -- modal-ux-inner
125+ var modalUxInner = document . createElement ( 'div' ) ;
126+ modalUxInner . className = 'modal-ux-inner' ;
127+ modalDialogUx . appendChild ( modalUxInner ) ;
128+
129+ // -- -- -- -- modal-ux-header
130+ var modalUxHeader = document . createElement ( 'div' ) ;
131+ modalUxHeader . className = 'modal-ux-header' ;
132+ modalUxInner . appendChild ( modalUxHeader ) ;
133+
134+ var modalHeader = document . createElement ( 'h3' ) ;
135+ modalHeader . innerText = 'Authorize' ;
136+ modalUxHeader . appendChild ( modalHeader ) ;
137+
138+ // -- -- -- -- modal-ux-content
139+ var modalUxContent = document . createElement ( 'div' ) ;
140+ modalUxContent . className = 'modal-ux-content' ;
141+ modalUxInner . appendChild ( modalUxContent ) ;
142+
143+ modalUxContent . onkeydown = function ( e ) {
144+ if ( e . keyCode === 13 ) {
145+ //try to login when user presses enter on authorize modal
146+ abp . swagger . login ( loginCallback ) ;
147+ }
148+ } ;
149+
150+ //Inputs
151+ createInput ( modalUxContent , 'tenancyName' , 'Tenancy Name (Leave empty for Host)' ) ;
152+ createInput ( modalUxContent , 'userName' , 'Username or email address' ) ;
153+ createInput ( modalUxContent , 'password' , 'Password' , 'password' ) ;
154+
155+ //Buttons
156+ var authBtnWrapper = document . createElement ( 'div' ) ;
157+ authBtnWrapper . className = 'auth-btn-wrapper' ;
158+ modalUxContent . appendChild ( authBtnWrapper ) ;
159+
160+ //Close button
161+ var closeButton = document . createElement ( 'button' ) ;
162+ closeButton . className = 'btn modal-btn auth btn-done button' ;
163+ closeButton . innerText = 'Close' ;
164+ closeButton . style . marginRight = '5px' ;
165+ closeButton . onclick = abp . swagger . closeAuthDialog ;
166+ authBtnWrapper . appendChild ( closeButton ) ;
167+
168+ //Authorize button
169+ var authorizeButton = document . createElement ( 'button' ) ;
170+ authorizeButton . className = 'btn modal-btn auth authorize button' ;
171+ authorizeButton . innerText = 'Login' ;
172+ authorizeButton . onclick = function ( ) {
173+ abp . swagger . login ( loginCallback ) ;
174+ } ;
175+ authBtnWrapper . appendChild ( authorizeButton ) ;
176+ }
177+
178+ function createInput ( container , id , title , type ) {
179+ var wrapper = document . createElement ( 'div' ) ;
180+ wrapper . className = 'wrapper' ;
181+ container . appendChild ( wrapper ) ;
182+
183+ var label = document . createElement ( 'label' ) ;
184+ label . innerText = title ;
185+ wrapper . appendChild ( label ) ;
186+
187+ var section = document . createElement ( 'section' ) ;
188+ section . className = 'block-tablet col-10-tablet block-desktop col-10-desktop' ;
189+ wrapper . appendChild ( section ) ;
190+
191+ var input = document . createElement ( 'input' ) ;
192+ input . id = id ;
193+ input . type = type ? type : 'text' ;
194+ input . style . width = '100%' ;
195+
196+ section . appendChild ( input ) ;
197+ }
198+
199+ } ) ( ) ;
0 commit comments