-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
24 changed files
with
1,120 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,38 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
|
||
import '../screens/home_screen.dart'; | ||
import '../screens/submit_request_screen.dart'; | ||
import '../screens/auth/login_screen.dart'; | ||
import '../screens/auth/register_screen.dart'; | ||
import '../screens/home/request_list_screen.dart'; | ||
import '../screens/home/submit_request_screen.dart'; | ||
|
||
Route<dynamic> onGenerate(RouteSettings settings) { | ||
switch (settings.name) { | ||
case '/': | ||
case '/request-list': | ||
return CupertinoPageRoute( | ||
builder: (_) => const RequestListScreen(), | ||
settings: settings, | ||
builder: (_) => const HomeScreen(), | ||
); | ||
case '/request-quote': | ||
return CupertinoPageRoute( | ||
settings: settings, | ||
builder: (BuildContext context) => const RequestQuoteScreen(), | ||
); | ||
case '/login': | ||
return CupertinoPageRoute( | ||
settings: settings, | ||
builder: (BuildContext context) => const LoginScreen(), | ||
); | ||
case '/register': | ||
return CupertinoPageRoute( | ||
settings: settings, | ||
builder: (BuildContext context) => const RegisterScreen(), | ||
); | ||
default: | ||
return CupertinoPageRoute( | ||
settings: settings, | ||
builder: (_) => const CupertinoPageScaffold(child: SizedBox()), | ||
builder: (_) => const CupertinoPageScaffold( | ||
child: Center(child: Text('Not Found')), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
class UserModel { | ||
final String? uuid; | ||
final String email; | ||
final String? password; | ||
final String fullname; | ||
final String? address; | ||
|
||
UserModel({ | ||
this.uuid, | ||
this.password, | ||
this.address, | ||
required this.email, | ||
required this.fullname, | ||
}); | ||
|
||
factory UserModel.fromJson(Map<String, dynamic> json) { | ||
return UserModel( | ||
uuid: json['id'], | ||
email: json['email'], | ||
fullname: json['user_metadata']['fullname'], | ||
address: json['user_metadata']['address'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'package:quote_request_app/providers/auth/auth_state.dart'; | ||
import 'package:quote_request_app/repositories/auth_repository.dart'; | ||
import 'package:riverpod/riverpod.dart'; | ||
|
||
class AuthNotifier extends StateNotifier<AuthState> { | ||
AuthNotifier(this._authRepository) : super(AuthInitial()); | ||
|
||
final AuthRepository _authRepository; | ||
|
||
Future<void> login(Map<String, dynamic> credentials) async { | ||
state = AuthLoading(); | ||
try { | ||
final user = await _authRepository.login( | ||
credentials['email'], | ||
credentials['password'], | ||
); | ||
state = AuthSuccess(user); | ||
} catch (e) { | ||
state = AuthError(e.toString()); | ||
} | ||
} | ||
|
||
Future<void> register(Map<String, dynamic> userData) async { | ||
state = AuthLoading(); | ||
try { | ||
final user = await _authRepository.register(userData); | ||
state = AuthSuccess(user); | ||
} catch (e) { | ||
state = AuthError(e.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
import '../../models/user_model.dart'; | ||
|
||
abstract class AuthState extends Equatable { | ||
const AuthState(); | ||
} | ||
|
||
class AuthInitial extends AuthState { | ||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class AuthLoading extends AuthState { | ||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class AuthSuccess extends AuthState { | ||
const AuthSuccess(this.user); | ||
|
||
final UserModel user; | ||
|
||
@override | ||
List<Object> get props => [user]; | ||
} | ||
|
||
class AuthError extends AuthState { | ||
final String message; | ||
|
||
const AuthError(this.message); | ||
|
||
@override | ||
List<Object> get props => [message]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
|
||
import '../models/user_model.dart'; | ||
|
||
abstract class AuthRepository { | ||
Future<UserModel> login(String email, String password); | ||
Future<UserModel> register(Map<String, dynamic> userData); | ||
} | ||
|
||
class AuthRepositoryImpl implements AuthRepository { | ||
final Dio _dio; | ||
|
||
AuthRepositoryImpl(this._dio); | ||
|
||
@override | ||
Future<UserModel> login(String email, String password) async { | ||
try { | ||
final response = await _dio.post( | ||
'/auth/v1/token', | ||
queryParameters: {'grant_type': 'password'}, | ||
data: {'email': email, 'password': password}, | ||
); | ||
|
||
if (response.statusCode == 200) { | ||
return UserModel.fromJson(response.data['user']); | ||
} else { | ||
throw Exception('An error occurred'); | ||
} | ||
} on DioError catch (e) { | ||
throw await Future.error(e.response!.data['error_description']); | ||
} catch (e) { | ||
debugPrint('AuthRepositoryImpl.login: $e'); | ||
throw await Future.error("Couldn't login. Is the device online?"); | ||
} | ||
} | ||
|
||
@override | ||
Future<UserModel> register(Map<String, dynamic> userData) async { | ||
try { | ||
final response = await _dio.post('/auth/v1/signup', data: userData); | ||
return UserModel.fromJson(response.data); | ||
} on DioError catch (e) { | ||
if (e.response!.statusCode == 400) { | ||
throw await Future.error(e.response!.data['error_description']); | ||
} else { | ||
throw await Future.error(e.message); | ||
} | ||
} catch (e, stack) { | ||
debugPrint('Error: $e\nStack : $stack'); | ||
throw await Future.error("Couldn't register. Is the device online?"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.