@@ -168,3 +168,53 @@ pub fn cancel(port: u16) -> Result<(), std::io::Error> {
168
168
169
169
Ok ( ( ) )
170
170
}
171
+
172
+ mod plugin_impl {
173
+ use tauri:: { Manager , Runtime , Window } ;
174
+
175
+ #[ tauri:: command]
176
+ pub ( crate ) fn start < R : Runtime > (
177
+ window : Window < R > ,
178
+ config : Option < super :: OauthConfig > ,
179
+ ) -> Result < u16 , String > {
180
+ let mut config = config. unwrap_or_default ( ) ;
181
+ if config. response . is_none ( ) {
182
+ config. response = window
183
+ . config ( )
184
+ . plugins
185
+ . 0
186
+ . get ( "oauth" )
187
+ . map ( |v| v. as_str ( ) . unwrap ( ) . to_string ( ) . into ( ) ) ;
188
+ }
189
+
190
+ crate :: start_with_config ( config, move |url| match url:: Url :: parse ( & url) {
191
+ Ok ( _) => {
192
+ if let Err ( emit_err) = window. emit ( "oauth://url" , url) {
193
+ log:: error!( "Error emitting oauth://url event: {}" , emit_err)
194
+ } ;
195
+ }
196
+ Err ( err) => {
197
+ if let Err ( emit_err) = window. emit ( "oauth://invalid-url" , err. to_string ( ) ) {
198
+ log:: error!( "Error emitting oauth://invalid-url event: {}" , emit_err)
199
+ } ;
200
+ }
201
+ } )
202
+ . map_err ( |err| err. to_string ( ) )
203
+ }
204
+
205
+ #[ tauri:: command]
206
+ pub ( crate ) fn cancel ( port : u16 ) -> Result < ( ) , String > {
207
+ crate :: cancel ( port) . map_err ( |err| err. to_string ( ) )
208
+ }
209
+ }
210
+
211
+ /// Initializes the plugin.
212
+ #[ must_use]
213
+ pub fn init < R : Runtime > ( ) -> TauriPlugin < R > {
214
+ Builder :: new ( "oauth" )
215
+ . invoke_handler ( tauri:: generate_handler![
216
+ plugin_impl:: start,
217
+ plugin_impl:: cancel
218
+ ] )
219
+ . build ( )
220
+ }
0 commit comments