-
Notifications
You must be signed in to change notification settings - Fork 0
Adding authentication for users #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| class Constants { | ||
|
|
||
| static const url = 'https://shop-app-4ed38-default-rtdb.firebaseio.com'; | ||
| // String authUrl = | ||
| // 'https://identitytoolkit.googleapis.com/v1/accounts:$urlSegment?key=AIzaSyDDS9QGht-1JD8bUjcqP-0BQ0cgibbacAQ'; | ||
| // static const signInUrl = 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyDDS9QGht-1JD8bUjcqP-0BQ0cgibbacAQ'; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:flutter/widgets.dart'; | ||
| import 'package:http/http.dart' as http; | ||
| import 'package:myshop_app/models/http_exception.dart'; | ||
|
|
||
| class Auth with ChangeNotifier { | ||
| String? _token; | ||
| DateTime? _expiryDate; | ||
| String? _userId; | ||
| Timer? _authTimer; | ||
|
|
||
| bool get isAuth { | ||
| return token != null; | ||
| } | ||
|
|
||
| String? get token { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. document this method |
||
| if (_expiryDate != null && _expiryDate!.isAfter(DateTime.now()) && _token != null) { | ||
| return _token; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| String? get userId { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. document |
||
| return _userId; | ||
| } | ||
|
|
||
| Future<void> _authenticate(String? email, String? password, String urlSegment) async { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| final url = Uri.parse( | ||
| 'https://identitytoolkit.googleapis.com/v1/accounts:$urlSegment?key=AIzaSyDDS9QGht-1JD8bUjcqP-0BQ0cgibbacAQ'); | ||
|
|
||
| try { | ||
| final response = await http.post( | ||
| url, | ||
| body: json.encode( | ||
| { | ||
| 'email': email, | ||
| 'password': password, | ||
| 'returnSecureToken': true, | ||
| }, | ||
| ), | ||
| ); | ||
| final dynamic responseData = json.decode(response.body); | ||
| if (responseData['error'] != null) { | ||
| throw HttpException((responseData['error']['message']).toString()); | ||
| } | ||
| _token = responseData['idToken'].toString(); | ||
| _userId = responseData['localId'].toString(); | ||
| _expiryDate = DateTime.now().add( | ||
| Duration( | ||
| seconds: int.parse( | ||
| responseData['expiresIn'].toString(), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| notifyListeners(); | ||
| } catch (error) { | ||
| rethrow; | ||
| } | ||
| } | ||
|
|
||
| Future<void> signUp(String? email, String? password) async { | ||
| return _authenticate(email, password, 'signUp'); | ||
| } | ||
|
|
||
| Future<void> login(String? email, String? password) async { | ||
| return _authenticate(email, password, 'signInWithPassword'); | ||
| } | ||
|
|
||
| void logout() { | ||
| _token = null; | ||
| _userId = null; | ||
| _expiryDate = null; | ||
| if (_authTimer != null) { | ||
| _authTimer?.cancel(); | ||
| _authTimer = null; | ||
| } | ||
| notifyListeners(); | ||
| } | ||
|
Comment on lines
+64
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please document all methods |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,23 +3,23 @@ import 'package:flutter/foundation.dart'; | |
| class CartItem { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. document this class and its methods |
||
| final String id; | ||
| final String title; | ||
| final int quantity; | ||
| final double price; | ||
| final int? quantity; | ||
| final double? price; | ||
|
|
||
| CartItem({ | ||
| @required this.id, | ||
| @required this.title, | ||
| @required this.quantity, | ||
| @required this.price, | ||
| required this.id, | ||
| required this.title, | ||
| required this.quantity, | ||
| required this.price, | ||
| }); | ||
| } | ||
|
|
||
| class Cart with ChangeNotifier { | ||
| ///Map for cart items | ||
| Map<String, CartItem> _items = {}; | ||
| Map<String?, CartItem> _items = {}; | ||
|
|
||
| ///getter of _items | ||
| Map<String, CartItem> get items => {..._items}; | ||
| Map<String?, CartItem> get items => {..._items}; | ||
|
|
||
| ///Counts the number of _items in the cart | ||
| int get itemCount => _items.length; | ||
|
|
@@ -28,15 +28,15 @@ class Cart with ChangeNotifier { | |
| double get totalAmount { | ||
| var total = 0.0; | ||
| _items.forEach((key, cartItem) { | ||
| total += cartItem.price * cartItem.quantity; | ||
| total += cartItem.price! * cartItem.quantity!; | ||
| }); | ||
| return total; | ||
| } | ||
|
|
||
| ///Function for adding items in the cart | ||
| void addItem( | ||
| String productId, | ||
| double price, | ||
| String? productId, | ||
| double? price, | ||
| String title, | ||
| ) { | ||
| if (_items.containsKey(productId)) { | ||
|
|
@@ -48,7 +48,7 @@ class Cart with ChangeNotifier { | |
| id: existingCartItem.id, | ||
| title: existingCartItem.title, | ||
| price: existingCartItem.price, | ||
| quantity: existingCartItem.quantity + 1, | ||
| quantity: existingCartItem.quantity! + 1, | ||
| )); | ||
| } else { | ||
| ///if there is no existing productId, a new item will be added | ||
|
|
@@ -68,25 +68,25 @@ class Cart with ChangeNotifier { | |
| } | ||
|
|
||
| ///Removes an item in the cart | ||
| void removeItem(String productId) { | ||
| void removeItem(String? productId) { | ||
| _items.remove(productId); | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| ///This function is used by the snackbar when the UNDO button is pressed | ||
| void removeSingleItem(String productId) { | ||
| void removeSingleItem(String? productId) { | ||
| if (!_items.containsKey(productId)) { | ||
| ///If the productId is not present in the cart items, it will return nothing | ||
| return; | ||
| } | ||
| if (_items[productId].quantity > 1) { | ||
| if (_items[productId]!.quantity! > 1) { | ||
| ///If the productId is present in the cart items, when the UNDO button is pressed, it will deduct the recently added item | ||
| _items.update(productId, (existingCartItem) { | ||
| return CartItem( | ||
| id: existingCartItem.id, | ||
| title: existingCartItem.title, | ||
| price: existingCartItem.price, | ||
| quantity: existingCartItem.quantity - 1, | ||
| quantity: existingCartItem.quantity! - 1, | ||
| ); | ||
| }); | ||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please put short description