https://github.com/iamchathu/flutter_auth_provider
Simple and extensible authentication manager for apps built with Flutter.
https://github.com/iamchathu/flutter_auth_provider
auth authentication dart flutter hacktoberfest
Last synced: 4 months ago
JSON representation
Simple and extensible authentication manager for apps built with Flutter.
- Host: GitHub
- URL: https://github.com/iamchathu/flutter_auth_provider
- Owner: iamchathu
- License: mit
- Created: 2021-05-29T19:50:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-25T19:48:57.000Z (almost 2 years ago)
- Last Synced: 2024-11-16T03:27:33.424Z (about 1 year ago)
- Topics: auth, authentication, dart, flutter, hacktoberfest
- Language: C++
- Homepage:
- Size: 141 KB
- Stars: 6
- Watchers: 6
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# flutter_auth_provider
Simple and extensible authentication manager for apps built with Flutter.
## Getting started
### Steps
* Add as a dependency.
* Implement the Stores.
* Connect the store to your views.
## Concepts
### 1. Stores
Stores are abstract classes that allow you to implement your custom persistence layer for
authentication related data.
You can implement your own authentication persistence logic by implementing the `Stores`.
* AuthStore - This is your user related data store. When you implement the AuthStore, you can also
use a custom type for a User specified as a generic.
* TokenStore - Implement how you store and refresh the token.
Example implementation with `flutter-secure-storage`.
```dart
import 'package:flutter_auth_provider/flutter_auth_provider.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class User {
string userName;
string role;
const User({ required this.role, required this.userName});
}
const String userNameKey = 'userName';
const String roleKey = 'name';
const String tokenKey = 'token';
const String refreshTokenKey = 'refreshToken';
class SecureStore implements AuthStore, TokenStore {
static SecureStore _instance = const SecureStore._();
final FlutterSecureStorage _storage = const FlutterSecureStorage();
const SecureStore._();
factory SecureStore() => _instance;
@override
Future delete() async {
await _storage.delete(key: userNameKey);
await _storage.delete(key: roleKey);
}
@override
Future retrieve() async {
final userName = await _storage.read(key: userNameKey);
final role = await _storage.read(key: roleKey);
if (userName != null && role != null) {
return User(userName: userName, role: role);
}
return null;
}
@override
Future save(User user) async {
await _storage.write(key: userNameKey, value: user.userName);
await _storage.write(key: roleKey, value: user.role);
}
@override
Future clear() async {
await _storage.delete(key: tokenKey);
await _storage.delete(key: refreshTokenKey);
}
@override
Future getRefreshToken() async {
return _storage.read(key: refreshTokenKey);
}
@override
Future getToken() async {
return _storage.read(key: tokenKey);
}
@override
Future saveTokens({required String token, String? refreshToken}) async {
await _storage.write(key: tokenKey, value: token);
if (refreshToken != null) {
await _storage.write(key: refreshTokenKey, value: refreshToken);
}
}
}
```
### 2. Listeners
There are two listeners available. These will execute your code upon Authentication events.
* LoginListener - Called when user is logged in.
* LogoutListener - Called when user logs out.
Examples:
* Setting up Sentry with user details.
* Remove/setup/release resources upon logging out.
## Contributors
* [Chathu Vishwajith](https://github.com/iamchathu)
* [Pasindu De Silva](https://github.com/pasindud)
* [Bhagya Nirmaan Silva](https://github.com/bhagyas)