Advertisement
harmonyV

Only logic changes

Mar 18th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.42 KB | None | 0 0
  1. import 'dart:developer';
  2. import 'package:google_sign_in/google_sign_in.dart';
  3. import 'package:supabase_flutter/supabase_flutter.dart';
  4.  
  5. class AuthService {
  6.   final _supabase = Supabase.instance.client;
  7. +  final _redirectUri = 'myapp://login-callback';
  8.  
  9.   // Auth state stream
  10.   Stream<AuthState> get authStateChanges => _supabase.auth.onAuthStateChange;
  11.   Session? get currentSession => _supabase.auth.currentSession;
  12.  
  13.   bool get isAuthenticated => _supabase.auth.currentUser != null;
  14.   User? get currentUser => _supabase.auth.currentUser;
  15.  
  16.   Future<AuthResponse?> signUp({
  17.     required String email,
  18.     required String password,
  19.   }) async {
  20.     try {
  21.       final response = await _supabase.auth.signUp(
  22.         email: email,
  23.         password: password,
  24. -        emailRedirectTo: 'myapp://login-callback',
  25. +        emailRedirectTo: _redirectUri,
  26.       );
  27.       return response;
  28.     } catch (e) {
  29.       log('Error signing up: $e');
  30.       rethrow;
  31.     }
  32.   }
  33.  
  34.   Future<AuthResponse?> signIn({
  35.     required String email,
  36.     required String password,
  37.   }) async {
  38.     try {
  39.       final response = await _supabase.auth.signInWithPassword(
  40.         email: email,
  41.         password: password,
  42.       );
  43.       return response;
  44.     } catch (e) {
  45.       log('Error signing in: $e');
  46.       rethrow;
  47.     }
  48.   }
  49.  
  50.   Future<AuthResponse?> signInWithGoogle() async {
  51.     try {
  52.       await _supabase.auth.signInWithOAuth(
  53.         OAuthProvider.google,
  54. -        redirectTo: 'myapp://login-callback.com',
  55. +        redirectTo: _redirectUri,
  56.       );
  57.       // Since OAuth is handled through a redirect flow, we return null here
  58.       // The auth state change listener will handle the successful sign-in
  59.       return null;
  60.     } catch (e) {
  61.       log('Error signing in with Google: $e');
  62.       rethrow;
  63.     }
  64.   }
  65.  
  66.   Future<void> nativeGoogleSignIn() async {
  67.     /// TODO: update the Web client ID with your own.
  68.     ///
  69.     /// Web Client ID that you registered with Google Cloud.
  70.     const webClientId =
  71.         'wildcard';
  72.  
  73.     /// TODO: update the iOS client ID with your own.
  74.     ///
  75.     /// iOS Client ID that you registered with Google Cloud.
  76.     /// const iosClientId = 'my-ios.apps.googleusercontent.com';
  77.  
  78.     final GoogleSignIn googleSignIn = GoogleSignIn(
  79.       //clientId: iosClientId,
  80.       serverClientId: webClientId,
  81.     );
  82.     final googleUser = await googleSignIn.signIn();
  83.     final googleAuth = await googleUser!.authentication;
  84.     final accessToken = googleAuth.accessToken;
  85.     final idToken = googleAuth.idToken;
  86.  
  87.     if (accessToken == null) {
  88.       throw 'No Access Token found.';
  89.     }
  90.     if (idToken == null) {
  91.       throw 'No ID Token found.';
  92.     }
  93.  
  94.     await _supabase.auth.signInWithIdToken(
  95.       provider: OAuthProvider.google,
  96.       idToken: idToken,
  97.       accessToken: accessToken,
  98.     );
  99.   }
  100.  
  101. +  Future<void> signInWithFacebook() async {
  102. +    await _supabase.auth.signInWithOAuth(
  103. +      OAuthProvider.facebook,
  104. +      redirectTo: _redirectUri,
  105. +    );
  106. +  }
  107. +
  108.   Future<AuthResponse?> signInAnonymously() async {
  109.     try {
  110.       final response = await _supabase.auth.signInAnonymously();
  111.       return response;
  112.     } catch (e) {
  113.       log('Error signing in anonymously: $e');
  114.       rethrow;
  115.     }
  116.   }
  117.  
  118.   Future<void> signOut() async {
  119.     try {
  120.       await _supabase.auth.signOut();
  121.     } catch (e) {
  122.       log('Error signing out: $e');
  123.       rethrow;
  124.     }
  125.   }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement