I am trying to create sign in method and also i want it to remember user by it’s tokens, so I created sqllite insert method and write there tokens. Also I want to create page where it will divide if user has token or not and show him RootPage or Registration Page. How can i chive this with flutter_bloc? For addition: i am saving token, but it still shows me registration page whatever if user has or hasn’t token
My bloc for remembering user:
on<CheckAuthorizationEvent>(_checkAuthorization);
}
void _checkAuthorization(
CheckAuthorizationEvent event, Emitter<AuthState> emit) async {
try {
final user = FirebaseAuth.instance.currentUser;
if (user == null) {
emit(const CheckAuth(null, false));
} else {
final tokenExists = await sQlService.isTokenExist();
emit(CheckAuth(user, tokenExists));
}
} on FirebaseAuthException catch (e) {
emit(AuthErrorState(e.getErrorMessage()));
}
}
and my page for dividing:
class _EnterPageState extends State<EnterPage> {
@override
void initState() {
super.initState();
final authBloc = BlocProvider.of<AuthBloc>(context);
authBloc.add(CheckAuthorizationEvent());
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocBuilder<AuthBloc, AuthState>(
builder: (context, state) {
if (state is CheckAuth) {
if (state.auth) {
return RootScreen();
} else {
return const RegistrationScreen();
}
}
return const Center(child: CircularProgressIndicator());
},
),
);
}
}
2
Answers
try authStateChanges() instated of currentUser.
you can well create a table on your server that saves user Firebase tokens along with there ID after signing up or logging in and create an api service the fetches them in json. that’s watch I do, i update them when they change,