loginWithGithub method

Future<AppUser> loginWithGithub(
  1. String code,
  2. String redirectUri, {
  3. AuthPlatform platform = AuthPlatform.web,
})

Authenticate a user using GitHub OAuth with authorization code

This method exchanges the GitHub authorization code for a user session. It creates a new user or logs in an existing user who registered with GitHub OAuth.

Throws:

  • Exception If GitHub Sign-In is not enabled in project settings
  • Exception If the GitHub authorization code is invalid or expired
  • Exception If email is already registered with password authentication

Example:

// After GitHub OAuth callback
final code = Uri.parse(callbackUrl).queryParameters['code'];
if (code != null) {
  final user = await db.auth.loginWithGithub(
    code,
    'http://localhost:3000/auth/github/callback',
    AuthPlatform.web
  );
  print('Logged in: ${user.email}');
}

Implementation

Future<AppUser> loginWithGithub(
  String code,
  String redirectUri, {
  AuthPlatform platform = AuthPlatform.web,
}) async {
  final response = await _request<Map<String, dynamic>>(
    'POST',
    '/auth-collections/github-verify',
    body: {
      'code': code,
      'redirect_uri': redirectUri,
      'platform': platform.value,
    },
    useDataKey: false,
  );

  _token = response['access_token'];
  await setToken(_token!);

  _user = AppUser.fromJson(response['user']);
  await setUser(_user!);

  // Trigger login callback
  _callbacks.onLogin?.call(_user!, _token!);

  return _user!;
}