Fakultas Ilmu Komputer UI

Commit 91f5ad5d authored by Usman Sidiq's avatar Usman Sidiq
Browse files

[GREEN] Done registrasi and login

parent b85dbf60
Pipeline #42248 failed with stages
in 4 minutes and 17 seconds
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"google_maps_flutter","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"location","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos"]},{"name":"path_provider_macos","dependencies":[]},{"name":"sqflite","dependencies":[]}]}
\ No newline at end of file
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"google_maps_flutter","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"location","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos"]},{"name":"path_provider_macos","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_macos","shared_preferences_web"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]},{"name":"sqflite","dependencies":[]}]}
\ No newline at end of file
import 'package:flutter/material.dart';
import 'package:ppl_disabilitas/config/styles.dart';
import 'package:ppl_disabilitas/page/login/login.dart';
import 'package:shared_preferences/shared_preferences.dart';
class BisaGoDrawer extends StatelessWidget {
final List<Map<String, dynamic>> drawerList = [
......@@ -57,6 +58,7 @@ class BisaGoDrawer extends StatelessWidget {
menus.forEach((menu) {
drawerItem.add(menu);
});
return Theme(
data: Theme.of(context).copyWith(
canvasColor: greenPrimary,
......@@ -85,7 +87,8 @@ class BisaGoDrawer extends StatelessWidget {
),
onTap: () {
if(title == 'Login') {
_navigateToLoginPage(context);
checkLoginStatus(context);
title = 'Logout';
}
},
),
......@@ -98,6 +101,16 @@ class BisaGoDrawer extends StatelessWidget {
);
}
checkLoginStatus(BuildContext context) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
if(sharedPreferences.getString("token") == null) {
_navigateToLoginPage(context);
}
else {
await sharedPreferences.clear();
await Navigator.pushNamed(context, '/');
}
}
void _navigateToLoginPage(BuildContext context) {
final route = MaterialPageRoute(builder: (_) => Login());
Navigator.of(context).push(route);
......
import 'dart:convert';
class User {
bool is_login;
String username;
String name;
String phone_number;
String email;
String password;
String phone_number;
String token;
User({this.name, this.phone_number, this.email, this.password});
User({this.is_login, this.username, this.name, this.email, this.phone_number, this.token});
factory User.fromJson(Map<String, dynamic> map) {
return User(
name: map["name"], phone_number: map["phone_number"], email: map["email"], password: map["password"]);
username: map["username"],
name: map["name"],
email: map["email"],
phone_number: map["phone_number"],);
}
Map<String, dynamic> toJson() {
return {"name": name, "phone_number": phone_number, "email": email, "password": password};
return {"username": username, "name": name, "email": email, "phone_number": phone_number};
}
@override
String toString() {
return 'User{"name": $name, "phone_number": $phone_number, "email": $email, "password": $password}';
return 'User{"username": $username, "name": $name, "email": $email, "phone_number": $phone_number}';
}
}
......
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ppl_disabilitas/config/styles.dart';
import 'package:ppl_disabilitas/page/registrasi/registrasi.dart';
import 'package:ppl_disabilitas/utils/customButton.dart';
import 'package:ppl_disabilitas/utils/customTextField.dart';
import 'package:ppl_disabilitas/utils/validator.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
class Login extends StatefulWidget{
......@@ -14,6 +18,7 @@ class Login extends StatefulWidget{
class LoginState extends State<Login> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
SharedPreferences sharedPreferences;
Widget build(BuildContext context) {
return Scaffold(
......@@ -45,7 +50,7 @@ class LoginState extends State<Login> {
style: TextStyle(fontSize: 15),
),
onTap: () {
Navigator.pushNamed(context, '/register');
_navigateToRegistrasiPage(context);
},
),
)
......@@ -124,17 +129,27 @@ class LoginState extends State<Login> {
login(String email, String password) async {
Map data = {
'email' : email,
'username' : email,
'password' : password
};
// var jsonData;
var response = await http.post('https://10.0.2.2:8000', body: data);
sharedPreferences = await SharedPreferences.getInstance();
var response = await http.post('http://poipole-staging.herokuapp.com/api-token-auth/', body: data);
if(response.statusCode == 200) {
// jsonData = json.decode(response.body);
await Navigator.pushNamed(context, '/');
Map<String, dynamic> tokenMap = jsonDecode(response.body);
setState(() {
sharedPreferences.setString("token", tokenMap['token']);
sharedPreferences.setString("email", email);
Navigator.pushNamed(context, '/');
});
}
else {
print(response.body);
}
}
void _navigateToRegistrasiPage(BuildContext context) {
final route = MaterialPageRoute(builder: (_) => Registrasi());
Navigator.of(context).push(route);
}
}
\ No newline at end of file
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ppl_disabilitas/config/styles.dart';
import 'package:ppl_disabilitas/page/dashboard/dashboard.dart';
import 'package:ppl_disabilitas/utils/customButton.dart';
import 'package:ppl_disabilitas/utils/customTextField.dart';
//import 'package:ppl_disabilitas/network/network_interface.dart';
import 'package:ppl_disabilitas/utils/validator.dart';
import 'package:http/http.dart' as http;
......@@ -138,6 +138,7 @@ class RegistrasiState extends State<Registrasi> {
form.save();
await createUser(nameController.text.toString(), phoneController.text.toString(),
emailController.text.toString(), passwordController.text.toString());
_showDialog();
}
}
......@@ -159,29 +160,32 @@ class RegistrasiState extends State<Registrasi> {
body: data,
);
if (response.statusCode == 200) {
// await Navigator.pushNamed(context, '/');
return true;
} else {
return false;
}
}
// Future<bool> register(User data) async {
// final response = await http.post(
// 'https://10.0.2.2:8000/api/registrasi/',
// body: data);
// if (response.statusCode == 201) {
// print(response.body);
// return true;
// }
// else {
// return false;
// }
// }
// var response = NetworkInterface().post(
// url: 'https://my.api.mockaroo.com/register.json?key=095ee610',
// bodyParams: data,
// isLogin: false
// );
void _showDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Pendaftaran Berhasil"),
content: Text("Silahkan kembali ke halaman utama untuk login"),
actions: <Widget>[
FlatButton(onPressed: () {
_navigateToDashboard(context);
},
child: Text("Dashboard"))
],
);
}
);
}
void _navigateToDashboard(BuildContext context) {
final route = MaterialPageRoute(builder: (_) => Dashboard());
Navigator.of(context).push(route);
}
}
......@@ -31,6 +31,7 @@ dependencies:
flutter_dotenv: ^2.1.0
json_serializable: ^3.2.5
cached_network_image: ^2.0.0
shared_preferences: 0.5.6+3
dev_dependencies:
flutter_test:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment