-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth_service.dart
101 lines (91 loc) · 3.19 KB
/
auth_service.dart
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
import 'dart:async';
import 'package:flutter_cbl_learning_path/features/login/models/user.dart';
enum AuthenticationStatus {
unknown,
authenticated,
unauthenticated,
authenticatedFailed,
logout
}
abstract class AuthenticationService {
Future<User?> getCurrentUser();
Future<void> authenticateUser(
{required String username, required String password});
Future<void> signOut();
Stream<AuthenticationStatus> get status;
}
class FakeAuthenticationService extends AuthenticationService {
FakeAuthenticationService() {
_user = null;
}
final _controller = StreamController<AuthenticationStatus>();
User? _user;
void dispose() => _controller.close();
@override
Stream<AuthenticationStatus> get status async* {
await Future<void>.delayed(const Duration(milliseconds: 20));
yield AuthenticationStatus.unauthenticated;
yield* _controller.stream;
}
@override
Future<User?> getCurrentUser() async => _user;
@override
Future<void> signOut() async {
_user = null;
_controller.add(AuthenticationStatus.logout);
}
@override
Future<bool> authenticateUser(
{required String username, required String password}) async {
await Future.delayed(const Duration(milliseconds: 50));
try {
_user = null;
_user = _users
.where((i) => i.username == username && i.password == password)
.first;
if (_user == null) {
_controller.add(AuthenticationStatus.authenticatedFailed);
} else {
_controller.add(AuthenticationStatus.authenticated);
return true;
}
} catch (e) {
_controller.add(AuthenticationStatus.authenticatedFailed);
}
return false;
}
final List<User> _users = [
const User(
username: 'demo@example.com', password: 'P@ssw0rd12', team: 'team1'),
const User(
username: 'demo1@example.com', password: 'P@ssw0rd12', team: 'team1'),
const User(
username: 'demo2@example.com', password: 'P@ssw0rd12', team: 'team2'),
const User(
username: 'demo3@example.com', password: 'P@ssw0rd12', team: 'team2'),
const User(
username: 'demo4@example.com', password: 'P@ssw0rd12', team: 'team3'),
const User(
username: 'demo5@example.com', password: 'P@ssw0rd12', team: 'team3'),
const User(
username: 'demo6@example.com', password: 'P@ssw0rd12', team: 'team4'),
const User(
username: 'demo7@example.com', password: 'P@ssw0rd12', team: 'team4'),
const User(
username: 'demo8@example.com', password: 'P@ssw0rd12', team: 'team5'),
const User(
username: 'demo9@example.com', password: 'P@ssw0rd12', team: 'team5'),
const User(
username: 'demo10@example.com', password: 'P@ssw0rd12', team: 'team6'),
const User(
username: 'demo11@example.com', password: 'P@ssw0rd12', team: 'team6'),
const User(
username: 'demo12@example.com', password: 'P@ssw0rd12', team: 'team7'),
const User(
username: 'demo13@example.com', password: 'P@ssw0rd12', team: 'team8'),
const User(
username: 'demo14@example.com', password: 'P@ssw0rd12', team: 'team9'),
const User(
username: 'demo15@example.com', password: 'P@ssw0rd12', team: 'team10'),
];
}