Skip to content

Commit

Permalink
Continue log in work
Browse files Browse the repository at this point in the history
  • Loading branch information
aguilaair committed Nov 22, 2021
1 parent abdc26f commit 857be1d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 69 deletions.
14 changes: 14 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:guard_app/providers/providers.dart';
import 'package:guard_app/screens/login.dart';
import 'package:guard_app/theme/dark_theme.dart';
import 'package:guard_app/theme/light_theme.dart';
Expand All @@ -25,3 +26,16 @@ class MyApp extends StatelessWidget {
);
}
}

class AuthWrapper extends HookConsumerWidget {
const AuthWrapper({Key? key}) : super(key: key);

@override
Widget build(BuildContext context, WidgetRef ref) {
final useAuth = ref.watch(authProvider.notifier);

final autoLoginState = useAuth.tryAutoLogin();

return Container();
}
}
32 changes: 28 additions & 4 deletions lib/providers/auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,31 @@ class AuthState {
String? userId;
Timer? authTimer;
Timer? refreshTimer;
bool isAuthing;

AuthState(
{this.authTimer,
this.expiryDate,
this.refreshTimer,
this.token,
this.userId});
this.userId,
this.isAuthing = false});

AuthState copyWith({
String? token,
DateTime? expiryDate,
String? userId,
Timer? authTimer,
Timer? refreshTimer,
bool? isAuthing,
}) {
return AuthState(
token: token ?? this.token,
expiryDate: expiryDate ?? this.expiryDate,
userId: userId ?? this.userId,
authTimer: authTimer ?? this.authTimer,
refreshTimer: refreshTimer ?? this.refreshTimer,
isAuthing: isAuthing ?? this.isAuthing,
);
}
}
Expand Down Expand Up @@ -72,14 +76,21 @@ class Auth extends StateNotifier<AuthState> {
if (isAuth) {
return true;
}

state = state.copyWith(isAuthing: true);

final prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey("authInfo")) {
state = state.copyWith(isAuthing: false);

return false;
}
final extractedData =
json.decode(prefs.getString("authInfo")!) as Map<String, dynamic>;
final expiryDate = DateTime.parse(extractedData["expiryDate"]);
if (expiryDate.isBefore(DateTime.now())) {
state = state.copyWith(isAuthing: false);

return false;
}
/*_token = extractedData["token"];
Expand All @@ -95,6 +106,8 @@ class Auth extends StateNotifier<AuthState> {

_autoLogout();
_autoRefreshToken();
state = state.copyWith(isAuthing: false);

return true;
}

Expand Down Expand Up @@ -126,7 +139,7 @@ class Auth extends StateNotifier<AuthState> {
userId: username,
);

if (state.token != null) {
if (tempState.token != null) {
tempState = tempState.copyWith(
expiryDate: JwtDecoder.getExpirationDate(state.token!),
);
Expand Down Expand Up @@ -202,8 +215,19 @@ class Auth extends StateNotifier<AuthState> {
}
}

Future<bool> signIn(email, passw) async {
await _auth(email, passw);
Future<bool> signIn(String email, String passw) async {
if ((email.isEmpty || passw.isEmpty || passw.length < 8) && !isAuth) {
return false;
}
state = state.copyWith(isAuthing: true);
try {
await _auth(email, passw);
} catch (e) {
state = state.copyWith(isAuthing: false);
return false;
}

state = state.copyWith(isAuthing: false);
return isAuth;
}

Expand Down
128 changes: 63 additions & 65 deletions lib/screens/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,73 +17,69 @@ class LoginPage extends HookConsumerWidget {
final usernameFocusNode = useFocusNode();
final passwordFocusNode = useFocusNode();

final useAuth = ref.watch(authProvider.notifier);

final autoLoginState = useAuth.tryAutoLogin();
final useAuth = ref.watch(authProvider);

return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: FutureBuilder<bool>(
future: autoLoginState,
builder: (context, future) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Logo(
width: 170,
),
SizedBox(
height: 12,
),
Text(
'''Welcome to the gatego guard app.
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Logo(
width: 170,
),
SizedBox(
height: 12,
),
Text(
'''Welcome to the gatego guard app.
Please log in to continue''',
textAlign: TextAlign.center,
),
],
),
textAlign: TextAlign.center,
),
Card(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12, vertical: 30),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextInput(
c: usernameController,
fn: usernameFocusNode,
text: "Username or Email",
icon: Icons.person,
),
TextInput(
c: passwordController,
fn: passwordFocusNode,
obscureText: true,
icon: Icons.vpn_key,
text: "Password",
nextFocus: (string) {},
),
],
),
],
),
),
Card(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 12, vertical: 30),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextInput(
c: usernameController,
fn: usernameFocusNode,
text: "Username or Email",
icon: Icons.person,
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(bottom: 50),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ElevatedButton.icon(
TextInput(
c: passwordController,
fn: passwordFocusNode,
obscureText: true,
icon: Icons.vpn_key,
text: "Password",
nextFocus: (string) {},
),
],
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(bottom: 50),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
!useAuth.isAuthing
? ElevatedButton.icon(
icon: const Icon(
Icons.login_rounded,
size: 18,
Expand Down Expand Up @@ -113,14 +109,16 @@ class LoginPage extends HookConsumerWidget {
fontSize: 18,
fontWeight: FontWeight.bold),
),
)
: CircularProgressIndicator(
color: Theme.of(context).primaryColor,
),
],
),
),
),
],
);
}),
],
),
),
),
],
),
),
),
);
Expand Down

0 comments on commit 857be1d

Please sign in to comment.