Skip to content

Commit

Permalink
Merge pull request #84 from mahdiramezanii/develop
Browse files Browse the repository at this point in the history
Handel Erorr for users
  • Loading branch information
mahdiramezanii authored May 12, 2024
2 parents 06ca2e5 + 4de3c16 commit 1b0cd7e
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 14 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/> <!-- Add this -->
<application
android:label="apple_shop"
android:name="${applicationName}"
Expand Down
5 changes: 4 additions & 1 deletion lib/data/datasource/authentication_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ class AuthenticationDataSource implements IAuthenticationDataSource {
"username": username,
"password": password,
"passwordConfirm": passwordConfirm,
"name": username,
});
login(username, password);
} on DioException catch (ex) {
throw ApiExceptiopn(
code: ex.response!.statusCode!,
messgae: ex.response!.data["message"],
response: ex.response,
);
} catch (ex) {
throw ApiExceptiopn(code: 0, messgae: "Erorr");
Expand All @@ -44,7 +47,7 @@ class AuthenticationDataSource implements IAuthenticationDataSource {

if (response.statusCode == 200) {
AuthManager.setUserId(response.data["record"]["id"]);

AuthManager.setToken(response.data["token"]);

return response.data["token"];
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/repository/authentication_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AuthenticatinRepository implements IAuthenticatinRepository {
await iauth.registerUser(username, password, passwordConfirm);
return const Right("ثبت نام با موفقیت انجام شد");
} on ApiExceptiopn catch (ex) {
return const Left("ثبت نام انجام نشد");
return Left(ex.messgae);
}
}

Expand All @@ -32,7 +32,7 @@ class AuthenticatinRepository implements IAuthenticatinRepository {
String token = await iauth.login(username, password);

if (token.isNotEmpty) {
AuthManager.setToken(token);

return Right("لاگین انجام شد");
} else {
return const Left("خطا");
Expand Down
4 changes: 3 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import 'package:apple_shop/util/auth_manager.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hive_flutter/hive_flutter.dart';

GlobalKey<NavigatorState> globalKey = GlobalKey<NavigatorState>();

void main() async {

WidgetsFlutterBinding();
await Hive.initFlutter();
Hive.registerAdapter(BucketAdapter());
await Hive.openBox<Bucket>("BucketBox");
await initLocator();
runApp(MyApp());


}

class MyApp extends StatelessWidget {
Expand Down
43 changes: 37 additions & 6 deletions lib/screan/login_screan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,20 @@ class ViewLoginWidget extends StatelessWidget {
),
BlocConsumer<AuthBloc, AuthState>(listener: (context, state) {
if (state is ResponseAuthState) {
state.response.fold((l) {}, (r) {
state.response.fold((l) {
_usernameTextController.text = "";
_passwordTextController.text = "";
var snakBar = SnackBar(
content: Text(l),
showCloseIcon: true,
closeIconColor: Colors.red,
behavior: SnackBarBehavior.floating,
duration: const Duration(seconds: 2),
);
ScaffoldMessenger.of(context).showSnackBar(
snakBar,
);
}, (r) {
return Navigator.of(context)
.push(MaterialPageRoute(builder: (context) {
return BottomNavigatonScrean();
Expand Down Expand Up @@ -162,12 +175,30 @@ class ViewLoginWidget extends StatelessWidget {
}

if (state is ResponseAuthState) {
Text widget = const Text("");
Widget widget = const Text("");

state.response.fold((l) {
widget = Text(
l,
style: const TextStyle(color: Colors.red),
widget = ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size(200, 50),
backgroundColor: MyColors.green,
),
onPressed: () {
BlocProvider.of<AuthBloc>(context).add(
RequestLoginEvent(
username: _usernameTextController.text,
password: _passwordTextController.text,
),
);
},
child: const Text(
"ورود به حساب کاربری",
style: TextStyle(
fontFamily: "sb",
color: Colors.white,
fontSize: 18,
),
),
);
}, (r) {
widget = Text(
Expand All @@ -186,7 +217,7 @@ class ViewLoginWidget extends StatelessWidget {
),
GestureDetector(
onTap: () {
Navigator.of(context).push(
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) {
return RegisterScrean();
}),
Expand Down
13 changes: 11 additions & 2 deletions lib/screan/register_screan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ class ViewRegisterWidget extends StatelessWidget {
return BlocConsumer<AuthBloc, AuthState>(
listener: (context, state) {
if (state is ResponseAuthState) {
state.response.fold((l) => null, (r) {
state.response.fold((l) {
var snakBar = SnackBar(
content: Text(l),
behavior: SnackBarBehavior.floating,
showCloseIcon: true,
duration: const Duration(seconds: 4),
closeIconColor: Colors.red,
);
ScaffoldMessenger.of(context).showSnackBar(snakBar);
}, (r) {
return Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BottomNavigatonScrean();
Expand Down Expand Up @@ -209,7 +218,7 @@ class ViewRegisterWidget extends StatelessWidget {
),
if (state is ResponseAuthState) ...{
state.response.fold((l) {
return Text(l);
return Text("");
}, (r) {
return Text(r);
})
Expand Down
42 changes: 40 additions & 2 deletions lib/util/api_exception.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
class ApiExceptiopn implements Exception{
import 'package:dio/dio.dart';

class ApiExceptiopn implements Exception {
int code;
String messgae;
Response<dynamic>? response;
ApiExceptiopn({required this.code, required this.messgae, this.response}) {
if (code != 400) {
return;
}
if (messgae == "Failed to authenticate.") {
messgae = "نام کاربری یا رمز عبور اشتباه است";
}

if (messgae == "Something went wrong while processing your request.") {
messgae = "مقادیر ورودی نمیتواند خالی باشد!";
}

if (messgae == "Failed to create record."){

if (response?.data["data"]["username"] != null){

if (response?.data["data"]["username"]["message"] == "The username is invalid or already in use."){

messgae = "نام کاربری تکراری است";
}
}
}




if (messgae == "Failed to create record."){

if (response?.data["data"]["passwordConfirm"] != null){

if (response?.data["data"]["passwordConfirm"]["message"] == "Values don't match."){

ApiExceptiopn({required this.code, required this.messgae});
messgae = "پسوردها شباهت ندارد";
}
}
}
}
}

0 comments on commit 1b0cd7e

Please sign in to comment.