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

Help! I try to use flutter_web_auth_2 on web app #35

Closed xjeremy60 closed 1 year ago

xjeremy60 commented 1 year ago

Describe the bug

A clear and concise description of what the bug is. Hello,

I want to use flutter_web_auth_2 for web app.

I test my application with visual studio code.

I have this error : flutter: Invalid argument(s): Callback url scheme must start with http://localhost:{port}

Can you help me ?

Thanks in advance

To Reproduce

This is my code :

import 'package:flutter/material.dart';
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';

class HomeController {
  final String pageTitle = 'Jukebox';
  final String clientId =
      'YOUR_CLIENT_ID'; // Remplacez par votre client ID Spotify
  final String redirectUri =
      'http://localhost:8080'; // Remplacez par votre URL de redirection Spotify

  Future<void> handleConnectToSpotify(BuildContext context) async {
    const authorizationEndpoint = 'https://accounts.spotify.com/authorize';
    const responseType = 'token';
    const scope =
        'user-read-private user-read-email'; // Définissez les scopes que vous souhaitez utiliser
    final authUrl =
        '$authorizationEndpoint?response_type=$responseType&client_id=$clientId&redirect_uri=$redirectUri&scope=$scope';

    try {
      final result = await FlutterWebAuth2.authenticate(
        url: authUrl,
        callbackUrlScheme: 'jukebox',
      );

      // Le jeton d'accès est inclus dans l'URL de redirection
      final token = Uri.parse(result).fragment.split('&').first.split('=').last;

      // Utilisez le jeton d'accès pour accéder aux ressources protégées
      // ...
    } catch (e) {
      // Gérez les erreurs d'authentification ici
      print(e);
    }
  }
}

Checklist

I try to set redirectUri = 'http://localhost:{port}'

ThexXTURBOXx commented 1 year ago

You need to set the callbackUrlScheme parameter appropriately here:

final result = await FlutterWebAuth2.authenticate(
        url: authUrl,
        callbackUrlScheme: 'jukebox',
      ); 

See the example: https://github.com/ThexXTURBOXx/flutter_web_auth_2/blob/master/flutter_web_auth_2/example/lib/main.dart#L113

ThexXTURBOXx commented 1 year ago

Closed as invalid