Repository navigation
Expand file tree
/
Copy pathsignup_page.dart
More file actions
104 lines (92 loc) · 3.6 KB
/
Copy pathsignup_page.dart
File metadata and controls
104 lines (92 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import 'package:flood/flood.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:todo/presentation/pages/auth/login_page.dart';
import 'package:todo/presentation/pages/home_page.dart';
import 'package:todo/presentation/utils/redirect_utils.dart';
import 'package:todo_core/features/user/user.dart';
import 'package:todo_core/features/user/user_entity.dart';
class SignupRoute with IsRoute<SignupRoute> {
late final redirectPathProperty = field<String>(name: 'redirect');
late final initialEmailProperty = field<String>(name: 'email');
late final initialPasswordProperty = field<String>(name: 'pass');
@override
PathDefinition get pathDefinition => PathDefinition.string('signup');
@override
List<RouteProperty> get queryProperties => [redirectPathProperty];
@override
List<RouteProperty> get hiddenProperties => [
initialEmailProperty,
initialPasswordProperty,
];
@override
SignupRoute copy() {
return SignupRoute();
}
}
class SignupPage with IsAppPageWrapper<SignupRoute> {
@override
AppPage<SignupRoute> get appPage =>
AppPage<SignupRoute>().onlyIfNotLoggedIn().withParent((context, route) => LoginRoute());
@override
Widget onBuild(BuildContext context, SignupRoute route) {
final signupPort = useMemoized(() => Port.of({
'name': PortField.string().withDisplayName('Name').isNotBlank(),
'email': PortField.string(initialValue: route.initialEmailProperty.value)
.withDisplayName('Email')
.isNotBlank()
.isEmail(),
'password': PortField.string(initialValue: route.initialPasswordProperty.value)
.withDisplayName('Password')
.isNotBlank()
.isPassword(),
'confirmPassword': PortField.string()
.withDisplayName('Confirm Password')
.isNotBlank()
.isConfirmPassword(passwordField: 'password'),
}));
return StyledPage(
titleText: 'Signup',
body: StyledList.column.centered.withScrollbar(
children: [
StyledCard(
titleText: 'Info',
bodyText: 'Fill in your information to get started!',
leadingIcon: Icons.person,
children: [
StyledObjectPortBuilder(port: signupPort),
],
),
StyledButton.strong(
labelText: 'Sign Up',
onPressed: () async {
final result = await signupPort.submit();
if (!result.isValid) {
return;
}
final data = result.data;
try {
final account = await context
.find<AuthCoreComponent>()
.signup(AuthCredentials.email(email: data['email'], password: data['password']));
final deviceToken = context.find<MessagingCoreComponent>().deviceToken;
await context.dropCoreComponent.updateEntity(
UserEntity()..id = account.accountId,
(User user) => user
..emailProperty.set(data['email'])
..nameProperty.set(data['name'])
..deviceTokenProperty.set(deviceToken),
);
await context.push(HomeRoute());
} catch (e, stackTrace) {
final errorText = e.as<SignupFailure>()?.displayText ?? e.toString();
signupPort.setError(path: 'email', error: errorText);
context.logError(e, stackTrace);
}
},
),
],
),
);
}
}