I have been trying to find a useful library to authenticate with a generic provider using oauth2. I am new to Ionic, and coudln't find a library that could help me accomplish this easily. I also see that most of the libraries are limited to mobile devices so I decided to go the hard way and try to write the code myself. I apologize if the code lucks ugly or awfully wrong. I have done some PHP programming a long time ago and I am trying to get back to programming with this.
I first generated a provider page with the command: ionic g provider MakeHttpRequest. Here is a what I wrote so far, just the first step in getting the token.
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the MakeHttpRequest provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class MakeHttpRequest {
constructor
(
public http: Http,
private client_name : string,
private client_id : string,
private client_secret : string,
private end_point : string,
private auth_uri : string,
private redirect_uri : string
) {
console.log('Hello MakeHttpRequest Provider');
this.client_name = 'Alex';
this.client_id = 'lalala';
this.client_secret = 'lalalalala';
this.end_point = 'http://www.lalaland.com';
this.auth_uri = 'https://services.lalaland.com/oauth/authorize';
this.redirect_uri = 'http://localhost/callback';
}
public getcode(){
this.http.get("${this.auth_uri}?client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&response?type=code")
.map(res => res.json()).subscribe(data => {
console.log(data);
});
}
}
then imported the class as follows into home.ts:
import {MakeHttpRequest} from '../../providers/make-http-request';
created the constructor still under home.ts as follows: constructor(public navCtrl: NavController, private platform : Platform, private MakeHttpRequest : MakeHttpRequest)
and created the following function:
public button(){
this.MakeHttpRequest.getcode()
}
then added the button function to the home.html page.
But all I get is an error. Can someone tell me if I'm in the right direction or even close. Any help or comments are highly appreciated.