Giter VIP home page Giter VIP logo

zalo-auth-capacitor-plugin's Introduction

zalo-auth-capacitor-plugin

Zalo Authenticate through CapacitorJS

Currently, I don't plan for maintainance. Instead, mail to me ([email protected]) if need any help. Thanks for using my plugins!

๐ŸŽ‰ Happy to check your Pull Request at anytime.



Installation:

npm install zalo-auth-capacitor-plugin
npx cap sync

Configuration:

1. iOS:

1. In ios/App/Podfile:

Inside target 'App', append:

target 'App' do
  capacitor_pods
  # Add your Pods here
  ...
  pod 'ZaloSDK'
  ...
end

2. In ios/App/App/AppDelegate.swift:

  1. Add to import list:
import ZaloSDK
  1. Inside AppDelegate class, take changes in 2 methods below:
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    ...
    ZaloSDK.sharedInstance()?.initialize(withAppId: "APP_ID_FROM_ZALO_DEVELOPER");
    ...
    return true
  }
  func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    ...
    if CAPBridge.handleOpenUrl(url, options) {
      if url.absoluteString.hasPrefix("zalo-APP_ID_FROM_ZALO_DEVELOPER") {
        return ((ZDKApplicationDelegate.sharedInstance()?.application(app, open: url, options: options)) != nil);
      }
    }
    ...
    return false;
  }

2. In ios/App/App/Info.plist:

Append or fill out these lines:

...
<key>CFBundleURLTypes</key>
<array>
  ...
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string>zalo</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>zalo-APP_ID_FROM_ZALO_DEVELOPER</string>
    </array>
  </dict>
  ...
</array>
...

2. Android:

1. In android/src/build.gradle:

Inside dependencies { ... }, append:

implementation "com.zing.zalo.zalosdk:core:+"
implementation "com.zing.zalo.zalosdk:auth:+"
implementation "com.zing.zalo.zalosdk:openapi:+"

2. In android/src/main/java/..../MainActivity.java:

  1. Import class.
    import com.zing.zalo.zalosdk.oauth.ZaloSDK;
  2. In the callback of `this.init(savedInstanceState, ....), append:
    add(com.rin.zaloauth.ZaloAuthCapacitorPlugin.class);
  3. In onActivityResult method, under super.onActivityResult(requestCode, resultCode, data); append:
    ZaloSDK.Instance.onActivityResult(this, requestCode, resultCode, data);

3. In android/src/main/res/values/strings.xml:

Inside resources, create Zalo App on Zalo Developer, get back APP_ID (may be similar 42735613396xxxx150):

<resources>
    ...
    <string name="zalo_app_id">APP_ID_FROM_ZALO_DEVELOPER</string>
    <string name="zalo_login_protocol_scheme">zalo-APP_ID_FROM_ZALO_DEVELOPER</string>
    ...
</resources>

4. In android/src/main/AndroidManifest.xml:

  1. Add name attribute to your application.
<application
    android:name="com.zing.zalo.zalosdk.oauth.ZaloSDKApplication"
    ...
>
    ...
    <meta-data 
        android:name="com.zing.zalo.zalosdk.appID"   
        android:value="@string/zalo_app_id" 
    />
    <activity
        android:name="com.zing.zalo.zalosdk.oauth.BrowserLoginActivity"
    >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="@string/zalo_login_protocol_scheme" />
        </intent-filter>
    </activity>

    ...
</application>

5. Create Zalo Service for using in your application: (web has not implemented yet)

import { Injectable } from '@angular/core';
import { Plugins } from '@capacitor/core';
import { isPlatform } from '@ionic/angular';
import {
  ShareInput,
  ZaloAuthCapacitorPlugin,
  ZaloAuthCapacitorPluginPlugin,
} from 'zalo-auth-capacitor-plugin';

@Injectable({ providedIn: 'root' })
export class LoginZaloService {
  private _zaloLogin: ZaloAuthCapacitorPluginPlugin;

  constructor() {
    this._setupZaloLogin();
  }
  async login() {
    return this._zaloLogin.login();
  }
  async getUserProfileFromZalo() {
    return this._zaloLogin.getUserProfile();
  }
  logout() {
    return this._zaloLogin.logout();
  }
  share(input: ShareInput) {
    return this._zaloLogin.share(input);
  }
  private _setupZaloLogin() {
    if (isPlatform('desktop')) {
      this._zaloLogin = ZaloAuthCapacitorPlugin;
    } else {
      this._zaloLogin = Plugins.ZaloAuthCapacitorPlugin as ZaloAuthCapacitorPluginPlugin;
    }
  }
}

3. Web

2. Set up URL Login with Zalo in Login page

appID = '*********************';
redirectURI = `Your Callback URL`;
zaloLogin = `https://oauth.zaloapp.com/v3/permission?app_id=${appID}&redirect_uri=${redirectURI}&state=${anything}`

3. Set up page Redirect

const url = new URL(getURL);
const uid = url.searchParams.get("uid"); //ID of user
const code = url.searchParams.get("code"); //OAuthCode
const state = url.searchParams.get("state"); //state in step 2

zalo-auth-capacitor-plugin's People

Contributors

thanhhoa214 avatar truongvh080999 avatar

Watchers

 avatar  avatar

Forkers

dylansproule

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.