You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
3.8 KiB
162 lines
3.8 KiB
1 year ago
|
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<UserModel?> 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<UserModel> 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<UserModel> 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<void> clearToken() async {
|
||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||
|
await prefs.remove('token');
|
||
|
}
|
||
|
|
||
|
FutureOr<bool> 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<UserModel> 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');
|
||
|
}
|
||
|
}
|
||
|
}
|