Shraddha
Shraddha

Reputation: 1

OAuth2 Plugin to login using refresh tokens

I have a mobile application where I am trying to authenticate the user using the nativescript-oauth2 plugin with custom providers using Azure B2C. My requirement is that I want to make the user login for the first time to the application using their credentials. After the user has successfully logged in, I need to store the refresh token of the user and use these stored refresh token for authentication when next time the user is logging in to the mobile application. Using the refresh tokens, I want to generate all the tokens again.

I have tried using the refreshTokenWithCompletion() method provided by the plugin but as the document states that this is for refreshing the access tokens(OAuth 2 Plugin for NativeScript).

Upvotes: 0

Views: 450

Answers (1)

Application Testing
Application Testing

Reputation: 31

I too have an issue with fetching fresh token results using "refreshTokenWithCompletion".

below is my code

import { TnsOAuthClient, ITnsOAuthTokenResult, configureTnsOAuth } from 'nativescript-oauth2';
import { AuthProvider } from './auth-provider.service';
import { TnsOaUnsafeProviderOptions } from 'nativescript-oauth2/providers/providers';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  private client: TnsOAuthClient = null;
  private customProvider: AuthProvider = null;

  private customProviderOptions: TnsOaUnsafeProviderOptions = {
    openIdSupport: "oid-none",
    clientId: "",
    redirectUri: "https://b2clogin.com/te/XXXXXXXXXXXXXX/XXXXXX_XXXXX_XXXXX/oauth2/authresp",
    scopes: ["openid https://XXXXXXXXXXXXXX.onmicrosoft.com/mobileapi/user_impersonation offline_access"],
    clientSecret: "",
    customQueryParams: {
      "p": "XXXXXX_XXXXX_XXXXX",
      "nonce": "defaultNonce",
      "response_mode": "query",
      "prompt": "login"
    }
};

  constructor() {
    this.customProvider = new AuthProvider(this.customProviderOptions);
    configureTnsOAuth([this.customProvider]);
   }

  public Login(providerType): Promise<ITnsOAuthTokenResult> {

    console.log('In Login');

    this.client = new TnsOAuthClient(providerType);

    return new Promise<ITnsOAuthTokenResult>((resolve, reject) => {
      this.client.loginWithCompletion((tokenResult: ITnsOAuthTokenResult, error) => {
          if (error) {
            console.error("back to main page with error: ");
            console.error(error);
            // reject(error);
          } else {
            console.log("back to main page with access token: ");
            console.log(tokenResult);
            // resolve(tokenResult);
          }
        });
    });
  }

  public Logout(): Promise<any> {

    return new Promise<any>((resolve, reject) => {
      if (this.client) {
        this.client.logoutWithCompletion((error) => {
            if (error) {
              console.error("back to main page with error: ");
              console.error(error);
              reject(error);
            } else {
              console.log("back to main page with success");
              resolve();
            }
          });
      }
      else {
        console.log("back to main page with success");
        resolve();
      }
    });

  }

  public RefreshAccess() {

    this.client.refreshTokenWithCompletion((tokenResult: ITnsOAuthTokenResult, error) => {
      if (error) {
        console.error("Unable to refresh token with error: ");
        console.error(error);
      } else {
        console.log("Successfully refreshed access token: ");
        console.log(tokenResult);
      }
    });

  }

}

Please let me know if there is any correction,

Error: Uncaught (in promise): URIError: URI malformed

I am using Azure AD B2C for identity management.

Upvotes: 0

Related Questions