Skip to content

Commit 0aa8793

Browse files
committed
feat(Authencation): done login and register
1 parent 3655de4 commit 0aa8793

File tree

14 files changed

+272
-72
lines changed

14 files changed

+272
-72
lines changed

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
BASE_URL=2school.ml
2-
SOCKET_URL=2school.ml
1+
BASE_URL=https://natha-lms.tk/
2+
SOCKET_URL=https://natha-lms.tk/
33
MODE=DEV

lib/src/blocs/authentication/authentication_bloc.dart

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import 'package:bloc/bloc.dart';
33
import 'package:cloudmate/src/blocs/authentication/bloc.dart';
44
import 'package:cloudmate/src/configs/application.dart';
55
import 'package:cloudmate/src/models/user.dart';
6+
import 'package:cloudmate/src/resources/local/user_local.dart';
7+
import 'package:cloudmate/src/resources/remote/authentication_repository.dart';
8+
import 'package:cloudmate/src/resources/remote/user_repository.dart';
69
import 'package:cloudmate/src/routes/app_pages.dart';
710

811
class AuthBloc extends Bloc<AuthEvent, AuthState> {
@@ -14,26 +17,83 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
1417
@override
1518
Stream<AuthState> mapEventToState(event) async* {
1619
if (event is OnAuthCheck) {
17-
yield await _onAuthCheck(event);
20+
bool isLogined = await _onAuthCheck();
21+
if (isLogined) {
22+
yield AuthenticationSuccess();
23+
} else {
24+
yield AuthenticationFail();
25+
}
1826
}
1927

20-
if (event is OnAuthProcess) {
21-
yield await _handlePressedLogin(event);
28+
if (event is OnClear) {
29+
yield AuthenticationSuccess(userModel: userModel);
2230
}
2331

24-
if (event is OnClear) {
25-
yield AuthenticationFail();
32+
if (event is LoginEvent) {
33+
bool isSuccess = await _handleLogin(event);
34+
AppNavigator.pop();
35+
if (isSuccess) {
36+
yield AuthenticationSuccess();
37+
} else {
38+
yield AuthenticationFail();
39+
}
40+
}
41+
42+
if (event is RegisterEvent) {
43+
bool isSuccess = await _handleRegister(event);
44+
AppNavigator.pop();
45+
if (isSuccess) {
46+
yield AuthenticationSuccess();
47+
} else {
48+
yield AuthenticationFail();
49+
}
50+
}
51+
52+
if (event is LogOutEvent) {
53+
bool isSuccess = await _handleLogOut();
54+
if (isSuccess) {
55+
yield AuthenticationFail();
56+
}
57+
}
58+
59+
if (event is GetInfoUser) {
60+
_handleGetUserInfo();
61+
yield AuthenticationSuccess();
2662
}
2763
}
2864

29-
Future<AuthState> _onAuthCheck(OnAuthCheck event) async {
30-
await Future.delayed(Duration(seconds: 1));
31-
return AuthenticationSuccess(userModel: userModel);
65+
Future<bool> _onAuthCheck() async {
66+
return UserLocal().getAccessToken() != '';
67+
}
68+
69+
Future<bool> _handleLogin(LoginEvent event) async {
70+
bool isSuccess = await AuthenticationRepository().login(
71+
event.username,
72+
event.password,
73+
);
74+
75+
return isSuccess;
76+
}
77+
78+
Future<bool> _handleRegister(RegisterEvent event) async {
79+
bool isSuccess = await AuthenticationRepository().register(
80+
fistName: event.firstName,
81+
lastName: event.lastName,
82+
username: event.username,
83+
password: event.password,
84+
);
85+
86+
return isSuccess;
87+
}
88+
89+
Future<bool> _handleLogOut() async {
90+
await AuthenticationRepository().logOut();
91+
return true;
3292
}
3393

34-
Future<AuthState> _handlePressedLogin(OnAuthProcess event) async {
35-
print(event.username);
36-
AppNavigator.pop();
37-
return AuthenticationSuccess(userModel: userModel);
94+
Future<void> _handleGetUserInfo() async {
95+
UserModel? user = await UserRepository().getInfoUser();
96+
userModel = user;
97+
print(userModel.toString());
3898
}
3999
}

lib/src/blocs/authentication/authentication_event.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:cloudmate/src/blocs/authentication/bloc.dart';
2+
13
abstract class AuthEvent {}
24

35
class OnAuthCheck extends AuthEvent {}
@@ -9,3 +11,26 @@ class OnAuthProcess extends AuthEvent {
911
}
1012

1113
class OnClear extends AuthEvent {}
14+
15+
class RegisterEvent extends AuthEvent {
16+
final String firstName;
17+
final String lastName;
18+
final String username;
19+
final String password;
20+
RegisterEvent({
21+
required this.username,
22+
required this.password,
23+
required this.firstName,
24+
required this.lastName,
25+
});
26+
}
27+
28+
class LoginEvent extends AuthEvent {
29+
final String username;
30+
final String password;
31+
LoginEvent({required this.username, required this.password});
32+
}
33+
34+
class LogOutEvent extends AuthEvent {}
35+
36+
class GetInfoUser extends AuthEvent {}

lib/src/blocs/authentication/authentication_state.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ class AuthenticationSuccess extends AuthState {
1010
}
1111

1212
class AuthenticationFail extends AuthState {}
13+
14+
class Authenticating extends AuthState {}

lib/src/models/user.dart

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,114 @@ import 'dart:convert';
22

33
class UserModel {
44
final String id;
5-
final String image;
6-
final String email;
7-
final String phone;
8-
final String fullName;
5+
final String? image;
6+
final String? blurHash;
7+
final String? phone;
8+
final String? displayName;
9+
final String? firstName;
10+
final String? lastName;
11+
final int? status;
12+
final String? intro;
13+
914
UserModel({
1015
required this.id,
11-
required this.image,
12-
required this.email,
13-
required this.phone,
14-
required this.fullName,
16+
this.image,
17+
this.blurHash,
18+
this.phone,
19+
this.displayName,
20+
this.firstName,
21+
this.lastName,
22+
this.status,
23+
this.intro,
1524
});
1625

1726
UserModel copyWith({
1827
String? id,
1928
String? image,
20-
String? email,
29+
String? blurHash,
2130
String? phone,
22-
String? fullName,
31+
String? displayName,
32+
String? firstName,
33+
String? lastName,
34+
int? status,
35+
String? intro,
2336
}) {
2437
return UserModel(
2538
id: id ?? this.id,
2639
image: image ?? this.image,
27-
email: email ?? this.email,
40+
blurHash: blurHash ?? this.blurHash,
2841
phone: phone ?? this.phone,
29-
fullName: fullName ?? this.fullName,
42+
displayName: displayName ?? this.displayName,
43+
firstName: firstName ?? this.firstName,
44+
lastName: lastName ?? this.lastName,
45+
status: status ?? this.status,
46+
intro: intro ?? this.intro,
3047
);
3148
}
3249

3350
Map<String, dynamic> toMap() {
3451
return {
3552
'id': id,
3653
'image': image,
37-
'email': email,
54+
'blurHash': blurHash,
3855
'phone': phone,
39-
'fullName': fullName,
56+
'displayName': displayName,
57+
'firstName': firstName,
58+
'lastName': lastName,
59+
'status': status,
60+
'intro': intro,
4061
};
4162
}
4263

4364
factory UserModel.fromMap(Map<String, dynamic> map) {
4465
return UserModel(
45-
id: map['id'],
66+
id: map['_id'],
4667
image: map['image'],
47-
email: map['email'],
68+
blurHash: map['blurHash'],
4869
phone: map['phone'],
49-
fullName: map['fullName'],
70+
displayName: map['displayName'],
71+
firstName: map['firstName'],
72+
lastName: map['lastName'],
73+
status: map['status'],
74+
intro: map['intro'],
5075
);
5176
}
5277

5378
String toJson() => json.encode(toMap());
5479

55-
factory UserModel.fromJson(String source) =>
56-
UserModel.fromMap(json.decode(source));
80+
factory UserModel.fromJson(String source) => UserModel.fromMap(json.decode(source));
5781

5882
@override
5983
String toString() {
60-
return 'UserModel(id: $id, image: $image, email: $email, phone: $phone, fullName: $fullName)';
84+
return 'UserModel(id: $id, image: $image, blurHash: $blurHash, phone: $phone, displayName: $displayName, firstName: $firstName, lastName: $lastName, status: $status, intro: $intro)';
6185
}
6286

6387
@override
6488
bool operator ==(Object other) {
6589
if (identical(this, other)) return true;
66-
90+
6791
return other is UserModel &&
68-
other.id == id &&
69-
other.image == image &&
70-
other.email == email &&
71-
other.phone == phone &&
72-
other.fullName == fullName;
92+
other.id == id &&
93+
other.image == image &&
94+
other.blurHash == blurHash &&
95+
other.phone == phone &&
96+
other.displayName == displayName &&
97+
other.firstName == firstName &&
98+
other.lastName == lastName &&
99+
other.status == status &&
100+
other.intro == intro;
73101
}
74102

75103
@override
76104
int get hashCode {
77105
return id.hashCode ^
78-
image.hashCode ^
79-
email.hashCode ^
80-
phone.hashCode ^
81-
fullName.hashCode;
106+
image.hashCode ^
107+
blurHash.hashCode ^
108+
phone.hashCode ^
109+
displayName.hashCode ^
110+
firstName.hashCode ^
111+
lastName.hashCode ^
112+
status.hashCode ^
113+
intro.hashCode;
82114
}
83115
}

lib/src/resources/api_gateway.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class ApiGateway {
2-
static const LOGIN = 'api/auth/login';
3-
static const REGISTER = 'api/auth/register';
2+
// Authentication
3+
static const LOGIN = 'api/authentication/login';
4+
static const REGISTER = 'api/authentication/register';
5+
6+
// User
7+
static const GET_INFO = 'api/user/info';
48
}

lib/src/resources/handle_apis.dart renamed to lib/src/resources/base_repository.dart

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:cloudmate/src/resources/local/user_local.dart';
12
import 'package:dio/dio.dart' as diox;
23
import 'dart:convert' as convert;
34
import 'dart:async';
@@ -7,8 +8,8 @@ import 'package:cloudmate/src/configs/application.dart';
78
class BaseRepository {
89
var dio = diox.Dio(diox.BaseOptions(
910
baseUrl: Application.baseUrl!,
10-
connectTimeout: 5000,
11-
receiveTimeout: 3000,
11+
connectTimeout: 10000,
12+
receiveTimeout: 10000,
1213
)); // with default Options
1314

1415
Future<diox.Response<dynamic>> downloadFile(
@@ -47,8 +48,10 @@ class BaseRepository {
4748
var response = await dio.post(
4849
gateway,
4950
data: convert.jsonEncode(body),
51+
options: getOptions(),
5052
);
5153
printEndpoint('POST', gateway);
54+
printResponse(response);
5255
return response;
5356
}
5457

@@ -65,9 +68,12 @@ class BaseRepository {
6568
return response;
6669
}
6770

68-
Future<diox.Response<dynamic>> getRoute(String gateway, String params) async {
71+
Future<diox.Response<dynamic>> getRoute(
72+
String gateway, {
73+
String? params,
74+
}) async {
6975
var response = await dio.get(
70-
gateway + params,
76+
gateway + (params ?? ''),
7177
options: getOptions(),
7278
);
7379
printEndpoint('GET', gateway);
@@ -91,7 +97,7 @@ class BaseRepository {
9197
diox.Options getOptions() {
9298
return diox.Options(
9399
headers: {
94-
'Authorization': 'Bearer ',
100+
'Authorization': 'Bearer ' + UserLocal().getAccessToken(),
95101
'Content-Type': 'application/json; charset=UTF-8',
96102
'Connection': 'keep-alive',
97103
'Accept': '*/*',
@@ -106,6 +112,6 @@ class BaseRepository {
106112

107113
printResponse(diox.Response<dynamic> response) {
108114
print('StatusCode: ${response.statusCode}');
109-
print('Body: ${convert.jsonDecode(response.data)}');
115+
// print('Body: ${response.data.toString()}');
110116
}
111117
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:get_storage/get_storage.dart';
2+
3+
class UserLocal {
4+
final _getStorage = GetStorage();
5+
final storageKey = 'token';
6+
7+
String getAccessToken() {
8+
return _getStorage.read(storageKey) ?? '';
9+
}
10+
11+
void saveAccessToken(String token) async {
12+
_getStorage.write(storageKey, token);
13+
}
14+
}

0 commit comments

Comments
 (0)