1
- import { loginHref , logoutHref , replacedAttrKey , replacedAttrValue } from "@/constants" ;
1
+ import { loginHref , logoutHref , updatedAttrKey , updatedAttrValue } from "@/constants" ;
2
2
import { parseQueryParams } from "@/query-params" ;
3
3
4
4
import {
5
5
clearCredentials ,
6
6
type Credentials ,
7
7
isLoggedIn ,
8
+ isOpenIDLoggedIn ,
8
9
isUITokenExpired ,
9
10
saveCredentials ,
10
11
validateCredentials ,
@@ -67,15 +68,22 @@ function removeInvalidCommands(commands: HTMLElement[]): void {
67
68
}
68
69
69
70
function updateUsageTabs ( usageTabsSelector : string ) : void {
71
+ const openIDLoggedIn = isOpenIDLoggedIn ( ) ;
72
+
73
+ const loggedIn = isLoggedIn ( ) ;
74
+
75
+ if ( ! openIDLoggedIn && loggedIn ) {
76
+ // If we are logged in but not with OpenID, we don't need to update the usage info
77
+ return ;
78
+ }
79
+
70
80
const tabs = [ ...document . querySelectorAll ( usageTabsSelector ) ] . filter (
71
- ( node ) => node . getAttribute ( replacedAttrKey ) !== replacedAttrValue ,
81
+ ( node ) => node . getAttribute ( updatedAttrKey ) !== updatedAttrValue ,
72
82
) ;
73
83
74
84
if ( tabs . length === 0 ) return ;
75
85
76
- const loggedIn = isLoggedIn ( ) ;
77
-
78
- const usageInfoLines = getUsageInfo ( loggedIn ) . split ( "\n" ) . reverse ( ) ;
86
+ const usageInfoLines = getUsageInfo ( openIDLoggedIn ) . split ( "\n" ) . reverse ( ) ;
79
87
80
88
for ( const tab of tabs ) {
81
89
const commands = [ ...tab . querySelectorAll ( "button" ) ]
@@ -85,26 +93,51 @@ function updateUsageTabs(usageTabsSelector: string): void {
85
93
if ( commands . length === 0 ) continue ;
86
94
87
95
for ( const info of usageInfoLines ) {
88
- cloneAndAppendCommand ( commands [ 0 ] , info , loggedIn ) ;
96
+ cloneAndAppendCommand ( commands [ 0 ] , info , openIDLoggedIn ) ;
89
97
}
90
98
91
99
removeInvalidCommands ( commands ) ;
92
100
93
- tab . setAttribute ( replacedAttrKey , replacedAttrValue ) ;
101
+ tab . setAttribute ( updatedAttrKey , updatedAttrValue ) ;
94
102
}
95
103
}
96
104
105
+ function addOpenIDLoginButton ( loginDialogSelector : string , loginButtonSelector : string , callback : ( ) => void ) : void {
106
+ const loginDialog = document . querySelector ( loginDialogSelector ) ;
107
+
108
+ if ( ! loginDialog || loginDialog . getAttribute ( updatedAttrKey ) === updatedAttrValue ) return ;
109
+
110
+ const loginButton = document . querySelector ( loginButtonSelector ) ! ;
111
+
112
+ const loginWithOpenIDButton = loginButton . cloneNode ( false ) as HTMLButtonElement ;
113
+
114
+ loginWithOpenIDButton . textContent = "Login with OpenID Connect" ;
115
+ loginWithOpenIDButton . dataset . testid = "dialogOpenIDLogin" ;
116
+
117
+ loginWithOpenIDButton . addEventListener ( "click" , callback ) ;
118
+
119
+ loginDialog . append ( loginWithOpenIDButton ) ;
120
+
121
+ loginDialog . setAttribute ( updatedAttrKey , updatedAttrValue ) ;
122
+ }
123
+
97
124
export interface InitOptions {
98
- loginButton : string ;
99
- logoutButton : string ;
100
- usageTabs : string ;
125
+ loginButtonSelector : string ;
126
+ loginDialogSelector : string ;
127
+ logoutButtonSelector : string ;
128
+ usageTabsSelector : string ;
101
129
}
102
130
103
131
//
104
132
// By default the login button opens a form that asks the user to submit credentials.
105
133
// We replace this behaviour and instead redirect to the route that handles OAuth.
106
134
//
107
- export function init ( { loginButton, logoutButton, usageTabs } : InitOptions ) : void {
135
+ export function init ( {
136
+ loginButtonSelector,
137
+ logoutButtonSelector,
138
+ usageTabsSelector,
139
+ loginDialogSelector,
140
+ } : InitOptions ) : void {
108
141
if ( parseAndSaveCredentials ( ) ) {
109
142
// If we are new logged in, reload the page to remove the query params
110
143
reloadToPathname ( ) ;
@@ -117,19 +150,25 @@ export function init({ loginButton, logoutButton, usageTabs }: InitOptions): voi
117
150
118
151
const baseUrl = getBaseUrl ( true ) ;
119
152
120
- interruptClick ( loginButton , ( ) => {
153
+ const gotoOpenIDLoginUrl = ( ) => {
121
154
location . href = baseUrl + loginHref ;
122
- } ) ;
155
+ } ;
156
+
157
+ if ( window . __VERDACCIO_OPENID_OPTIONS ?. keepPasswdLogin ) {
158
+ const updateLoginDialog = ( ) => addOpenIDLoginButton ( loginDialogSelector , loginButtonSelector , gotoOpenIDLoginUrl ) ;
123
159
124
- interruptClick ( logoutButton , ( ) => {
160
+ document . addEventListener ( "click" , ( ) => retry ( updateLoginDialog , 2 ) ) ;
161
+ } else {
162
+ interruptClick ( loginButtonSelector , gotoOpenIDLoginUrl ) ;
163
+ }
164
+
165
+ interruptClick ( logoutButtonSelector , ( ) => {
125
166
clearCredentials ( ) ;
126
167
127
168
location . href = baseUrl + logoutHref ;
128
169
} ) ;
129
170
130
- const updateUsageInfo = ( ) => updateUsageTabs ( usageTabs ) ;
171
+ const updateUsageInfo = ( ) => updateUsageTabs ( usageTabsSelector ) ;
131
172
132
173
document . addEventListener ( "click" , ( ) => retry ( updateUsageInfo , 2 ) ) ;
133
-
134
- retry ( updateUsageInfo ) ;
135
174
}
0 commit comments