Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ionic native - MS Adal in ionic project?

I'm new to Ionic and I want to authenticate the user using Azure. So I'm using MS ADAL Ionic Native in my project. I couldn't find any proper example online. Here is my app.component.ts

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { MSAdal, AuthenticationContext, AuthenticationResult } from '@ionic- 
native/ms-adal';

import { HomePage } from '../pages/home/home';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = HomePage;

  constructor(private msAdal: MSAdal,platform: Platform, statusBar: 
              StatusBar, splashScreen: SplashScreen) {
       platform.ready().then(() => {

           let authContext: AuthenticationContext= 
this.msAdal.createAuthenticationContext('https://login.windows.net/common');

  authContext.acquireTokenAsync('https://graph.windows.net', '[My-appID]', 'http://localhost:8000')
    .then((authResponse: AuthenticationResult) => {
       console.log('Token is' , authResponse.accessToken);
       console.log('Token will expire on', authResponse.expiresOn);
    })
    .catch((e: any) => 
    alert(e));
    statusBar.styleDefault();
    splashScreen.hide();
     });
   }
 }

I'm getting the following error.

TypeError: Wrong type for parameter "userId" of AuthenticationContext.acquireTokenAsync:Expected String,but got Function.

like image 755
Rathnakara S Avatar asked Jun 27 '18 18:06

Rathnakara S


People also ask

What are ionic native plugins?

Ionic Native is a TypeScript wrapper for Cordova/PhoneGap plugins that make adding any native functionality you need to your Ionic mobile app easy.

Is ionic native?

Ionic React is an open source UI and Native API project consisting of cross-platform UI components and native functionality for building iOS, Android, Electron and Progressive Web Apps using React and standard web technology.


1 Answers

I have got a work around for the issue, i'm posting my answer below. If anyone is facing the same issue they can try this.

Sending empty string for userId and extraQueryParameters in acquireTokenAsync function solved the issue for me.

let authContext: AuthenticationContext=
this.msAdal.createAuthenticationContext('https://login.windows.net/common');
authContext.acquireTokenAsync("https://graph.windows.net", "[My-appID]",
"http://localhost:8000","","")
.then((authResponse: AuthenticationResult) => {
console.log('Token is' , authResponse.accessToken);
console.log('Token will expire on', authResponse.expiresOn);
})
.catch((e: any) =>
alert(e));
like image 85
Rathnakara S Avatar answered Sep 21 '22 18:09

Rathnakara S