2021-08-21 22:38:25 -04:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
2021-08-25 00:43:51 -04:00
|
|
|
import 'package:flutter_web_auth/flutter_web_auth.dart';
|
2021-11-14 23:18:43 -05:00
|
|
|
import 'package:frosty/api/twitch_api.dart';
|
2022-01-07 21:56:45 -05:00
|
|
|
import 'package:frosty/constants/constants.dart';
|
2021-12-21 23:20:38 -05:00
|
|
|
import 'package:frosty/core/user/user_store.dart';
|
2021-11-14 23:18:43 -05:00
|
|
|
import 'package:mobx/mobx.dart';
|
2021-08-21 22:38:25 -04:00
|
|
|
|
2021-10-01 00:08:19 -04:00
|
|
|
part 'auth_store.g.dart';
|
|
|
|
|
|
2021-10-03 22:13:36 -04:00
|
|
|
class AuthStore = _AuthBase with _$AuthStore;
|
2021-09-06 18:59:33 -04:00
|
|
|
|
2021-10-03 22:13:36 -04:00
|
|
|
abstract class _AuthBase with Store {
|
2021-10-05 03:08:38 -04:00
|
|
|
/// Secure storage to store tokens.
|
2022-01-25 01:02:08 -05:00
|
|
|
static const _storage = FlutterSecureStorage();
|
2021-09-06 18:59:33 -04:00
|
|
|
|
2022-01-25 02:07:08 -05:00
|
|
|
/// The shared_preferences key for the default token.
|
|
|
|
|
static const _defaultTokenKey = 'default_token';
|
|
|
|
|
|
|
|
|
|
/// The shared_preferences key for the user token.
|
|
|
|
|
static const _userTokenKey = 'user_token';
|
|
|
|
|
|
2022-01-14 14:56:54 -05:00
|
|
|
final TwitchApi twitchApi;
|
|
|
|
|
|
2021-10-16 04:01:09 -04:00
|
|
|
/// Whether the token is valid or not.
|
|
|
|
|
var _tokenIsValid = false;
|
|
|
|
|
|
2021-12-21 23:20:38 -05:00
|
|
|
/// The store containing information relevant to the current user.
|
2022-01-14 14:56:54 -05:00
|
|
|
final UserStore user;
|
2021-12-21 23:20:38 -05:00
|
|
|
|
2021-10-17 16:26:02 -04:00
|
|
|
/// The current token.
|
|
|
|
|
@readonly
|
|
|
|
|
String? _token;
|
|
|
|
|
|
2021-10-05 03:08:38 -04:00
|
|
|
/// Whether the user is logged in or not.
|
2021-10-11 23:01:40 -04:00
|
|
|
@readonly
|
2021-10-03 22:13:36 -04:00
|
|
|
var _isLoggedIn = false;
|
2021-08-21 22:38:25 -04:00
|
|
|
|
2021-10-05 03:08:38 -04:00
|
|
|
/// Authentication headers for Twitch API requests.
|
2021-10-03 22:13:36 -04:00
|
|
|
@computed
|
|
|
|
|
Map<String, String> get headersTwitch => {'Authorization': 'Bearer $_token', 'Client-Id': clientId};
|
2021-08-21 22:38:25 -04:00
|
|
|
|
2022-01-15 16:51:11 -05:00
|
|
|
/// Error flag that will be non-null if login failed.
|
|
|
|
|
@readonly
|
|
|
|
|
String? _error;
|
|
|
|
|
|
2022-01-14 14:56:54 -05:00
|
|
|
_AuthBase({required this.twitchApi}) : user = UserStore(twitchApi: twitchApi);
|
|
|
|
|
|
2021-08-21 22:38:25 -04:00
|
|
|
/// Initialize by retrieving a token if it does not already exist.
|
2021-10-03 22:13:36 -04:00
|
|
|
@action
|
2021-08-24 09:58:31 -04:00
|
|
|
Future<void> init() async {
|
2022-01-15 16:51:11 -05:00
|
|
|
try {
|
|
|
|
|
// Read and set the currently stored user token, if any.
|
2022-01-25 02:07:08 -05:00
|
|
|
_token = await _storage.read(key: _userTokenKey);
|
2021-08-25 00:43:51 -04:00
|
|
|
|
2022-01-15 16:51:11 -05:00
|
|
|
// If the token does not exist, get the default token.
|
|
|
|
|
// Otherwise, log in.
|
2021-10-03 22:13:36 -04:00
|
|
|
if (_token == null) {
|
2022-01-15 16:51:11 -05:00
|
|
|
// Retrieve the currently stored default token if it exists.
|
2022-01-25 02:07:08 -05:00
|
|
|
_token = await _storage.read(key: _defaultTokenKey);
|
2022-01-15 16:51:11 -05:00
|
|
|
// If the token does not exist, get a new token and store it.
|
|
|
|
|
if (_token == null) {
|
|
|
|
|
_token = await twitchApi.getDefaultToken();
|
2022-01-25 02:07:08 -05:00
|
|
|
await _storage.write(key: _defaultTokenKey, value: _token);
|
2022-01-15 16:51:11 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Initialize the user store.
|
|
|
|
|
await user.init(headers: headersTwitch);
|
|
|
|
|
|
|
|
|
|
if (user.details != null) _isLoggedIn = true;
|
2021-10-03 22:13:36 -04:00
|
|
|
}
|
2021-12-11 00:45:44 -05:00
|
|
|
|
2022-01-15 16:51:11 -05:00
|
|
|
// Validate the token.
|
|
|
|
|
_tokenIsValid = await twitchApi.validateToken(token: _token!);
|
2021-08-24 09:58:31 -04:00
|
|
|
|
2022-01-15 16:51:11 -05:00
|
|
|
// If the token is invalid, logout.
|
2022-01-25 01:02:08 -05:00
|
|
|
if (!_tokenIsValid) await logout();
|
2021-09-06 18:59:33 -04:00
|
|
|
|
2022-01-15 16:51:11 -05:00
|
|
|
_error = null;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint(e.toString());
|
|
|
|
|
_error = e.toString();
|
2021-08-21 22:38:25 -04:00
|
|
|
}
|
2021-08-25 00:43:51 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-05 03:08:38 -04:00
|
|
|
/// Initiates the OAuth sign-in process and updates fields accordingly upon successful login.
|
2021-10-03 22:13:36 -04:00
|
|
|
@action
|
2022-01-08 22:48:28 -05:00
|
|
|
Future<void> login({String? customToken}) async {
|
2021-08-25 00:43:51 -04:00
|
|
|
try {
|
2022-01-08 22:48:28 -05:00
|
|
|
if (customToken == null) {
|
|
|
|
|
// Create the OAuth sign-in URI.
|
|
|
|
|
final loginUrl = Uri(
|
|
|
|
|
scheme: 'https',
|
|
|
|
|
host: 'id.twitch.tv',
|
|
|
|
|
path: '/oauth2/authorize',
|
|
|
|
|
queryParameters: {
|
|
|
|
|
'client_id': clientId,
|
|
|
|
|
'redirect_uri': 'auth://',
|
|
|
|
|
'response_type': 'token',
|
|
|
|
|
'scope': 'chat:read chat:edit user:read:follows user:read:blocked_users user:manage:blocked_users',
|
|
|
|
|
'force_verify': 'true',
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Retrieve the OAuth redirect URI.
|
|
|
|
|
final result = await FlutterWebAuth.authenticate(
|
|
|
|
|
url: loginUrl.toString(),
|
|
|
|
|
callbackUrlScheme: 'auth',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Parse the user token from the redirect URI fragment.
|
2022-01-19 13:18:35 -05:00
|
|
|
final url = Uri.parse(result.replaceFirst('#', '?'));
|
|
|
|
|
_token = url.queryParameters['access_token'];
|
2022-01-08 22:48:28 -05:00
|
|
|
} else {
|
2022-02-01 23:34:55 -05:00
|
|
|
// Validate the custom token.
|
|
|
|
|
_tokenIsValid = await twitchApi.validateToken(token: customToken);
|
|
|
|
|
if (!_tokenIsValid) return;
|
|
|
|
|
|
|
|
|
|
// Replace the current default token with the new custom token.
|
2022-01-08 22:48:28 -05:00
|
|
|
_token = customToken;
|
|
|
|
|
}
|
2021-08-25 00:43:51 -04:00
|
|
|
|
2021-10-05 03:08:38 -04:00
|
|
|
// Store the user token.
|
2022-01-25 02:07:08 -05:00
|
|
|
await _storage.write(key: _userTokenKey, value: customToken ?? _token);
|
2021-08-25 00:43:51 -04:00
|
|
|
|
2021-12-21 23:20:38 -05:00
|
|
|
// Initialize the user with the new token.
|
2021-12-29 18:11:39 -05:00
|
|
|
await user.init(headers: headersTwitch);
|
2021-12-11 00:45:44 -05:00
|
|
|
|
2021-10-05 03:08:38 -04:00
|
|
|
// Set the login status to logged in.
|
2021-10-03 22:13:36 -04:00
|
|
|
_isLoggedIn = true;
|
2021-08-25 00:43:51 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
debugPrint('Login failed due to $error');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 03:08:38 -04:00
|
|
|
/// Logs out the current user and updates fields accordingly.
|
2021-10-03 22:13:36 -04:00
|
|
|
@action
|
|
|
|
|
Future<void> logout() async {
|
2022-01-15 16:51:11 -05:00
|
|
|
try {
|
2022-02-01 23:34:55 -05:00
|
|
|
// Delete the existing user token.
|
2022-01-25 02:07:08 -05:00
|
|
|
await _storage.delete(key: _userTokenKey);
|
2022-01-15 16:51:11 -05:00
|
|
|
_token = null;
|
2021-10-03 22:13:36 -04:00
|
|
|
|
2022-01-15 16:51:11 -05:00
|
|
|
// Clear the user info.
|
|
|
|
|
user.dispose();
|
2021-10-05 00:44:46 -04:00
|
|
|
|
2022-01-15 16:51:11 -05:00
|
|
|
// If the default token already exists, set it.
|
2022-01-25 02:07:08 -05:00
|
|
|
_token = await _storage.read(key: _defaultTokenKey);
|
2021-10-17 16:26:02 -04:00
|
|
|
|
2022-01-15 16:51:11 -05:00
|
|
|
// If the default token does not already exist, get the new default token and store it.
|
|
|
|
|
// Else, validate the existing token.
|
|
|
|
|
if (_token == null) {
|
2022-01-14 14:56:54 -05:00
|
|
|
_token = await twitchApi.getDefaultToken();
|
2022-01-25 02:07:08 -05:00
|
|
|
await _storage.write(key: _defaultTokenKey, value: _token);
|
2022-01-15 16:51:11 -05:00
|
|
|
} else {
|
|
|
|
|
// Validate the stored token.
|
|
|
|
|
_tokenIsValid = await twitchApi.validateToken(token: _token!);
|
|
|
|
|
|
|
|
|
|
// If the stored token is invalid, get a new default token and store it.
|
|
|
|
|
if (!_tokenIsValid) {
|
|
|
|
|
_token = await twitchApi.getDefaultToken();
|
2022-01-25 02:07:08 -05:00
|
|
|
await _storage.write(key: _defaultTokenKey, value: _token);
|
2022-01-15 16:51:11 -05:00
|
|
|
}
|
2021-10-17 16:26:02 -04:00
|
|
|
}
|
2022-01-25 01:02:08 -05:00
|
|
|
|
2022-01-18 21:41:29 -05:00
|
|
|
// Set the login status to logged out.
|
|
|
|
|
_isLoggedIn = false;
|
|
|
|
|
|
|
|
|
|
debugPrint('Successfully logged out');
|
2022-01-15 16:51:11 -05:00
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint(e.toString());
|
2021-10-17 16:26:02 -04:00
|
|
|
}
|
2021-08-21 22:38:25 -04:00
|
|
|
}
|
|
|
|
|
}
|