Earlybyte / aad_oauth

Flutter Azure Active Directory OAuth Package
https://pub.dev/packages/aad_oauth
MIT License
92 stars 186 forks source link

navigatorKey in aad_oauth #307

Closed vanhcoder closed 4 months ago

vanhcoder commented 7 months ago

In 'aad_oauth' library, it's necessary to pass the navigatorKey to MaterialApp(), but in recent versions, when combined with route libraries like auto_router, go_router, it's required to use MaterialApp.router, and MaterialApp.router does not support the navigatorKey parameter. So what's the solution here?"

Chaloemphisit commented 7 months ago

Hi @vanhcoder

Basically, go_router provides the ability to add the navigation key.

final _rootNavigatorKey = GlobalKey<NavigatorState>();

class AppRouter {
  final goRouter = GoRouter(
    navigatorKey: _rootNavigatorKey,
    routes: [],
  );
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      debugShowCheckedModeBanner: false,
      theme: DemoThemes.lightTheme,
      routerConfig: AppRouter().goRouter
    );
  }
}

then you can use _rootNavigatorKey on the add_oauth as the same that you add on the go_router

  // ... 

  static final Config config = new Config(
    tenant: "YOUR_TENANT_ID",
    clientId: "YOUR_CLIENT_ID",
    scope: "openid profile offline_access",
    // redirectUri is Optional as a default is calculated based on app type/web location
    redirectUri: "your redirect url available in azure portal",
    navigatorKey: _rootNavigatorKey,
    webUseRedirect: true, // default is false - on web only, forces a redirect flow instead of popup auth
    //Optional parameter: Centered CircularProgressIndicator while rendering web page in WebView
    loader: Center(child: CircularProgressIndicator()),
    postLogoutRedirectUri: 'http://your_base_url/logout', //optional
  );

  final AadOAuth oauth = new AadOAuth(config);
duvet86 commented 7 months ago

It's not working in a go_router redirect parameter: image

vanhcoder commented 7 months ago

Hi @vanhcoder

Basically, go_router provides the ability to add the navigation key.

final _rootNavigatorKey = GlobalKey<NavigatorState>();

class AppRouter {
  final goRouter = GoRouter(
    navigatorKey: _rootNavigatorKey,
    routes: [],
  );
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      debugShowCheckedModeBanner: false,
      theme: DemoThemes.lightTheme,
      routerConfig: AppRouter().goRouter
    );
  }
}

then you can use _rootNavigatorKey on the add_oauth as the same that you add on the go_router

  // ... 

  static final Config config = new Config(
    tenant: "YOUR_TENANT_ID",
    clientId: "YOUR_CLIENT_ID",
    scope: "openid profile offline_access",
    // redirectUri is Optional as a default is calculated based on app type/web location
    redirectUri: "your redirect url available in azure portal",
    navigatorKey: _rootNavigatorKey,
    webUseRedirect: true, // default is false - on web only, forces a redirect flow instead of popup auth
    //Optional parameter: Centered CircularProgressIndicator while rendering web page in WebView
    loader: Center(child: CircularProgressIndicator()),
    postLogoutRedirectUri: 'http://your_base_url/logout', //optional
  );

  final AadOAuth oauth = new AadOAuth(config);

Thank bro, it work!!

github-actions[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you still think this issue is needed, reopen it again.

github-actions[bot] commented 4 months ago

false