11'use strict' ;
22
33// import * as settings from 'settings';
4- import { confirm } from 'bootbox' ;
4+ import { alert , confirm } from 'bootbox' ;
55import { get , post , del } from 'api' ;
66import { error } from 'alerts' ;
77import { render } from 'benchpress' ;
@@ -25,6 +25,7 @@ export function init() {
2525 title,
2626 message,
2727 callback : handleEditStrategy ,
28+ onShown : handleAutoDiscovery ,
2829 } ) ;
2930
3031 break ;
@@ -39,6 +40,7 @@ export function init() {
3940 title,
4041 message,
4142 callback : handleEditStrategy ,
43+ onShown : handleAutoDiscovery ,
4244 } ) ;
4345
4446 break ;
@@ -58,6 +60,19 @@ export function init() {
5860 handleDeleteStrategy . call ( this , ok , name ) ;
5961 } ,
6062 } ) ;
63+
64+ break ;
65+ }
66+
67+ case 'callback-help' : {
68+ alert ( {
69+ title : 'What is the callback URL?' ,
70+ message : `
71+ When you create a new OAuth2 client at the provider, you need to specify a callback URL.
72+ Each provider is unfamiliar with how individual clients handle the callback.
73+ NodeBB provides the following URL for you to enter into that configuration.
74+ ` ,
75+ } ) ;
6176 }
6277 }
6378 }
@@ -84,6 +99,48 @@ function handleEditStrategy(ok) {
8499 return false ;
85100}
86101
102+ function handleAutoDiscovery ( ) {
103+ const modalEl = this ;
104+
105+ const domainEl = modalEl . querySelector ( '#domain' ) ;
106+ const successEl = modalEl . querySelector ( '#discovery-success' ) ;
107+ const failureEl = modalEl . querySelector ( '#discovery-failure' ) ;
108+ const detailsEl = modalEl . querySelector ( '#details' ) ;
109+ const idEl = modalEl . querySelector ( '#id' ) ;
110+ if ( ! [ domainEl , successEl , failureEl , detailsEl ] . every ( Boolean ) ) {
111+ return ;
112+ }
113+
114+ domainEl . addEventListener ( 'change' , async ( ) => {
115+ try {
116+ detailsEl . classList . add ( 'opacity-50' ) ;
117+ successEl . classList . replace ( 'd-flex' , 'd-none' ) ;
118+ failureEl . classList . replace ( 'd-flex' , 'd-none' ) ;
119+ const { authorization_endpoint, token_endpoint, userinfo_endpoint } = await get ( `/plugins/oauth2-multiple/discover?domain=${ encodeURIComponent ( domainEl . value ) } ` ) ;
120+
121+ if ( authorization_endpoint ) {
122+ modalEl . querySelector ( '#authUrl' ) . value = authorization_endpoint ;
123+ }
124+
125+ if ( token_endpoint ) {
126+ modalEl . querySelector ( '#tokenUrl' ) . value = token_endpoint ;
127+ }
128+
129+ if ( userinfo_endpoint ) {
130+ modalEl . querySelector ( '#userRoute' ) . value = userinfo_endpoint ;
131+ }
132+
133+ successEl . classList . replace ( 'd-none' , 'd-flex' ) ;
134+ } catch ( e ) {
135+ console . log ( e ) ;
136+ failureEl . classList . replace ( 'd-none' , 'd-flex' ) ;
137+ } finally {
138+ detailsEl . classList . remove ( 'opacity-50' ) ;
139+ idEl . focus ( ) ;
140+ }
141+ } ) ;
142+ }
143+
87144function handleDeleteStrategy ( ok , name ) {
88145 if ( ! ok ) {
89146 return ;
0 commit comments