• Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Angular oauth library for use with Apache Cordova projects

Build Status PayPal Bitcoin

Angular 2 Cordova Oauth

ng2-cordova-oauth is an Oauth library which easily integrates in Angular2/Ionic2 or any other WEB or Cordova applications. The purpose of this library is to quickly and easily obtain an access token from various web services to use their APIs.

Donations

If you found this project useful, please consider donating some Bitcoin to the following address:

1M8SKTepmgA2KAUSZc7LpXN1owaky1DjNM

Requirements

For Cordova application:

For Web application:

  • webpack, systemjs or amd loaders

Installing ng2-cordova-oauth Into Your Project

Installing

From the root of your Apache Cordova project, execute the following:

npm install ng2-cordova-oauth --save

This will install ng2-cordova-oauth and its dependencies.

Injecting:

There are 2 types of entities in the library: Platform (i.e., Cordova, Browser) and Provider (i.e., Facebook, LinkedIn, etc.). Each provider has it's own class. You need to inject the Platform class into every class in which you wish to use them. For example, if you wish to use Facebook oauth in a particular class, it would look something like:

import {Facebook, Google} from 'ng2-cordova-oauth/core';
import {OauthBrowser} from 'ng2-cordova-oauth/platform/browser'
// or
import {OauthCordova} from 'ng2-cordova-oauth/platform/cordova'

Alternatively you can use Angular2 Injector in order to provide platform specific service for all components:

import {bootstrap} from '@angular/platform-browser-dynamic'
import {App} from './app.component'
import {OauthCordova} from 'ng2-cordova-oauth/platform/cordova'
import {Oauth} from 'ng2-cordova-oauth/oauth'

bootstrap(App, [
  { provide: Oauth, useClass: OauthCordova }
])

// and later in component

@Component({
  selector: 'my-component'
})
class MyComponent {
  constructor(oauth: Oauth) {
    this.oauth = oauth
  }
}

Using ng2-cordova-oauth In Your Project

Each web service API acts independently in this library. However, when configuring each web service, one thing must remain consistent.

Currently it supports several oAuth providers: Facebook, Instagram, LinkedIn, Google, Meetup, Imgur. Example of creating oAuth provider:

const provider = new Facebook({
    clientId: string,
    appScope?: string[],
    redirectUri?: string,
    responseType?: string,
    authType?: string
});

Each API call returns a promise. The success callback will provide a response object and the error callback will return an Error object. Not all providers use implicit grants. Any provider that uses an explicit grant will return a code rather than an access_token. The code must be further exchanged server side for an access_token. This is for the safety of your users.

const oauth = new OauthCordova();
const provider = new Facebook({
  clientId: "CLIENT_ID_HERE",
  appScope: ["email"]
})

oauth.logInVia(provider).then((success) => {
    console.log(JSON.stringify(success));
}, (error) => {
    console.log(JSON.stringify(error));
});

As of Apache Cordova 5.0.0, the whitelist plugin must be used in order to reach external web services.

Now this library can work with a web browser, ionic serve, or ionic view in case if you use OauthPlatform service but do not forget to replace it with correct one for cordova project (i.e., OauthCordova)

Important Note About Google

Google, as of October 2016, has started blocking requests from web views commonly found in hybrid applications. For this reason, support for Google has been removed from this library.

More information can be found at:

https://developers.googleblog.com/2016/08/modernizing-oauth-interactions-in-native-apps.html

A Working Example

import {Component} from '@angular/core';
import {NavController, Platform} from 'ionic-angular';
import {Facebook, Google, LinkedIn} from "ng2-cordova-oauth/core";
import {OauthCordova} from 'ng2-cordova-oauth/platform/cordova';

@Component({
    templateUrl: 'build/pages/home/home.html'
})
export class HomePage {

    private oauth: OauthCordova = new OauthCordova();
    private facebookProvider: Facebook = new Facebook({
      clientId: "CLIENT_ID_HERE",
      appScope: ["email"]
    })

    constructor(private navCtrl: NavController, private platform: Platform) { }

    public facebook() {
        this.platform.ready().then(() => {
            this.oauth.logInVia(this.facebookProvider).then(success => {
                console.log("RESULT: " + JSON.stringify(success));
            }, error => {
                console.log("ERROR: ", error);
            });
        });
    }

}

Alternatively you can inject OauthCordova in constructor as shown in examples above.

Custom browser window options

Browser's window.open and Cordova's InAppBrowser supports bunch of options which can be passed as a second argument to logInVia. For example if you don't know want to clear session cache, or place toolbar at the top for iOS:

new OauthCordova().logInVia(facebookProvider, {
  clearsessioncache: 'no',
  toolbarposition: 'top'
})

the list of all available options can be found:

Version History

Coming soon...

Contribution Rules

All contributions must be made via the development branch. This keeps the project more maintainable in terms of versioning as well as code control.

Have a question or found a bug (compliments work too)?

This project is maintained by Nic Raboy.

Tweet Nic Raboy on Twitter - @nraboy

Resources

Ionic 2 - http://www.ionicframework.com

Angular 2 - https://www.angular.io

Apache Cordova - http://cordova.apache.org

Nic Raboy's Code Blog - https://www.thepolyglotdeveloper.com

More Repositories

1

ng-cordova-oauth

AngularJS oauth library for use with Apache Cordova projects
JavaScript
456
star
2

ubuntu-ionic-installer

Installs Android, Apache Ant, Java JDK, NodeJS, Apache Cordova, and Ionic Framework
Shell
108
star
3

ng-cordova-facebook-example

Ionic Framework ngCordova Oauth Example for Facebook
JavaScript
104
star
4

unity3d-floppy-clone-game

Flappy Bird type game created with Unity3D
Java
48
star
5

open-ledger-micro

Go
41
star
6

ng-cordova-beacon

AngularJS wrapper for the Apache Cordova iBeacon plugin
JavaScript
39
star
7

hugo-phlat-theme

A flat theme for the Hugo static template engine
CSS
31
star
8

ubuntu-nativescript-installer

Install Node.js, the Android SDK, and Telerik NativeScript in Ubuntu via a single script
Shell
29
star
9

ionic-cipher-safe-app

Store passwords on Firebase using a master password and AES strength encryption with Ionic Framework
JavaScript
27
star
10

cordova-hooks

Useful hooks to be included in Apache Cordova, Ionic Framework, and Phonegap projects
JavaScript
15
star
11

android-spyfi-app

Mobile surveillance application for use with Foscam and Yanmix brand IP cameras.
Java
14
star
12

nativescript-ratings

TypeScript
10
star
13

stream-deck-webhooks

CSS
10
star
14

android-minesweeper-game

Java
9
star
15

ng-cordova-oauth-example

A simple test application for ng-cordova-oauth
JavaScript
9
star
16

ng-imgur

AngularJS library for making RESTful requests to Imgur
JavaScript
7
star
17

alexa-slick-dealer

Slick Dealer Skill for Amazon Alexa
Go
5
star
18

smartcar-here-example

HTML
3
star
19

mongo-draws

JavaScript
2
star
20

tracydevs-unity-example

C#
2
star
21

ionic-imgur-app

An unofficial Imgur photo-sharing application created with Ionic Framework and Apache Cordova
JavaScript
2
star
22

here-devrel-workshops

TypeScript
2
star
23

phaser-mongodb-leaderboards-example

JavaScript
1
star
24

simple_mongodb_node_example

JavaScript
1
star
25

atlas-search-kotlin-example

Kotlin
1
star