alexziskind1 / nativescript-oauth

Other
35 stars 53 forks source link

Login does not capture token and resolve promise #64

Closed rokkuchan closed 6 years ago

rokkuchan commented 6 years ago

This is after updating tns cli to 3.4.2 and caused this to fail. I was on very early version (June 2017) i believe in 2.5.x version.

The code below will execute and goes to the facebook login view. After signing in, it will go to the success.html but it stops there and doesn't do anything (see screenshot).

This is the typical code below for login.

tnsOAuthModule.login() //same when used with ensureValidToken()
    .then(()=>{
        console.log('logged in');  // THIS DOES NOT EXECUTE FROM HERE
        console.dir("accessToken " + tnsOAuthModule.accessToken());
    })
    .catch((er)=>{
        //NO ERRORS HERE
        alert(er);
    });

When it successfully logs in, facebook will redirect to an URL that has the "code" in the query string (BRIEFLY), before redirecting to the "success.html" seen in screenshot.

What it seems to be doing is that it's not returning the promise and stuck somewhere in one of the tns oauth helpers. It isn't calling the "checkCodeIntercept" function in tns-oauth.js, which is suppose to intercept the redirect and capture the "code" or "token" in the url query string.

I believe this is a bug. Can someone please help.

screen shot 2018-02-07 at 8 47 05 pm
MrfksIv commented 6 years ago

I have exactly the same issue but only for iOS builds. For Android, everything works just fine - hope this helps!

jackwaller commented 6 years ago

I also have the same issue for iOS

rokkuchan commented 6 years ago

I've reverted back to tns v3.2.0 and everything seems to work fine.

However, Google sign does not work anymore (also latest tns version) with a warning message saying only certain browsers support the webview oauth request.

jackwaller commented 6 years ago

The helper functions are not getting called when the webview is being loaded. The intercept happens when the TnsOAuthWebViewHelper.prototype.webViewDidFinishLoad function is called. Not sure what would have changed between v3.2 and v3.4 for this to occur... But looks like I will have to downgrade

jackwaller commented 6 years ago

I reverted everything back to 3.2 through still no luck. Do you mind posting your package.json?

rokkuchan commented 6 years ago

I pretty much worked off one of the sample applications.

{ "name": "", "version": "1.0.0", "description": "", "repository": { "type": "git", "url": "" }, "keywords": [ "" ], "author": "", "license": "", "bugs": { "url": "" }, "homepage": "", "nativescript": { "id": "au.com.dishitup", "tns-ios": { "version": "3.2.0" }, "tns-android": { "version": "3.2.0" } }, "scripts": { "tslint": "tslint -p tsconfig.json", "ns-bundle": "ns-bundle", "publish-ios-bundle": "npm run ns-bundle --ios --publish-app", "generate-android-snapshot": "generate-android-snapshot --targetArchs arm,arm64,ia32 --install", "start-android-bundle": "npm run ns-bundle --android --run-app", "start-ios-bundle": "npm run ns-bundle --ios --run-app", "build-android-bundle": "npm run ns-bundle --android --build-app", "build-ios-bundle": "npm run ns-bundle --ios --build-app" }, "dependencies": { "@angular/common": "~4.2.3", "@angular/compiler": "~4.2.3", "@angular/core": "~4.2.3", "@angular/forms": "~4.2.3", "@angular/http": "~4.2.3", "@angular/platform-browser": "~4.2.3", "@angular/router": "~4.2.3", "email-validator": "^1.0.7", "nativescript-angular": "next", "nativescript-iqkeyboardmanager": "1.0.1", "nativescript-oauth": "^2.0.1", "nativescript-social-share": "~1.3.2", "nativescript-unit-test-runner": "^0.3.3", "reflect-metadata": "^0.1.8", "rxjs": "^5.4.0", "tns-core-modules": "^3.2.0", "zone.js": "^0.8.11" }, "devDependencies": { "@angular/compiler-cli": "~4.2.3", "@ngtools/webpack": "~1.5.3", "babel-traverse": "6.24.1", "babel-types": "6.24.1", "babylon": "6.17.0", "codelyzer": "^3.0.1", "copy-webpack-plugin": "~4.0.1", "extract-text-webpack-plugin": "~3.0.0", "filewalker": "^0.1.3", "jasmine-core": "^2.6.2", "karma": "^1.7.0", "karma-jasmine": "^1.0.2", "karma-nativescript-launcher": "^0.4.0", "lazy": "1.0.11", "nativescript-css-loader": "~0.26.0", "nativescript-dev-typescript": "^0.4.5", "nativescript-dev-webpack": "next", "raw-loader": "~0.5.1", "resolve-url-loader": "~2.1.0", "tslint": "^5.4.2", "typescript": "~2.3.4", "webpack": "~3.2.0", "webpack-bundle-analyzer": "^2.8.2", "webpack-sources": "~1.0.1", "nativescript-worker-loader": "~0.8.1" } }

rokkuchan commented 6 years ago

package.txt

Fufuxx commented 6 years ago

I have the same issue. loginPageFunc in the oauth page provider runs but the checkCodeIntercept passed in never runs in iOs (works fine on android).

Reverting back to 3.2.0 does not resolve for me and I cannot point and solve the issue (I'll keep trying though).

Package.json is:

{ "description": "NativeScript Application", "license": "SEE LICENSE IN ", "readme": "NativeScript Application", "repository": "", "nativescript": { "id": "org.nativescript.qlabsapp", "tns-ios": { "version": "3.4.1" }, "tns-android": { "version": "3.4.1" } }, "scripts": { "lint": "tslint \"app/*/.ts\"" }, "dependencies": { "@angular/animations": "~5.2.0", "@angular/common": "~5.2.0", "@angular/compiler": "~5.2.0", "@angular/core": "~5.2.0", "@angular/forms": "~5.2.0", "@angular/http": "~5.2.0", "@angular/platform-browser": "~5.2.0", "@angular/platform-browser-dynamic": "~5.2.0", "@angular/router": "~5.2.0", "nativescript-angular": "~5.2.0", "nativescript-oauth": "^2.0.1", "nativescript-pro-ui": "~3.3.0", "nativescript-theme-core": "~1.0.4", "reflect-metadata": "~0.1.10", "rxjs": "~5.5.5", "tns-core-modules": "~3.4.0", "zone.js": "~0.8.18" }, "devDependencies": { "@angular/compiler-cli": "~5.2.0", "@ngtools/webpack": "~1.9.4", "babel-traverse": "6.26.0", "babel-types": "6.26.0", "babylon": "6.18.0", "codelyzer": "~4.0.2", "copy-webpack-plugin": "~4.3.0", "css-loader": "~0.28.7", "extract-text-webpack-plugin": "~3.0.2", "lazy": "1.0.11", "nativescript-dev-sass": "~1.3.5", "nativescript-dev-typescript": "~0.6.0", "nativescript-dev-webpack": "~0.9.1", "nativescript-worker-loader": "~0.8.1", "raw-loader": "~0.5.1", "resolve-url-loader": "~2.2.1", "sass-loader": "~6.0.6", "tslint": "~5.8.0", "typescript": "~2.6.2", "uglifyjs-webpack-plugin": "~1.1.6", "webpack": "~3.10.0", "webpack-bundle-analyzer": "^2.9.1", "webpack-sources": "~1.1.0" } }

jackwaller commented 6 years ago

Your package.json is still showing version 3.4.

The following commands should do it

npm install tns-core-modules@3.2 tns platform remove ios tns platform remove android tns platform add ios@3.2 tns platform add android@3.2

Jack

On 22 Feb 2018, 12:29 AM +1000, Florian notifications@github.com, wrote:

I have the same issue. loginPageFunc in the oauth page provider runs but the checkCodeIntercept passed in never runs in iOs (works fine on android). Reverting back to 3.2.0 does not resolve for me and I cannot point and solve the issue (I'll keep trying though). Package.json is: { "description": "NativeScript Application", "license": "SEE LICENSE IN ", "readme": "NativeScript Application", "repository": "", "nativescript": { "id": "org.nativescript.qlabsapp", "tns-ios": { "version": "3.4.1" }, "tns-android": { "version": "3.4.1" } }, "scripts": { "lint": "tslint \"app/*/.ts\"" }, "dependencies": { "@angular/animations": "~5.2.0", "@angular/common": "~5.2.0", "@angular/compiler": "~5.2.0", "@angular/core": "~5.2.0", "@angular/forms": "~5.2.0", "@angular/http": "~5.2.0", "@angular/platform-browser": "~5.2.0", "@angular/platform-browser-dynamic": "~5.2.0", "@angular/router": "~5.2.0", "nativescript-angular": "~5.2.0", "nativescript-oauth": "^2.0.1", "nativescript-pro-ui": "~3.3.0", "nativescript-theme-core": "~1.0.4", "reflect-metadata": "~0.1.10", "rxjs": "~5.5.5", "tns-core-modules": "~3.4.0", "zone.js": "~0.8.18" }, "devDependencies": { "@angular/compiler-cli": "~5.2.0", "@ngtools/webpack": "~1.9.4", "babel-traverse": "6.26.0", "babel-types": "6.26.0", "babylon": "6.18.0", "codelyzer": "~4.0.2", "copy-webpack-plugin": "~4.3.0", "css-loader": "~0.28.7", "extract-text-webpack-plugin": "~3.0.2", "lazy": "1.0.11", "nativescript-dev-sass": "~1.3.5", "nativescript-dev-typescript": "~0.6.0", "nativescript-dev-webpack": "~0.9.1", "nativescript-worker-loader": "~0.8.1", "raw-loader": "~0.5.1", "resolve-url-loader": "~2.2.1", "sass-loader": "~6.0.6", "tslint": "~5.8.0", "typescript": "~2.6.2", "uglifyjs-webpack-plugin": "~1.1.6", "webpack": "~3.10.0", "webpack-bundle-analyzer": "^2.9.1", "webpack-sources": "~1.1.0" } } — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

ownleviathan commented 6 years ago

I am facing the same problem, seems that is works for IOS when I downgrade it to 3.2, but for my project, some functionalities are not working when I downgraded.

Is it possible to have a soon solution for version 3.4?

Fufuxx commented 6 years ago

Modified the tns-oauth-webview-helper.ios.ts as follow to use WKNavigationDelegate rather than UIWebViewDelegate:

import { WebView } from 'ui/web-view';

//Change from UIWebViewDelegate
export class TnsOAuthWebViewHelper extends NSObject implements WKNavigationDelegate {
    //public static ObjCProtocols = [UIWebViewDelegate];
    public static ObjCProtocols = [WKNavigationDelegate];
    private _owner: WeakRef<WebView>;
    private _origDelegate: any; //UIWebViewDelegateImpl
    private _checkCodeIntercept: (WebView, error?, url?) => boolean;

    constructor() {
        super();
    }

    public static initWithWebViewAndIntercept(wv: WebView, checkCodeIntercept) {
        (<any>wv)._delegate = TnsOAuthWebViewHelper.initWithOwner(new WeakRef(wv), checkCodeIntercept);
    }

    public static initWithOwner(owner: WeakRef<WebView>, checkCodeIntercept): TnsOAuthWebViewHelper {
        let handler = <TnsOAuthWebViewHelper>TnsOAuthWebViewHelper.new();
        handler._owner = owner;
        handler._origDelegate = (<any>owner.get())._delegate;
        handler._checkCodeIntercept = checkCodeIntercept;
        return handler;
    }

    public webViewDidFinishNavigation(webView: WKWebView, navigation: WKNavigation): void {
        this._checkCodeIntercept(webView, null);
        this._origDelegate.webViewDidFinishNavigation(webView, navigation);
    }

    public webViewDidFailNavigationWithError(webView: WKWebView, navigation: WKNavigation, error: NSError): void {
        this._checkCodeIntercept(webView, err);
        this._origDelegate.webViewDidFailNavigationWithError(webView, navigation, error);
    }

Then just added on a new condition 'else if' on the loginViaAuthorizationCodeFlow in the tns-oauth for the interceptor to handle the new webView format (WKWebView):

else if (webView.URL && webView.URL.absoluteString) {
                    console.log('Webview Url -> '+webView.URL.absoluteString);
                    retStr = webView.URL.absoluteString;

And it works for me ! With version 3.4.1 :)

ownleviathan commented 6 years ago

I reverting the ios version to 3.2, but after that Android is facing the same problem. When tried to call the frameModule.topmost().goBack() in function loginViaAuthorizationCodeFlow(credentials, successPage) .. never goes back and shows that is stuck..

In the next code, is supposed to print the token after login, but never goes to that step

tnsOAuthModule.ensureValidToken() .then((token: string) => { console.log('Token--------->: ' + token); }) .catch((er) => { console.error('error logging in'); console.dir(er); });

This situation is happening when I logging, to have access to my app I need to put the app in the back, the app reloads and works.

Any ideas?

This is my package.json { "description": "NativeScript Application", "license": "SEE LICENSE IN ", "readme": "NativeScript Application", "repository": "", "nativescript": { "id": "com.cisco.mbr", "tns-ios": { "version": "3.2.0" }, "tns-android": { "version": "3.2.0" } }, "dependencies": { "@angular/animations": "4.2.5", "@angular/common": "4.2.5", "@angular/compiler": "4.2.5", "@angular/core": "4.2.5", "@angular/forms": "4.2.5", "@angular/http": "4.2.5", "@angular/platform-browser": "4.2.5", "@angular/router": "4.2.5", "nativescript-angular": "4.2.0", "nativescript-bottombar": "3.0.7", "nativescript-cardview": "2.0.4", "nativescript-checkbox": "3.0.1", "nativescript-fingerprint-auth": "6.0.0", "nativescript-floatingactionbutton": "4.1.3", "nativescript-grid-view": "3.2.1", "nativescript-loading-indicator": "2.4.0", "nativescript-ngx-slides": "0.4.3", "nativescript-oauth": "../nativescript-oauth", "nativescript-pro-ui": "3.4.0", "nativescript-pulltorefresh": "^2.1.0", "nativescript-sqlite": "1.1.7", "nativescript-theme-core": "1.0.4", "reflect-metadata": "0.1.10", "rxjs": "5.4.3", "tns-core-modules": "3.2.0", "zone.js": "0.8.17" }, "devDependencies": { "babel-traverse": "6.4.5", "babel-types": "6.4.5", "babylon": "6.4.5", "codelyzer": "3.2.0", "lazy": "1.0.11", "nativescript-dev-sass": "1.3.2", "nativescript-dev-typescript": "0.5.1", "node-sass": "4.5.3", "tslint": "5.7.0", "typescript": "2.5.2" } }

alexziskind1 commented 6 years ago

Thanks @Fufuxx for your suggestion. I've rolled this into the plugin and now iOS side works as expected.