add login invalid credentials error and logout funcationality

feature/passo/PASSO-#1-Sync-data-from-device-to-postgre-and-vice-versa
PGAN-MIS 2023-02-10 10:02:35 +08:00
parent 118a4f4587
commit 98991c0ef6
5 changed files with 58 additions and 9 deletions

View File

@ -1,3 +1,6 @@
import 'dart:async';
import 'dart:io';
import 'package:barcode_scan2/barcode_scan2.dart'; import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
@ -7,6 +10,7 @@ import 'package:unit2/screens/unit2/login/functions/get_app_version.dart';
import 'package:unit2/sevices/login_service/auth_service.dart'; import 'package:unit2/sevices/login_service/auth_service.dart';
import '../../utils/scanner.dart'; import '../../utils/scanner.dart';
import '../../utils/text_container.dart';
part 'user_event.dart'; part 'user_event.dart';
part 'user_state.dart'; part 'user_state.dart';
@ -14,6 +18,7 @@ part 'user_state.dart';
class UserBloc extends Bloc<UserEvent, UserState> { class UserBloc extends Bloc<UserEvent, UserState> {
UserData? _userData; UserData? _userData;
VersionInfo? _versionInfo; VersionInfo? _versionInfo;
String? _apkVersion;
UserBloc() : super(UserInitial()) { UserBloc() : super(UserInitial()) {
// this event is called when opening the app to check if // this event is called when opening the app to check if
// there is new app version // there is new app version
@ -23,7 +28,8 @@ class UserBloc extends Bloc<UserEvent, UserState> {
VersionInfo versionInfo = await AuthService.instance.getVersionInfo(); VersionInfo versionInfo = await AuthService.instance.getVersionInfo();
_versionInfo = versionInfo; _versionInfo = versionInfo;
String apkVersion = await getAppVersion(); String apkVersion = await getAppVersion();
emit(VersionLoaded(versionInfo: _versionInfo,apkVersion: apkVersion)); _apkVersion = apkVersion;
emit(VersionLoaded(versionInfo: _versionInfo,apkVersion: _apkVersion));
} catch (e) { } catch (e) {
emit(UserError( emit(UserError(
message: e.toString(), message: e.toString(),
@ -31,7 +37,7 @@ class UserBloc extends Bloc<UserEvent, UserState> {
} }
}); });
on<LoadVersion>((event, emit) { on<LoadVersion>((event, emit) {
emit(VersionLoaded(versionInfo: _versionInfo)); emit(VersionLoaded(versionInfo: _versionInfo,apkVersion: _apkVersion));
}); });
on<UserLogin>((event, emit) async { on<UserLogin>((event, emit) async {
try { try {
@ -39,8 +45,12 @@ class UserBloc extends Bloc<UserEvent, UserState> {
.webLogin(username: event.username, password: event.password); .webLogin(username: event.username, password: event.password);
_userData = userData; _userData = userData;
emit(UserLoggedIn(userData: _userData)); emit(UserLoggedIn(userData: _userData));
} catch (e) { } on TimeoutException catch (_) {
emit(UserError(message: e.toString())); emit(InternetTimeout(message: timeoutError));
}on SocketException catch (_){
emit(InternetTimeout(message:timeoutError));
}on Error catch(_){
emit(InvalidCredentials(message: "Invalid username or password"));
} }
}); });
on<UuidLogin>((event, emit) async { on<UuidLogin>((event, emit) async {
@ -49,8 +59,12 @@ class UserBloc extends Bloc<UserEvent, UserState> {
.qrLogin(uuid: event.uuid, password: event.password); .qrLogin(uuid: event.uuid, password: event.password);
_userData = userData; _userData = userData;
emit(UserLoggedIn(userData: _userData)); emit(UserLoggedIn(userData: _userData));
} catch (e) { } on TimeoutException catch (_) {
emit(UserError(message: e.toString())); emit(InternetTimeout(message: timeoutError));
}on SocketException catch (_){
emit(InternetTimeout(message:timeoutError));
}on Error catch(_){
emit(InvalidCredentials(message: "Invalid username or password"));
} }
}); });
on<LoadLoggedInUser>((event, emit) { on<LoadLoggedInUser>((event, emit) {

View File

@ -48,3 +48,15 @@ class UuidLoaded extends UserState{
@override @override
List<Object> get props => [uuid]; List<Object> get props => [uuid];
} }
class InternetTimeout extends UserState{
final String message;
InternetTimeout({required this.message});
@override
List<Object> get props => [message];
}
class InvalidCredentials extends UserState{
final String message ;
InvalidCredentials ({required this.message});
}

View File

@ -62,7 +62,7 @@ class _MenuScreenState extends State<MenuScreen> {
Expanded( Expanded(
child: Align( child: Align(
alignment: FractionalOffset.bottomLeft, alignment: FractionalOffset.bottomLeft,
child: getTile(WebSymbols.logout, "Logout", 'login', context, child: getTile(WebSymbols.logout, "Logout", '/', context,
widget.userData!), widget.userData!),
)), )),
], ],

View File

@ -7,6 +7,7 @@ import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:form_builder_validators/form_builder_validators.dart'; import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart'; import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:unit2/screens/unit2/login/components/update_required.dart'; import 'package:unit2/screens/unit2/login/components/update_required.dart';
import 'package:unit2/utils/alerts.dart';
import 'package:unit2/utils/text_container.dart'; import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/error_state.dart'; import 'package:unit2/widgets/error_state.dart';
import '../../../bloc/user/user_bloc.dart'; import '../../../bloc/user/user_bloc.dart';
@ -43,8 +44,24 @@ class _UniT2LoginState extends State<UniT2Login> {
Navigator.pushReplacementNamed(context, '/module-screen'); Navigator.pushReplacementNamed(context, '/module-screen');
} }
if (state is UuidLoaded) { if (state is UuidLoaded) {
final progress = ProgressHUD.of(context);
progress!.dismiss();
Navigator.pushNamed(context, '/qr-login'); Navigator.pushNamed(context, '/qr-login');
} }
if (state is UserError) {
final progress = ProgressHUD.of(context);
progress!.dismiss();
}
if (state is InternetTimeout) {
final progress = ProgressHUD.of(context);
progress!.dismiss();
}
if (state is InvalidCredentials) {
final progress = ProgressHUD.of(context);
progress!.dismiss();
errorAlert(context, "Error Login", state.message);
context.read<UserBloc>().add(LoadVersion());
}
}, builder: (context, state) { }, builder: (context, state) {
if (state is VersionLoaded) { if (state is VersionLoaded) {
return Builder(builder: (context) { return Builder(builder: (context) {
@ -293,6 +310,7 @@ class _UniT2LoginState extends State<UniT2Login> {
), ),
); );
} else { } else {
//New update available
return Update( return Update(
apkVersion: state.apkVersion!, apkVersion: state.apkVersion!,
currenVersion: state.versionInfo!.version!, currenVersion: state.versionInfo!.version!,
@ -305,6 +323,11 @@ class _UniT2LoginState extends State<UniT2Login> {
message: state.message, message: state.message,
); );
} }
if (state is InternetTimeout) {
return ErrorState(
message: state.message,
);
}
if (state is SplashScreen) { if (state is SplashScreen) {
return const UniTSplashScreen(); return const UniTSplashScreen();
} }

View File

@ -4,8 +4,8 @@ class Url {
String host() { String host() {
// return '192.168.10.221:3003'; // return '192.168.10.221:3003';
return 'agusandelnorte.gov.ph'; // return 'agusandelnorte.gov.ph';
// return 'devweb.agusandelnorte.gov.ph'; return 'devweb.agusandelnorte.gov.ph';
} }
String authentication() { String authentication() {