220 lines
6.9 KiB
Dart
220 lines
6.9 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:provider/provider.dart';
|
|
import 'package:pvt15/models/user_model.dart';
|
|
import 'package:pvt15/providers/user_provider.dart';
|
|
import 'navigation_controller_page.dart';
|
|
import 'package:pvt15/pages/add_user_credentials_page.dart';
|
|
import 'package:pvt15/services/auth/auth_service.dart';
|
|
|
|
class GoogleSignInPage extends StatefulWidget {
|
|
const GoogleSignInPage({super.key});
|
|
|
|
@override
|
|
State<GoogleSignInPage> createState() => _GoogleSignInPageState();
|
|
}
|
|
|
|
class _GoogleSignInPageState extends State<GoogleSignInPage> {
|
|
GoogleSignIn signInCredentials = GoogleSignIn(
|
|
scopes: ['email', 'profile', 'openid'],
|
|
serverClientId:
|
|
"63395519979-robk2jog8rd89mi68466259r5arqufm1.apps.googleusercontent.com",
|
|
hostedDomain: '',
|
|
);
|
|
final AuthService authService = AuthService();
|
|
|
|
Future<bool> doesUserExist(String? googleID) async {
|
|
if (googleID == null || googleID.isEmpty) {
|
|
return false;
|
|
}
|
|
final url = Uri.parse(
|
|
'https://group-1-15.pvt.dsv.su.se/api/users/$googleID',
|
|
);
|
|
|
|
try {
|
|
final response = await http.get(url);
|
|
|
|
if (response.statusCode == 200) {
|
|
final jsonData = jsonDecode(response.body);
|
|
|
|
return true;
|
|
} else if (response.statusCode == 404) {
|
|
return false;
|
|
} else {
|
|
print(
|
|
'Fel vid anrop till backend: ${response.statusCode} Body: ${response.body}',
|
|
);
|
|
throw Exception(
|
|
'Fel vid anrop till backend: ${response.statusCode} Body: ${response.body}',
|
|
);
|
|
}
|
|
} catch (e) {
|
|
print('Ett fel inträffade vid anrop till backend: $e');
|
|
throw Exception('Ett fel inträffade vid anrop till backend: $e');
|
|
}
|
|
}
|
|
|
|
void googleSignIn() async {
|
|
try {
|
|
final GoogleSignInAccount? googleUser = await signInCredentials.signIn();
|
|
|
|
if (googleUser != null) {
|
|
final GoogleSignInAuthentication googleAuth =
|
|
await googleUser.authentication;
|
|
|
|
final String? idToken = googleAuth.idToken;
|
|
|
|
if (idToken != null) {
|
|
final response = await http.post(
|
|
Uri.parse('https://group-1-15.pvt.dsv.su.se/api/auth/google/login'),
|
|
headers: <String, String>{
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
},
|
|
body: jsonEncode(<String, String>{'idToken': idToken}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final responseData = jsonDecode(response.body);
|
|
final String applicationAccessToken = responseData['accessToken'];
|
|
final String applicationRefreshToken = responseData['refreshToken'];
|
|
final String? username = responseData['username'];
|
|
|
|
await authService.saveTokens(
|
|
applicationAccessToken,
|
|
applicationRefreshToken,
|
|
);
|
|
|
|
if (username == null) {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => AddUserCredentialPage(user: googleUser),
|
|
),
|
|
);
|
|
} else {
|
|
final userProvider = Provider.of<UserProvider>(
|
|
context,
|
|
listen: false,
|
|
);
|
|
final String profilePicUrl = googleUser.photoUrl ?? '';
|
|
userProvider.setUser(
|
|
AppUser(
|
|
googleID: googleUser.id,
|
|
name: '${googleUser.displayName}',
|
|
username: username,
|
|
email: googleUser.email,
|
|
profilePicUrl: profilePicUrl,
|
|
),
|
|
);
|
|
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder:
|
|
(context) => NavigationControllerPage(user: googleUser),
|
|
settings: RouteSettings(name: '/gameCategory'),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print('Fel vid Google Sign-In: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Container(
|
|
decoration: const BoxDecoration(
|
|
gradient: LinearGradient(
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
colors: [Color(0xFF7A28A5), Color(0xFFF9377B)],
|
|
),
|
|
),
|
|
child: Center(
|
|
// Wrappa Column med Center
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
'Hop',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
color: const Color(0xFFFF5394),
|
|
fontSize: 60,
|
|
fontFamily: 'Poppins',
|
|
fontWeight: FontWeight.w700,
|
|
height: 0.27,
|
|
letterSpacing: 0.50,
|
|
shadows: const [
|
|
Shadow(
|
|
offset: Offset(0, 6),
|
|
blurRadius: 8,
|
|
color: Color(0x3F000000),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 0.50),
|
|
Text(
|
|
'Spot',
|
|
style: TextStyle(
|
|
color: const Color(0xFFFFC83B),
|
|
fontSize: 60,
|
|
fontFamily: 'Poppins',
|
|
fontWeight: FontWeight.w700,
|
|
height: 0.27,
|
|
letterSpacing: 0.50,
|
|
shadows: const [
|
|
Shadow(
|
|
offset: Offset(0, 6),
|
|
blurRadius: 8,
|
|
color: Color(0x3F000000),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 150),
|
|
Image(
|
|
image: AssetImage("assets/images/KMLogga.png"),
|
|
height: 180,
|
|
),
|
|
const SizedBox(height: 60),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
googleSignIn();
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
padding: EdgeInsets.zero,
|
|
side: BorderSide.none,
|
|
),
|
|
child: SizedBox(
|
|
width: 302,
|
|
height: 70,
|
|
child: Image.asset(
|
|
"assets/images/signInGoogleButton.png",
|
|
fit: BoxFit.cover,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|