Include pop ups + change auth flow for github codespace

This commit is contained in:
hunteraraujo
2023-10-05 23:50:21 -07:00
parent f810264c65
commit bc10845cb5
2 changed files with 17 additions and 42 deletions

View File

@@ -84,7 +84,10 @@ class MyApp extends StatelessWidget {
if (snapshot.connectionState == ConnectionState.waiting) { if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator(); return CircularProgressIndicator();
} }
if (snapshot.hasData && snapshot.data != null) { String hostname = Uri.base.host;
if (snapshot.hasData && snapshot.data != null ||
hostname.contains('github.dev')) {
return MultiProvider( return MultiProvider(
providers: [ providers: [
ChangeNotifierProvider( ChangeNotifierProvider(

View File

@@ -7,31 +7,21 @@ class AuthService {
clientId: clientId:
"387936576242-iejdacrjljds7hf99q0p6eqna8rju3sb.apps.googleusercontent.com"); "387936576242-iejdacrjljds7hf99q0p6eqna8rju3sb.apps.googleusercontent.com");
// Sign in with Google using redirect
// Sign in with Google using redirect // Sign in with Google using redirect
Future<UserCredential?> signInWithGoogle() async { Future<UserCredential?> signInWithGoogle() async {
try { try {
final GoogleAuthProvider googleProvider = GoogleAuthProvider(); final GoogleSignInAccount? googleSignInAccount =
await googleSignIn.signIn();
// Step 1: Detect the current hostname if (googleSignInAccount != null) {
String hostname = Uri.base.host; final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
// Step 2: Determine the redirect URI final AuthCredential credential = GoogleAuthProvider.credential(
String redirectUri; accessToken: googleSignInAuthentication.accessToken,
if (hostname.contains('github.dev')) { idToken: googleSignInAuthentication.idToken,
// If running in Github Codespaces );
redirectUri = Uri.base.toString(); return await _auth.signInWithCredential(credential);
} else {
// For local development or other environments, set accordingly
redirectUri = 'http://localhost:8000'; // Example for local development
} }
// Step 3: Update OAuth 2.0 provider configuration dynamically
googleProvider.setCustomParameters({'redirect_uri': redirectUri});
await _auth.signInWithRedirect(googleProvider);
final result = await _auth.getRedirectResult();
print(result);
return result;
} catch (e) { } catch (e) {
print("Error during Google Sign-In: $e"); print("Error during Google Sign-In: $e");
return null; return null;
@@ -41,26 +31,8 @@ class AuthService {
// Sign in with GitHub using redirect // Sign in with GitHub using redirect
Future<UserCredential?> signInWithGitHub() async { Future<UserCredential?> signInWithGitHub() async {
try { try {
final GithubAuthProvider githubProvider = GithubAuthProvider(); final GithubAuthProvider provider = GithubAuthProvider();
return await _auth.signInWithPopup(provider);
// Step 1: Detect the current hostname
String hostname = Uri.base.host;
// Step 2: Determine the redirect URI
String redirectUri;
if (hostname.contains('github.dev')) {
// If running in Github Codespaces
redirectUri = Uri.base.toString();
} else {
// For local development or other environments, set accordingly
redirectUri = 'http://localhost:8000'; // Example for local development
}
// Step 3: Update OAuth 2.0 provider configuration dynamically
githubProvider.setCustomParameters({'redirect_uri': redirectUri});
await _auth.signInWithRedirect(githubProvider);
return await _auth.getRedirectResult();
} catch (e) { } catch (e) {
print("Error during GitHub Sign-In: $e"); print("Error during GitHub Sign-In: $e");
return null; return null;