first commit
This commit is contained in:
11
lib/auth/custom_auth/auth_util.dart
Normal file
11
lib/auth/custom_auth/auth_util.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
import 'custom_auth_manager.dart';
|
||||
|
||||
export 'custom_auth_manager.dart';
|
||||
|
||||
final _authManager = CustomAuthManager();
|
||||
CustomAuthManager get authManager => _authManager;
|
||||
|
||||
String get currentUserUid => currentUser?.uid ?? '';
|
||||
String? get currentAuthenticationToken => authManager.authenticationToken;
|
||||
String? get currentAuthRefreshToken => authManager.refreshToken;
|
||||
DateTime? get currentAuthTokenExpiration => authManager.tokenExpiration;
|
||||
135
lib/auth/custom_auth/custom_auth_manager.dart
Normal file
135
lib/auth/custom_auth/custom_auth_manager.dart
Normal file
@@ -0,0 +1,135 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'custom_auth_user_provider.dart';
|
||||
|
||||
export 'custom_auth_manager.dart';
|
||||
|
||||
const _kAuthTokenKey = '_auth_authentication_token_';
|
||||
const _kRefreshTokenKey = '_auth_refresh_token_';
|
||||
const _kTokenExpirationKey = '_auth_token_expiration_';
|
||||
const _kUidKey = '_auth_uid_';
|
||||
const _kUserDataKey = '_auth_user_data_';
|
||||
|
||||
class CustomAuthManager {
|
||||
// Auth session attributes
|
||||
String? authenticationToken;
|
||||
String? refreshToken;
|
||||
DateTime? tokenExpiration;
|
||||
// User attributes
|
||||
String? uid;
|
||||
|
||||
Future signOut() async {
|
||||
authenticationToken = null;
|
||||
refreshToken = null;
|
||||
tokenExpiration = null;
|
||||
uid = null;
|
||||
|
||||
// Update the current user.
|
||||
numstationAuthUserSubject.add(
|
||||
NumstationAuthUser(loggedIn: false),
|
||||
);
|
||||
persistAuthData();
|
||||
}
|
||||
|
||||
Future<NumstationAuthUser?> signIn({
|
||||
String? authenticationToken,
|
||||
String? refreshToken,
|
||||
DateTime? tokenExpiration,
|
||||
String? authUid,
|
||||
}) async =>
|
||||
_updateCurrentUser(
|
||||
authenticationToken: authenticationToken,
|
||||
refreshToken: refreshToken,
|
||||
tokenExpiration: tokenExpiration,
|
||||
authUid: authUid,
|
||||
);
|
||||
|
||||
void updateAuthUserData({
|
||||
String? authenticationToken,
|
||||
String? refreshToken,
|
||||
DateTime? tokenExpiration,
|
||||
String? authUid,
|
||||
}) {
|
||||
assert(
|
||||
currentUser?.loggedIn ?? false,
|
||||
'User must be logged in to update auth user data.',
|
||||
);
|
||||
|
||||
_updateCurrentUser(
|
||||
authenticationToken: authenticationToken,
|
||||
refreshToken: refreshToken,
|
||||
tokenExpiration: tokenExpiration,
|
||||
authUid: authUid,
|
||||
);
|
||||
}
|
||||
|
||||
NumstationAuthUser? _updateCurrentUser({
|
||||
String? authenticationToken,
|
||||
String? refreshToken,
|
||||
DateTime? tokenExpiration,
|
||||
String? authUid,
|
||||
}) {
|
||||
this.authenticationToken = authenticationToken;
|
||||
this.refreshToken = refreshToken;
|
||||
this.tokenExpiration = tokenExpiration;
|
||||
this.uid = authUid;
|
||||
|
||||
// Update the current user stream.
|
||||
final updatedUser = NumstationAuthUser(
|
||||
loggedIn: true,
|
||||
uid: authUid,
|
||||
);
|
||||
numstationAuthUserSubject.add(updatedUser);
|
||||
persistAuthData();
|
||||
return updatedUser;
|
||||
}
|
||||
|
||||
late SharedPreferences _prefs;
|
||||
Future initialize() async {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
|
||||
try {
|
||||
authenticationToken = _prefs.getString(_kAuthTokenKey);
|
||||
refreshToken = _prefs.getString(_kRefreshTokenKey);
|
||||
tokenExpiration = _prefs.getInt(_kTokenExpirationKey) != null
|
||||
? DateTime.fromMillisecondsSinceEpoch(
|
||||
_prefs.getInt(_kTokenExpirationKey)!)
|
||||
: null;
|
||||
uid = _prefs.getString(_kUidKey);
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error initializing auth: $e');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final authTokenExists = authenticationToken != null;
|
||||
final tokenExpired =
|
||||
tokenExpiration != null && tokenExpiration!.isBefore(DateTime.now());
|
||||
final updatedUser = NumstationAuthUser(
|
||||
loggedIn: authTokenExists && !tokenExpired,
|
||||
uid: uid,
|
||||
);
|
||||
numstationAuthUserSubject.add(updatedUser);
|
||||
}
|
||||
|
||||
void persistAuthData() {
|
||||
authenticationToken != null
|
||||
? _prefs.setString(_kAuthTokenKey, authenticationToken!)
|
||||
: _prefs.remove(_kAuthTokenKey);
|
||||
refreshToken != null
|
||||
? _prefs.setString(_kRefreshTokenKey, refreshToken!)
|
||||
: _prefs.remove(_kRefreshTokenKey);
|
||||
tokenExpiration != null
|
||||
? _prefs.setInt(
|
||||
_kTokenExpirationKey, tokenExpiration!.millisecondsSinceEpoch)
|
||||
: _prefs.remove(_kTokenExpirationKey);
|
||||
uid != null ? _prefs.setString(_kUidKey, uid!) : _prefs.remove(_kUidKey);
|
||||
}
|
||||
}
|
||||
|
||||
NumstationAuthUser? currentUser;
|
||||
bool get loggedIn => currentUser?.loggedIn ?? false;
|
||||
18
lib/auth/custom_auth/custom_auth_user_provider.dart
Normal file
18
lib/auth/custom_auth/custom_auth_user_provider.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import 'custom_auth_manager.dart';
|
||||
|
||||
class NumstationAuthUser {
|
||||
NumstationAuthUser({required this.loggedIn, this.uid});
|
||||
|
||||
bool loggedIn;
|
||||
String? uid;
|
||||
}
|
||||
|
||||
/// Generates a stream of the authenticated user.
|
||||
BehaviorSubject<NumstationAuthUser> numstationAuthUserSubject =
|
||||
BehaviorSubject.seeded(NumstationAuthUser(loggedIn: false));
|
||||
Stream<NumstationAuthUser> numstationAuthUserStream() =>
|
||||
numstationAuthUserSubject
|
||||
.asBroadcastStream()
|
||||
.map((user) => currentUser = user);
|
||||
Reference in New Issue
Block a user