ThexXTURBOXx / flutter_web_auth_2

Flutter plugin for authenticating a user with a web service
https://pub.dev/packages/flutter_web_auth_2
MIT License
53 stars 54 forks source link

Redirect url after login in Flutter web does not work #99

Closed volkanustekidag closed 6 months ago

volkanustekidag commented 8 months ago

Description

I'm trying to provide login using package in a web application. I open the link I created and log in, but the tab is closed and I cannot receive the code I expect. I couldn't understand what I was missing.

Minimal Reproduction

Steps to reproduce the behaviour:

  1. Use the following code:

    final authorizationUrl = Uri.https(
        ServicesConstants.authorizationUrlProd, '/connect/authorize', {
      'response_type': 'code',
      'client_id': newClientId,
      'redirect_uri': "worksy://auth",
      'grant_type': 'authorization_code',
      'code_challenge': codeChallenge,
      'code_challenge_method': 'S256',
      'scope': 'openid profile offline_access email',
      'state': _generateRandomString(16),
      'nonce': _generateRandomString(16),
      'prompt': 'login'
    });
    
      final result = await FlutterWebAuth2.authenticate(
        url: authorizationUrl.toString(),
        callbackUrlScheme: "worksy",
      );
    
      final code = Uri.parse(result).queryParameters['code'];
    

<!DOCTYPE html>

Authentication complete

Authentication is complete. If this does not happen automatically, please close the window.



### Exception or Error

It does not redirect after login and the expected code is not returned

### Expected Behaviour

Redirect after login and continue from the first tab

### Screenshots

![a](https://github.com/ThexXTURBOXx/flutter_web_auth_2/assets/56761000/76fa77e9-2382-4c08-94ef-133c52a090b6)

### Additional context

_No response_

### Device

Mac

### OS

 MacOS

### Browser

Chrome

### Flutter version

Channel stable, 3.13.5,

### flutter_web_auth_2 version

flutter_web_auth_2 3.1.1

### Checklist

- [X] I have read and followed the **entire** [troubleshooting guide](https://github.com/ThexXTURBOXx/flutter_web_auth_2#troubleshooting) and it has not provided the solution I need.
- [X] I have provided all the information I can.
ThexXTURBOXx commented 8 months ago

You need to follow exactly the steps in https://github.com/ThexXTURBOXx/flutter_web_auth_2?tab=readme-ov-file#web If you are building a Flutter Web application, you should not redirect to some URL scheme, but your redirect URL needs to point to the auth.html file that is provided in the setup guide.

stan-at-work commented 6 months ago

You need to follow exactly the steps in https://github.com/ThexXTURBOXx/flutter_web_auth_2?tab=readme-ov-file#web If you are building a Flutter Web application, you should not redirect to some URL scheme, but your redirect URL needs to point to the auth.html file that is provided in the setup guide. @ThexXTURBOXx

Why is http://localhost:5200/auth.html not a valid callbackUrlScheme ?

It get's blocked by the _assertCallbackScheme function.

The regex: RegExp(r'^[a-z][a-z\d+.-]*$') does not allow this: http://localhost:5200/auth.html.

Can you give an example of a valid callbackUrlScheme on WEB ?

If u use Microsoft you can't add a callbackUrlScheme in the settings without providing the prefix: http://...

ThexXTURBOXx commented 6 months ago

@stan-at-work Because you are supposed to provide a callback URL scheme, not a full URL. Furthermore, as seen in this line, on Web the callback URL scheme gets ignored anyway, so just provide anything (in your case, just supply http). In general, this is a case of RTFM.

Your comment does also seem to be not related to this issue - so for the future, open an own issue for that. I will also close this issue here as stale.

stan-at-work commented 6 months ago

@stan-at-work Because you are supposed to provide a callback URL scheme, not a full URL. Furthermore, as seen in this line, on Web the callback URL scheme gets ignored anyway, so just provide anything (in your case, just supply http). In general, this is a case of RTFM.

Your comment does also seem to be not related to this issue - so for the future, open an own issue for that. I will also close this issue here as stale.

Thanks, that works. You should really consider adding this to the documentation:

Web: callbackscheme = 'http'

This would help ALLOT.

Thanks for the quick response :-)