LinusU / flutter_web_auth

Flutter plugin for authenticating a user with a web service
MIT License
196 stars 169 forks source link

It doesn't ask me for the login #143

Closed Nedimko123 closed 1 year ago

Nedimko123 commented 1 year ago

Hello, I would appreciate your help greatly, thank you... This is my code: I commented out everything after FlutterWeb Auth.authenticate, because it just awaits it for me to finish.

We used this as an api https://goldfish-app-bd5g8.ondigitalocean.app/api#, and this is how I would login via google to our rest api: https://goldfish-app-bd5g8.ondigitalocean.app/auth/google .. but in the app it only returns accessToken, and it doesnt ask me to login or anything else..

            final googleClientId =
                            '18236502214-4q4379e1jt0ah5lui1r2fuh03rpbqtg9.apps.googleusercontent.com';
                        final callbackUrlScheme =
                            'goldfish-app-bd5g8.ondigitalocean.app';

// Construct the url
                        final url = Uri.parse(
                            "https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?response_type=code&redirect_uri=https%3A%2F%2Fgoldfish-app-bd5g8.ondigitalocean.app%2Fauth%2Fgoogle%2Fredirect&scope=email%20profile&client_id=18236502214-4q4379e1jt0ah5lui1r2fuh03rpbqtg9.apps.googleusercontent.com&service=lso&flowName=GeneralOAuthFlow");

// Present the dialog to the user
               final result  = await    FlutterWebAuth.authenticate(
                          url: url.toString(),
                          callbackUrlScheme: callbackUrlScheme,
                        );

// Extract code from resulting url
//                         final code = Uri.parse(result).queryParameters['code'];

// // Use this code to get an access token
//                         final response = await http.post(
//                             Uri.parse(
//                                 "https://www.googleapis.com/oauth2/v4/token"),
//                             body: {
//                               'client_id': googleClientId,
//                               'redirect_uri': '$callbackUrlScheme:/',
//                               'grant_type': 'authorization_code',
//                               'code': code,
//                             });

// Get the access token from the response
                        // final accessToken =
                        //     jsonDecode(response.body)['accessToken'] as String;
                        // print(accessToken);
LinusU commented 1 year ago

but in the app it only returns accessToken, and it doesnt ask me to login or anything else..

This is probably because you are already signed in. There might be a flag that you can pass to your OAuth provider to control what happens if a user is already logged in.

Unfortunately I cannot help you with that since it's outside of this library...

Nedimko123 commented 1 year ago

Okay, so the problem with it never asking me for login was due to the reason that I only had 1 account linked with google chrome on the phone, when I added another one, it prompted me.

But I still can't fix the error when it does return me json accessToken, the app just awaits for the login process to complete, maybe my url scheme is not right or something. When the login completes it redirects me here: https://goldfish-app-bd5g8.ondigitalocean.app/auth/google/redirect?code=4%2F0ARtbsJpAS0HI_FduXCxKvTlUyWUE1C86THxCpLeFfM700kGa35RfbbMAxO9O9PcVXTMCsg&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+openid&authuser=0&prompt=none The code is still the same:


                        const callbackUrlScheme =
                            'goldfish-app-bd5g8.ondigitalocean.app';

// Construct the url
                        final url = Uri.parse(
                            "https://goldfish-app-bd5g8.ondigitalocean.app/auth/google");

// Present the dialog to the user

                        try {
                          final result = await FlutterWebAuth.authenticate(
                              url: url.toString(),
                              preferEphemeral: false,
                              callbackUrlScheme: callbackUrlScheme);
                          print(result);
                        } catch (e) {
                          print(e);
                        }

Here is a 20 second video of what happens in the app:

LinusU commented 1 year ago

Your callback url scheme is goldfish-app-bd5g8.ondigitalocean.app, so you need to redirect to goldfish-app-bd5g8.ondigitalocean.app://...-something, not https://goldfish-app-bd5g8.ondigitalocean.app/...

Please read the troubleshooting guide for more info 👍