import 'dart:convert'; import 'dart:async'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; import '../connection/connection.dart'; import '../models/user_model.dart'; class AuthService { Future getUserInfoFromServer(String token) async { var url = Uri.parse('$baseUrl/user'); var headers = { 'Content-Type': 'application/json', 'Authorization': token, }; var response = await http.get(url, headers: headers); if (response.statusCode == 200) { var data = json.decode(response.body); UserModel user = UserModel.fromJson(data); return user; } else { return null; } } FutureOr register({ String? name, String? email, String? password, }) async { var url = Uri.parse('$baseUrl/register'); var headers = {'Content-Type': 'application/json'}; var body = jsonEncode({ 'name': name, 'email': email, 'password': password, }); var response = await http.post( url, headers: headers, body: body, ); if (response.statusCode == 200) { var data = jsonDecode(response.body)['data']; UserModel user = UserModel.fromJson(data['user']); user.token = 'Bearer ' + data['token']; return user; } else { throw Exception('Failed to register'); } } Future login({ String? email, String? password, }) async { var url = Uri.parse('$baseUrl/login'); var headers = {'Content-Type': 'application/json'}; var body = jsonEncode({ 'email': email, 'password': password, }); var response = await http.post( url, headers: headers, body: body, ); print(response.body); var dataUser = print(response.body); if (response.statusCode == 200) { var data = json.decode(response.body)['data']; UserModel user = UserModel.fromJson(data['user']); user.token = 'Bearer ' + data['token']; SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setString('token', user.token!); return user; } else { throw Exception('Gagal Login'); } } Future clearToken() async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.remove('token'); } FutureOr logout(String token) async { try { var url = Uri.parse('$baseUrl/logout'); var headers = { 'Content-Type': 'application/json', 'Authorization': token, }; var response = await http.post( url, headers: headers, ); if (response.statusCode == 200) { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.remove('token'); await prefs.remove('email'); await prefs.remove('password'); await prefs.clear(); return true; } else { return false; } } catch (e) { print(e); return false; } } FutureOr updateProfile({ String? name, String? username, String? token, }) async { try { var url = Uri.parse('$baseUrl/user'); var headers = { 'Content-Type': 'application/json', 'Authorization': token!, }; var body = jsonEncode({ 'name': name, 'username': username, }); var response = await http.post( url, headers: headers, body: body, ); if (response.statusCode == 200) { var data = jsonDecode(response.body)['data']; UserModel user = UserModel.fromJson(data); return user; } else { throw Exception('Failed to update profile'); } } catch (e) { print(e); throw Exception('Failed to update profile'); } } }