re-organisation login info
This commit is contained in:
152
covas_mobile/lib/classes/auth_service.dart
Normal file
152
covas_mobile/lib/classes/auth_service.dart
Normal file
@@ -0,0 +1,152 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../variable/globals.dart' as globals;
|
||||
import 'dart:io';
|
||||
|
||||
class AuthService {
|
||||
final GoogleSignIn _googleSignIn = GoogleSignIn();
|
||||
|
||||
// Login with username and password
|
||||
Future<bool> login(String username, String password) async {
|
||||
final url = Uri.parse("${globals.api}/token");
|
||||
|
||||
try {
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {
|
||||
'accept': 'application/json',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: {
|
||||
"username": username,
|
||||
"password": password,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final cookies = response.headers["set-cookie"]?.split(";") ?? [];
|
||||
|
||||
for (final cookie in cookies) {
|
||||
final cookieParts = cookie.split(",");
|
||||
for (final part in cookieParts) {
|
||||
final keyValue = part.split("=");
|
||||
if (keyValue.length == 2 && keyValue[0] == "access_token") {
|
||||
prefs.setString("access_token", keyValue[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
print("Login error: $e");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Logout
|
||||
Future<void> logout() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove("access_token");
|
||||
await _googleSignIn.signOut();
|
||||
}
|
||||
|
||||
Future<bool> isLoggedIn() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final accessToken = prefs.getString("access_token");
|
||||
|
||||
if (accessToken == null || accessToken.isEmpty) {
|
||||
print("No access token found.");
|
||||
return false;
|
||||
}
|
||||
|
||||
print("Checking token validity...");
|
||||
var url = Uri.parse("${globals.api}/token");
|
||||
|
||||
try {
|
||||
final response = await http.get(
|
||||
url,
|
||||
headers: {
|
||||
HttpHeaders.cookieHeader: "access_token=$accessToken",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print("Token is valid.");
|
||||
return true;
|
||||
} else {
|
||||
print("Token is invalid. Status code: ${response.statusCode}");
|
||||
await prefs.remove("access_token"); // Clear invalid token
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
print("Error while checking token: $e");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Get stored access token
|
||||
Future<String?> getAccessToken() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getString("access_token");
|
||||
}
|
||||
|
||||
// Login with Facebook
|
||||
Future<String?> signInWithFacebook() async {
|
||||
try {
|
||||
final LoginResult result = await FacebookAuth.instance.login();
|
||||
if (result.status == LoginStatus.success && result.accessToken != null) {
|
||||
final AccessToken? accessToken = result.accessToken;
|
||||
|
||||
final response = await http.post(
|
||||
Uri.parse("${globals.api}/auth/facebook"),
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: jsonEncode({"token": accessToken!.tokenString}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.setString("access_token", data["access_token"]);
|
||||
return data["access_token"];
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print("Facebook login error: $e");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Login with Google
|
||||
Future<String?> signInWithGoogle() async {
|
||||
try {
|
||||
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
|
||||
if (googleUser == null) return null; // User canceled login
|
||||
|
||||
final GoogleSignInAuthentication googleAuth =
|
||||
await googleUser.authentication;
|
||||
|
||||
final response = await http.post(
|
||||
Uri.parse("${globals.api}/auth/google"),
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: jsonEncode({"token": googleAuth.idToken}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.setString("access_token", data["access_token"]);
|
||||
return data["access_token"];
|
||||
}
|
||||
} catch (e) {
|
||||
print("Google login error: $e");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user