Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:developer';
- import 'package:google_sign_in/google_sign_in.dart';
- import 'package:supabase_flutter/supabase_flutter.dart';
- class AuthService {
- final _supabase = Supabase.instance.client;
- + final _redirectUri = 'myapp://login-callback';
- // Auth state stream
- Stream<AuthState> get authStateChanges => _supabase.auth.onAuthStateChange;
- Session? get currentSession => _supabase.auth.currentSession;
- bool get isAuthenticated => _supabase.auth.currentUser != null;
- User? get currentUser => _supabase.auth.currentUser;
- Future<AuthResponse?> signUp({
- required String email,
- required String password,
- }) async {
- try {
- final response = await _supabase.auth.signUp(
- email: email,
- password: password,
- - emailRedirectTo: 'myapp://login-callback',
- + emailRedirectTo: _redirectUri,
- );
- return response;
- } catch (e) {
- log('Error signing up: $e');
- rethrow;
- }
- }
- Future<AuthResponse?> signIn({
- required String email,
- required String password,
- }) async {
- try {
- final response = await _supabase.auth.signInWithPassword(
- email: email,
- password: password,
- );
- return response;
- } catch (e) {
- log('Error signing in: $e');
- rethrow;
- }
- }
- Future<AuthResponse?> signInWithGoogle() async {
- try {
- await _supabase.auth.signInWithOAuth(
- OAuthProvider.google,
- - redirectTo: 'myapp://login-callback.com',
- + redirectTo: _redirectUri,
- );
- // Since OAuth is handled through a redirect flow, we return null here
- // The auth state change listener will handle the successful sign-in
- return null;
- } catch (e) {
- log('Error signing in with Google: $e');
- rethrow;
- }
- }
- Future<void> nativeGoogleSignIn() async {
- /// TODO: update the Web client ID with your own.
- ///
- /// Web Client ID that you registered with Google Cloud.
- const webClientId =
- 'wildcard';
- /// TODO: update the iOS client ID with your own.
- ///
- /// iOS Client ID that you registered with Google Cloud.
- /// const iosClientId = 'my-ios.apps.googleusercontent.com';
- final GoogleSignIn googleSignIn = GoogleSignIn(
- //clientId: iosClientId,
- serverClientId: webClientId,
- );
- final googleUser = await googleSignIn.signIn();
- final googleAuth = await googleUser!.authentication;
- final accessToken = googleAuth.accessToken;
- final idToken = googleAuth.idToken;
- if (accessToken == null) {
- throw 'No Access Token found.';
- }
- if (idToken == null) {
- throw 'No ID Token found.';
- }
- await _supabase.auth.signInWithIdToken(
- provider: OAuthProvider.google,
- idToken: idToken,
- accessToken: accessToken,
- );
- }
- + Future<void> signInWithFacebook() async {
- + await _supabase.auth.signInWithOAuth(
- + OAuthProvider.facebook,
- + redirectTo: _redirectUri,
- + );
- + }
- +
- Future<AuthResponse?> signInAnonymously() async {
- try {
- final response = await _supabase.auth.signInAnonymously();
- return response;
- } catch (e) {
- log('Error signing in anonymously: $e');
- rethrow;
- }
- }
- Future<void> signOut() async {
- try {
- await _supabase.auth.signOut();
- } catch (e) {
- log('Error signing out: $e');
- rethrow;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement