react-native-windows icon indicating copy to clipboard operation
react-native-windows copied to clipboard

Linking: addEventListener 'url' callback function not called during Oauth2 flow

Open nomartin opened this issue 8 years ago • 10 comments

Environment

  1. react-native -v:
react-native-cli: 2.0.1
react-native: 0.52.2
  1. npm ls rnpm-plugin-windows:
[email protected]
  1. npm ls react-native-windows:
[email protected]
  1. node -v:
v9.4.0
  1. npm -v:
5.6.0
  1. yarn --version:
N/A

Target Platform: UWP Target Platform Version(s): 10.0.14393 Target Device(s): Desktop Development Operating System: Windows Visual Studio Version: 2017

Steps to Reproduce

Run following code:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
  Linking
} from 'react-native';

export default class OauthExample extends Component {
  _handleURL(event) {
    console.log(event.url);
  }
  _microsoftLogin() {
    Linking.openURL([
      'https://login.microsoftonline.com/common/oauth2/v2.0/authorize', 
      '?response_type=code',
      '&client_id='+'---your client id---', 
      '&redirect_uri=---your redirect url---',
      '&scope=User.read' // Specify permissions
    ].join(''));
  }
  componentDidMount() {
    console.log('componentDidMount');
    Linking.addEventListener('url', this._handleURL);
  }
  render() {
    return (
      <View style={styles.container}>
        <TouchableOpacity onPress={this._microsoftLogin}>
          <Text style={styles.welcome}>
            Microsoft Login!
          </Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  }
});

AppRegistry.registerComponent('auth', () => OauthExample);

Expected Behavior

  1. Click the 'Microsoft Login!' text
  2. Browser launches MS log in
  3. Log into MS account
  4. Expect _handleURL() function to be called on App

Actual Behavior

_handleURL() function is not called.

nomartin avatar Feb 13 '18 04:02 nomartin

@nomartin - can you debug the native app and confirm in OnActivated is called in App.xaml.cs? If so, what is the value of the Kind property on the IActivatedEventArgs argument?

rozele avatar Apr 06 '18 14:04 rozele

Just confirmed it's a Protocol activation. Should be working...

rozele avatar Apr 06 '18 15:04 rozele

@rozele - Tried with the versions above (same project) and see the same issue. No console log and the debugger will not break for me in OnActivate.

I created a new project using RN v0.55 and RNW v0.54 (RNW v0.55 isn't available). That project consistently has the following error: image

Both TouchableOpacity and Button cause this error. NOTE: Simple <View><Text>text</Text></View> works. Will try again tomorrow.

nomartin avatar Apr 07 '18 03:04 nomartin

ok - downgraded to RN 0.54. Error goes away.

Still cannot get the breakpoint to fire. However, if this is working for you then I'd suspect user-error. You can just close this iisue and I'll watch it on my end.

nomartin avatar Apr 07 '18 04:04 nomartin

Have you found a solution @nomartin ? I can't make the listener trigger the function... I've seen around that many people are dealing with this issue as well...

fedealconada avatar Apr 16 '18 23:04 fedealconada

@nomartin probably a dumb question, but I assume you've updated the Package.appxmanifest to include the app protocol, correct?

rozele avatar Apr 17 '18 01:04 rozele

Yes, actually, Linking.getInitialURL() works. It is just that, when the app is open but running in the back, when I go from listener seems to have no effect...

fedealconada avatar Apr 17 '18 01:04 fedealconada

@fedealconada Hmm - good point - when I tried to repro, I believe I was debugging with Visual Studio, which means the app likely didn't suspend.

Can you try to run the app with debugger attached (so the app is still "foreground") and see if the flow works correctly? I'd like to make sure the issue is isolated to suspended apps.

rozele avatar Apr 17 '18 02:04 rozele

@rozele I've just figured out that this is the Microsoft react-native-windows repo... I have this problem with react-native, not using windows :/ Anyways, it's the same problem and I've seen that many people has it...

fedealconada avatar Apr 17 '18 11:04 fedealconada

@nomartin please let me know if this has been resolved in RNW 0.55

rozele avatar Jul 23 '18 22:07 rozele