feat: user pin setup uncompletely yet
This commit is contained in:
parent
f620157de8
commit
da4191408d
|
@ -27,7 +27,7 @@ final router = GoRouter(
|
|||
return VerifotpScreen(phone: phone!);
|
||||
},
|
||||
),
|
||||
GoRoute(path: '/setpin', builder: (context, state) => UserPinScreen()),
|
||||
GoRoute(path: '/setpin', builder: (context, state) => SetPinScreen()),
|
||||
GoRoute(path: '/verifpin', builder: (context, state) => VerifPinScreen()),
|
||||
GoRoute(
|
||||
path: '/navigasi',
|
||||
|
|
|
@ -1,34 +1,31 @@
|
|||
import 'package:rijig_mobile/core/api_services.dart';
|
||||
import 'package:rijig_mobile/model/response_model.dart';
|
||||
|
||||
class PinModel {
|
||||
final ApiService _apiService = ApiService();
|
||||
|
||||
Future<bool> checkPinStatus() async {
|
||||
Future<ResponseModel?> checkPinStatus(String userId) async {
|
||||
try {
|
||||
var response = await _apiService.get('/cek-pin-status');
|
||||
if (response['meta']['status'] == 200) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return ResponseModel.fromJson(response);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> setPin(String userPin) async {
|
||||
Future<ResponseModel?> setPin(String pin) async {
|
||||
try {
|
||||
var response = await _apiService.post('/set-pin', {'userpin': userPin});
|
||||
return response['meta']['status'] == 201;
|
||||
var response = await _apiService.post('/set-pin', {'userpin': pin});
|
||||
return ResponseModel.fromJson(response);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> verifyPin(String userPin) async {
|
||||
Future<ResponseModel?> verifyPin(String pin) async {
|
||||
try {
|
||||
var response = await _apiService.post('/verif-pin', {'userpin': userPin});
|
||||
return response['meta']['status'] == 200;
|
||||
var response = await _apiService.post('/verif-pin', {'userpin': pin});
|
||||
return ResponseModel.fromJson(response);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
|
|
|
@ -1,87 +1,54 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:rijig_mobile/core/guide.dart';
|
||||
import 'package:rijig_mobile/core/router.dart';
|
||||
import 'package:rijig_mobile/viewmodel/userpin_vmod.dart';
|
||||
|
||||
class UserPinScreen extends StatefulWidget {
|
||||
const UserPinScreen({super.key});
|
||||
class SetPinScreen extends StatefulWidget {
|
||||
const SetPinScreen({super.key});
|
||||
|
||||
@override
|
||||
State<UserPinScreen> createState() => _UserPinScreenState();
|
||||
SetPinScreenState createState() => SetPinScreenState();
|
||||
}
|
||||
|
||||
class _UserPinScreenState extends State<UserPinScreen> {
|
||||
final TextEditingController _pinController = TextEditingController();
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String errorMessage = '';
|
||||
class SetPinScreenState extends State<SetPinScreen> {
|
||||
final _pinController = TextEditingController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final pinViewModel = Provider.of<PinViewModel>(context);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Buat PIN Pengguna")),
|
||||
appBar: AppBar(title: Text("Buat PIN Baru")),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _pinController,
|
||||
decoration: const InputDecoration(labelText: 'Masukkan PIN'),
|
||||
obscureText: true,
|
||||
maxLength: 6,
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'PIN harus diisi';
|
||||
} else if (value.length != 6) {
|
||||
return 'PIN harus 6 digit';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
if (_formKey.currentState?.validate() ?? false) {
|
||||
final pin = _pinController.text;
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text("Buat PIN Anda (6 digit)", style: TextStyle(fontSize: 18)),
|
||||
SizedBox(height: 20),
|
||||
TextField(
|
||||
controller: _pinController,
|
||||
decoration: InputDecoration(labelText: "PIN"),
|
||||
keyboardType: TextInputType.number,
|
||||
obscureText: true,
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
String pin = _pinController.text;
|
||||
|
||||
bool pinCreated = await pinViewModel.checkPinStatus();
|
||||
|
||||
if (!pinCreated) {
|
||||
bool pinSet = await pinViewModel.setPin(pin);
|
||||
if (pinSet) {
|
||||
router.go('/navigasi');
|
||||
} else {
|
||||
setState(() {
|
||||
errorMessage = 'Gagal membuat PIN';
|
||||
});
|
||||
}
|
||||
} else {
|
||||
bool pinVerified = await pinViewModel.verifyPin(pin);
|
||||
if (pinVerified) {
|
||||
router.go('/navigasi');
|
||||
} else {
|
||||
setState(() {
|
||||
errorMessage = 'PIN yang anda masukkan salah';
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text('Kirim'),
|
||||
),
|
||||
if (errorMessage.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 20),
|
||||
child: Text(errorMessage, style: TextStyle(color: redColor)),
|
||||
),
|
||||
],
|
||||
),
|
||||
await pinViewModel.createPin(pin);
|
||||
if (pinViewModel.pinExists == true) {
|
||||
router.go('/navigasi');
|
||||
} else {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('Gagal membuat PIN')));
|
||||
}
|
||||
},
|
||||
child: Text("Buat PIN"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -3,6 +3,7 @@ import 'package:pin_code_fields/pin_code_fields.dart';
|
|||
import 'package:provider/provider.dart';
|
||||
import 'package:rijig_mobile/core/router.dart';
|
||||
import 'package:rijig_mobile/viewmodel/auth_vmod.dart';
|
||||
import 'package:rijig_mobile/viewmodel/userpin_vmod.dart';
|
||||
import 'package:rijig_mobile/widget/buttoncard.dart';
|
||||
|
||||
class VerifotpScreen extends StatefulWidget {
|
||||
|
@ -19,7 +20,9 @@ class _VerifotpScreenState extends State<VerifotpScreen> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final viewModel = Provider.of<AuthViewModel>(context);
|
||||
|
||||
final authViewModel = Provider.of<AuthViewModel>(context);
|
||||
final pinViewModel = Provider.of<PinViewModel>(context);
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
|
@ -58,34 +61,42 @@ class _VerifotpScreenState extends State<VerifotpScreen> {
|
|||
horizontal: double.infinity,
|
||||
vertical: 50,
|
||||
onTap: () {
|
||||
// if (_otpController.text.isNotEmpty) {
|
||||
// viewModel.verifyOtp(widget.phone, _otpController.text).then(
|
||||
// (_) {
|
||||
// if (viewModel.errorMessage == null) {
|
||||
// router.go('/navigasi');
|
||||
// } else {
|
||||
// debugPrint(viewModel.errorMessage ?? '');
|
||||
// }
|
||||
// },
|
||||
// );
|
||||
// }
|
||||
if (_otpController.text.isNotEmpty) {
|
||||
viewModel.verifyOtp(widget.phone, _otpController.text).then(
|
||||
(_) {
|
||||
if (viewModel.errorMessage == null) {
|
||||
if (viewModel.pinExists == false) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
authViewModel.verifyOtp(widget.phone, _otpController.text).then((
|
||||
_,
|
||||
) {
|
||||
|
||||
pinViewModel
|
||||
.checkPinStatus(
|
||||
authViewModel.authModel?.data?['user_id'],
|
||||
)
|
||||
.then((_) {
|
||||
if (pinViewModel.pinExists == false) {
|
||||
router.go('/setpin');
|
||||
} else {
|
||||
} else if (pinViewModel.pinExists == true) {
|
||||
router.go('/verifpin');
|
||||
} else {
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Gagal memverifikasi status PIN'),
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
debugPrint(viewModel.errorMessage ?? '');
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
loadingTrue: viewModel.isLoading,
|
||||
loadingTrue: authViewModel.isLoading,
|
||||
usingRow: false,
|
||||
),
|
||||
],
|
||||
|
|
|
@ -7,72 +7,52 @@ class VerifPinScreen extends StatefulWidget {
|
|||
const VerifPinScreen({super.key});
|
||||
|
||||
@override
|
||||
State<VerifPinScreen> createState() => _VerifPinScreenState();
|
||||
VerifPinScreenState createState() => VerifPinScreenState();
|
||||
}
|
||||
|
||||
class _VerifPinScreenState extends State<VerifPinScreen> {
|
||||
final TextEditingController _pinController = TextEditingController();
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String errorMessage = '';
|
||||
class VerifPinScreenState extends State<VerifPinScreen> {
|
||||
final _pinController = TextEditingController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final pinViewModel = Provider.of<PinViewModel>(context);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Verifikasi PIN")),
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _pinController,
|
||||
decoration: const InputDecoration(labelText: 'Masukkan PIN'),
|
||||
obscureText: true,
|
||||
maxLength: 6,
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'PIN harus diisi';
|
||||
} else if (value.length != 6) {
|
||||
return 'PIN harus 6 digit';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
if (_formKey.currentState?.validate() ?? false) {
|
||||
final pin = _pinController.text;
|
||||
|
||||
bool pinVerified = await pinViewModel.verifyPin(pin);
|
||||
if (pinVerified) {
|
||||
router.go('/navigasi');
|
||||
} else {
|
||||
setState(() {
|
||||
errorMessage = 'PIN yang Anda masukkan salah';
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text('Kirim'),
|
||||
),
|
||||
if (errorMessage.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 20),
|
||||
child: Text(
|
||||
errorMessage,
|
||||
style: const TextStyle(color: Colors.red),
|
||||
),
|
||||
),
|
||||
],
|
||||
appBar: AppBar(title: Text("Verifikasi PIN")),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"Masukkan PIN yang sudah dibuat",
|
||||
style: TextStyle(fontSize: 18),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
TextField(
|
||||
controller: _pinController,
|
||||
decoration: InputDecoration(labelText: "PIN"),
|
||||
keyboardType: TextInputType.number,
|
||||
obscureText: true,
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
// String userId = 'user_id_here';
|
||||
String pin = _pinController.text;
|
||||
|
||||
await pinViewModel.verifyPin(pin);
|
||||
if (pinViewModel.pinExists == true) {
|
||||
router.go('/navigasi');
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('PIN yang anda masukkan salah')),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Text("Verifikasi PIN"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -64,7 +64,16 @@ class AuthViewModel extends ChangeNotifier {
|
|||
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool('isLoggedIn', true);
|
||||
pinExists = await _pinModel.checkPinStatus();
|
||||
|
||||
var pinStatusResponse = await _pinModel.checkPinStatus(
|
||||
response.data?['user_id'],
|
||||
);
|
||||
|
||||
if (pinStatusResponse?.status == 200) {
|
||||
pinExists = true;
|
||||
} else {
|
||||
pinExists = false;
|
||||
}
|
||||
|
||||
authModel = response;
|
||||
notifyListeners();
|
||||
|
|
|
@ -2,29 +2,57 @@ import 'package:flutter/material.dart';
|
|||
import 'package:rijig_mobile/model/userpin_model.dart';
|
||||
|
||||
class PinViewModel extends ChangeNotifier {
|
||||
final PinModel _pinService = PinModel();
|
||||
final PinModel _pinModel = PinModel();
|
||||
bool? pinExists;
|
||||
|
||||
Future<bool> checkPinStatus() async {
|
||||
Future<void> checkPinStatus(String userId) async {
|
||||
try {
|
||||
return await _pinService.checkPinStatus();
|
||||
var response = await _pinModel.checkPinStatus(userId);
|
||||
|
||||
if (response?.status == 200) {
|
||||
pinExists = true;
|
||||
} else {
|
||||
pinExists = false;
|
||||
}
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
throw Exception('Error checking PIN status: $e');
|
||||
debugPrint('Error checking pin status: $e');
|
||||
pinExists = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> setPin(String userPin) async {
|
||||
Future<void> createPin(String pin) async {
|
||||
try {
|
||||
return await _pinService.setPin(userPin);
|
||||
var response = await _pinModel.setPin(pin);
|
||||
|
||||
if (response?.status == 201) {
|
||||
pinExists = true;
|
||||
} else {
|
||||
pinExists = false;
|
||||
}
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
throw Exception('Error setting PIN: $e');
|
||||
debugPrint('Error creating pin: $e');
|
||||
pinExists = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> verifyPin(String userPin) async {
|
||||
Future<void> verifyPin(String pin) async {
|
||||
try {
|
||||
return await _pinService.verifyPin(userPin);
|
||||
var response = await _pinModel.verifyPin(pin);
|
||||
|
||||
if (response?.status == 200) {
|
||||
pinExists = true;
|
||||
} else {
|
||||
pinExists = false;
|
||||
}
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
throw Exception('Error verifying PIN: $e');
|
||||
debugPrint('Error verifying pin: $e');
|
||||
pinExists = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue