aad_oauth icon indicating copy to clipboard operation
aad_oauth copied to clipboard

navigatorKey in aad_oauth

Open vanhcoder opened this issue 1 year ago • 4 comments

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?"

vanhcoder avatar Mar 02 '24 08:03 vanhcoder

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);

Chaloemphisit avatar Mar 04 '24 13:03 Chaloemphisit

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

duvet86 avatar Mar 05 '24 05:03 duvet86

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!!

vanhcoder avatar Mar 06 '24 07:03 vanhcoder

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] avatar May 06 '24 02:05 github-actions[bot]

false

github-actions[bot] avatar Jun 06 '24 02:06 github-actions[bot]