mirror of
https://github.com/timokz/flutter-vienna-hackathon-25.git
synced 2025-11-08 19:04:20 +01:00
add auth skeleton
This commit is contained in:
parent
cbc788b33e
commit
9219a04614
3 changed files with 103 additions and 5 deletions
30
wien_talks/wien_talks_flutter/lib/helper/auth_service.dart
Normal file
30
wien_talks/wien_talks_flutter/lib/helper/auth_service.dart
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import 'package:google_sign_in/google_sign_in.dart';
|
||||||
|
|
||||||
|
class AuthService {
|
||||||
|
static final _google = GoogleSignIn.instance;
|
||||||
|
|
||||||
|
static AuthService? _instance;
|
||||||
|
|
||||||
|
AuthService._() {
|
||||||
|
_google.initialize();
|
||||||
|
}
|
||||||
|
factory AuthService() {
|
||||||
|
if (_instance != null) return _instance!;
|
||||||
|
_instance = AuthService._();
|
||||||
|
|
||||||
|
return _instance!;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Stream<GoogleSignInAuthenticationEvent?> get onUserChanged =>
|
||||||
|
_google.authenticationEvents;
|
||||||
|
|
||||||
|
static Future<GoogleSignInAccount?> signIn() async {
|
||||||
|
try {
|
||||||
|
return await _google.authenticate();
|
||||||
|
} catch (_) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<void> signOut() => _google.disconnect();
|
||||||
|
}
|
||||||
|
|
@ -1,13 +1,31 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:wien_talks_flutter/create_event_screen.dart';
|
import 'package:wien_talks_flutter/create_event_screen.dart';
|
||||||
import 'package:wien_talks_flutter/home_screen.dart';
|
import 'package:wien_talks_flutter/helper/auth_service.dart';
|
||||||
|
import 'package:wien_talks_flutter/login_page.dart';
|
||||||
|
import 'package:wien_talks_flutter/news_screen.dart';
|
||||||
|
|
||||||
final router = GoRouter(
|
final router = GoRouter(
|
||||||
|
redirect: (context, state) {
|
||||||
|
final loggedIn = AuthService.user != null;
|
||||||
|
final atLogin = state.matchedLocation == '/login';
|
||||||
|
if (!loggedIn && !atLogin) return '/login';
|
||||||
|
if (loggedIn && atLogin) return '/';
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
refreshListenable: AuthChangeNotifier(),
|
||||||
routes: [
|
routes: [
|
||||||
|
GoRoute(path: '/login', builder: (c, s) => const LoginScreen()),
|
||||||
|
GoRoute(path: '/', builder: (c, s) => NewsScreen()),
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/',
|
path: '/create_event',
|
||||||
builder: (context, state) => HomeScreen(),
|
name: 'create_event',
|
||||||
),
|
builder: (c, s) => CreateEventScreen()),
|
||||||
GoRoute(path: '/create_event', name: 'create_event', builder: (context, state) => CreateEventScreen()),
|
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
class AuthChangeNotifier extends ChangeNotifier {
|
||||||
|
AuthChangeNotifier() {
|
||||||
|
AuthService.onUserChanged.listen((_) => notifyListeners());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
50
wien_talks/wien_talks_flutter/lib/login_page.dart
Normal file
50
wien_talks/wien_talks_flutter/lib/login_page.dart
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:google_fonts/google_fonts.dart';
|
||||||
|
import 'package:wien_talks_flutter/helper/auth_service.dart';
|
||||||
|
|
||||||
|
class LoginScreen extends StatelessWidget {
|
||||||
|
const LoginScreen({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
body: Container(
|
||||||
|
decoration: const BoxDecoration(
|
||||||
|
gradient: LinearGradient(
|
||||||
|
colors: [Color(0xff2193b0), Color(0xff6dd5ed)],
|
||||||
|
begin: Alignment.topLeft,
|
||||||
|
end: Alignment.bottomRight,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
alignment: Alignment.center,
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Text('Wien Talks',
|
||||||
|
style: GoogleFonts.poppins(
|
||||||
|
fontSize: 42,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
color: Colors.white)),
|
||||||
|
const SizedBox(height: 60),
|
||||||
|
FilledButton.icon(
|
||||||
|
onPressed: () async => await AuthService.signIn(),
|
||||||
|
style: FilledButton.styleFrom(
|
||||||
|
backgroundColor: Colors.white,
|
||||||
|
foregroundColor: Colors.black87,
|
||||||
|
padding:
|
||||||
|
const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(30)),
|
||||||
|
elevation: 6,
|
||||||
|
),
|
||||||
|
icon: Icon(
|
||||||
|
Icons.lock,
|
||||||
|
),
|
||||||
|
label: const Text('Sign in with Google'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue