alexziskind1 / nativescript-oauth2

Other
86 stars 93 forks source link

[Question] Calling configureTnsOAuth after app bootstrapping #150

Closed tommag21 closed 3 years ago

tommag21 commented 3 years ago

In my Nativescript-Vue app, I need to configure a Microsoft provider with data received from a web request. Since it's not recommended performing asynchronous tasks while the app bootstraps, I make my request when the first page loads and then I configure the provider through configureTnsOAuth. While this works well on Android, on iOS something strange happens. After a successful login inside the webview, redirect doesn't work and I can't go back to the app unless I cancel.

I am aware that the documentation tells to configure the providers on startup, but is there a way to do it inside a component?


EDIT:

I implemented this and now it works, is it safe or might it cause issues?

main.js

import * as platform from "tns-core-modules/platform";
import { TnsOAuthClientAppDelegate } from "nativescript-oauth2/delegate";

if (platform.isIOS) {
  application.ios.delegate = TnsOAuthClientAppDelegate;
}

FirstPage.vue

// ...
async onPageLoaded() {
  let config = await api.getAzureConfig();

  let provider = new TnsOaProviderMicrosoft({
    openIdSupport: "oid-full",
    clientId: config.clientId,
    redirectUri: "myscheme://mobile-auth/",
    urlScheme: "myscheme",
    scopes: ["openid","email","profile","offline_access"]
  });
  provider.authority = config.authority;

  configureTnsOAuth([ provider ]);
}

// later on
login() {
  return new Promise((resolve, reject) => {
    let client = new TnsOAuthClient("microsoft");
    client.loginWithCompletion((tokenResult, error) => {
      if (error) reject();
      else resolve(tokenResult);
    });
  });
}
// ...
alexziskind1 commented 3 years ago

Glad you figured out a solution. I haven't tried a remote config scenario. But if this is working for you, then great! You might want to check for existing iOS delegates though, because if other plugins also define an app delegate, then you have to take care of it too.