Skip to content

Commit 7345ef5

Browse files
committed
login rest api connected
1 parent a031b90 commit 7345ef5

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

lib/NetworkHandler.dart

+2-8
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,15 @@ class NetworkHandler {
2020
log.i(response.statusCode);
2121
}
2222

23-
Future<dynamic> post(String url, Map<String, String> body) async {
23+
Future<http.Response> post(String url, Map<String, String> body) async {
2424
url = formater(url);
2525
log.d(body);
2626
var response = await http.post(
2727
url,
2828
headers: {"Content-type": "application/json"},
2929
body: json.encode(body),
3030
);
31-
if (response.statusCode == 200 || response.statusCode == 201) {
32-
log.i(response.body);
33-
34-
return response;
35-
}
36-
log.d(response.body);
37-
log.d(response.statusCode);
31+
return response;
3832
}
3933

4034
String formater(String url) {

lib/Pages/SinInPage.dart

+44-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import 'package:blogapp/Pages/SignUpPage.dart';
24
import "package:flutter/material.dart";
35

@@ -15,7 +17,6 @@ class _SignInPageState extends State<SignInPage> {
1517
final _globalkey = GlobalKey<FormState>();
1618
NetworkHandler networkHandler = NetworkHandler();
1719
TextEditingController _usernameController = TextEditingController();
18-
TextEditingController _emailController = TextEditingController();
1920
TextEditingController _passwordController = TextEditingController();
2021
String errorText;
2122
bool validate = false;
@@ -95,27 +96,54 @@ class _SignInPageState extends State<SignInPage> {
9596
height: 30,
9697
),
9798
InkWell(
98-
onTap: () async {},
99-
child: circular
100-
? CircularProgressIndicator()
101-
: Container(
102-
width: 150,
103-
height: 50,
104-
decoration: BoxDecoration(
105-
borderRadius: BorderRadius.circular(10),
106-
color: Color(0xff00A86B),
107-
),
108-
child: Center(
109-
child: Text(
99+
onTap: () async {
100+
setState(() {
101+
circular = true;
102+
});
103+
Map<String, String> data = {
104+
"username": _usernameController.text,
105+
"password": _passwordController.text,
106+
};
107+
var response =
108+
await networkHandler.post("/user/login", data);
109+
110+
if (response.statusCode == 200 ||
111+
response.statusCode == 201) {
112+
Map<String, dynamic> output = json.decode(response.body);
113+
print(output["token"]);
114+
setState(() {
115+
validate = true;
116+
circular = false;
117+
});
118+
} else {
119+
String output = json.decode(response.body);
120+
setState(() {
121+
validate = false;
122+
errorText = output;
123+
circular = false;
124+
});
125+
}
126+
},
127+
child: Container(
128+
width: 150,
129+
height: 50,
130+
decoration: BoxDecoration(
131+
borderRadius: BorderRadius.circular(10),
132+
color: Color(0xff00A86B),
133+
),
134+
child: Center(
135+
child: circular
136+
? CircularProgressIndicator()
137+
: Text(
110138
"Sign In",
111139
style: TextStyle(
112140
color: Colors.white,
113141
fontSize: 18,
114142
fontWeight: FontWeight.bold,
115143
),
116144
),
117-
),
118-
),
145+
),
146+
),
119147
),
120148
// Divider(
121149
// height: 50,
@@ -155,13 +183,9 @@ class _SignInPageState extends State<SignInPage> {
155183
Text("Password"),
156184
TextFormField(
157185
controller: _passwordController,
158-
validator: (value) {
159-
if (value.isEmpty) return "Password can't be empty";
160-
if (value.length < 8) return "Password lenght must have >=8";
161-
return null;
162-
},
163186
obscureText: vis,
164187
decoration: InputDecoration(
188+
errorText: validate ? null : errorText,
165189
suffixIcon: IconButton(
166190
icon: Icon(vis ? Icons.visibility_off : Icons.visibility),
167191
onPressed: () {

0 commit comments

Comments
 (0)