import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:rogapp/model/auth_user.dart'; import 'package:get/get.dart'; import 'package:flutter/material.dart'; import '../utils/const.dart'; class AuthService { Future userLogin(String email, String password) async { final serverUrl = ConstValues.currentServer(); final url = '$serverUrl/api/login/'; try { final http.Response response = await http.post( Uri.parse(url), headers: { 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode({'email': email, 'password': password}), ); switch (response.statusCode) { case 200: final data = json.decode(utf8.decode(response.bodyBytes)); AuthUser user = AuthUser.fromMap(data["user"]); final String token = data["token"]; user.auth_token = token; return user; default: return null; //throw Exception(response.reasonPhrase); } } on Exception catch (_) { rethrow; } } Future userFromToken(String token) async { final serverUrl = ConstValues.currentServer(); final url = '$serverUrl/api/user/'; try { final http.Response response = await http.get(Uri.parse(url), headers: { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': 'Token $token' }); switch (response.statusCode) { case 200: final data = json.decode(utf8.decode(response.bodyBytes)); AuthUser user = AuthUser.fromMap(data); user.auth_token = token; return user; default: return null; //throw Exception(response.reasonPhrase); } } on Exception catch (_) { rethrow; } } static Future> changePassword( String oldpassword, String newpassword, String token) async { Map changePassword = {}; String serverUrl = ConstValues.currentServer(); String url = '$serverUrl/api/change-password/'; //print('++++++++$url'); final http.Response response = await http.put( Uri.parse(url), headers: { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': 'Token $token' }, body: jsonEncode({ 'old_password': oldpassword, 'new_password': newpassword }), ); if (response.statusCode == 200) { changePassword = json.decode(utf8.decode(response.bodyBytes)); } return changePassword; } static Future> login( String email, String password) async { //print("------- in logged email $email pwd $password ###### --------"); Map cats = {}; String serverUrl = ConstValues.currentServer(); String url = '$serverUrl/api/login/'; //print('++++++++$url'); //String url = 'http://localhost:8100/api/login/'; try { final http.Response response = await http.post( Uri.parse(url), headers: { 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode( {'email': email, 'password': password}), ); if (response.statusCode == 200) { cats = json.decode(utf8.decode(response.bodyBytes)); } else { print('Login failed with status code: ${response.statusCode}'); cats = {}; } } catch( e ){ print('Error in login: $e'); Get.snackbar("通信エラーがおきました", "サーバーと通信できませんでした", backgroundColor: Colors.red, colorText: Colors.white); Get.snackbar( "通信エラーがおきました", "サーバーと通信できませんでした", backgroundColor: Colors.red, colorText: Colors.white, icon: const Icon( Icons.assistant_photo_outlined, size: 40.0, color: Colors.blue), snackPosition: SnackPosition.TOP, duration: const Duration(seconds: 3), //backgroundColor: Colors.yellow, ); cats = {}; } return cats; } static Future> register( String email, String password) async { Map cats = {}; String serverUrl = ConstValues.currentServer(); String url = '$serverUrl/api/register/'; //print('++++++++$url'); final http.Response response = await http.post( Uri.parse(url), headers: { 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode({'email': email, 'password': password}), ); //print(response.body); if (response.statusCode == 200) { cats = json.decode(utf8.decode(response.bodyBytes)); } return cats; } static Future> deleteUser(String token) async { Map cats = {}; String serverUrl = ConstValues.currentServer(); String url = '$serverUrl/api/delete-account/'; //print('++++++++$url'); final http.Response response = await http.get(Uri.parse(url), headers: { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': 'Token $token' }); if (response.statusCode == 200) { cats = json.decode(utf8.decode(response.bodyBytes)); } return cats; } static Future?> userDetails(int userid) async { List cats = []; String serverUrl = ConstValues.currentServer(); String url = '$serverUrl/api/userdetials?user_id=$userid'; //print('++++++++$url'); //print("---- UserDetails url is $url"); final response = await http.get( Uri.parse(url), headers: { 'Content-Type': 'application/json; charset=UTF-8', }, ); if (response.statusCode == 200) { cats = json.decode(utf8.decode(response.bodyBytes)); } return cats; } static Future?> userForToken(String token) async { Map cats = {}; String serverUrl = ConstValues.currentServer(); String url = '$serverUrl/api/user/'; //print('++++++++$url'); //print("---- UserDetails url is $url"); final response = await http.get( Uri.parse(url), headers: { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': 'Token $token' }, ); if (response.statusCode == 200) { cats = json.decode(utf8.decode(response.bodyBytes)); //print("--- eeeeee $cats ----"); } return [ {"user": cats, "token": token} ]; } }