New sign in page

This commit is contained in:
LittleSheep 2024-04-21 20:55:17 +08:00
parent eb9a24582b
commit bef3221e2f
7 changed files with 122 additions and 51 deletions

View File

@ -8,9 +8,14 @@
"signInRequired": "Sign in required", "signInRequired": "Sign in required",
"signUp": "Sign Up", "signUp": "Sign Up",
"signUpCaption": "Create an account on Solarpass and then get the access of entire Solar Networks!", "signUpCaption": "Create an account on Solarpass and then get the access of entire Solar Networks!",
"poweredBy": "Powered by Project Hydrogen",
"copyright": "Copyright © 2024 Solsynth LLC",
"confirmation": "Confirmation", "confirmation": "Confirmation",
"confirmCancel": "Not sure", "confirmCancel": "Not sure",
"confirmOkay": "OK", "confirmOkay": "OK",
"username": "Username",
"password": "Password",
"next": "Next",
"edit": "Edit", "edit": "Edit",
"apply": "Apply", "apply": "Apply",
"delete": "Delete", "delete": "Delete",

View File

@ -8,9 +8,14 @@
"signInRequired": "请先登陆", "signInRequired": "请先登陆",
"signUp": "注册", "signUp": "注册",
"signUpCaption": "在 Solarpass 注册一个账号以获得整个 Solar Networks 的存取权!", "signUpCaption": "在 Solarpass 注册一个账号以获得整个 Solar Networks 的存取权!",
"poweredBy": "由 Project Hydrogen 强力驱动",
"copyright": "2024 Solsynth LLC © 版权所有",
"confirmation": "确认", "confirmation": "确认",
"confirmCancel": "不太确定", "confirmCancel": "不太确定",
"confirmOkay": "确定", "confirmOkay": "确定",
"username": "用户名",
"password": "密码",
"next": "下一步",
"edit": "编辑", "edit": "编辑",
"delete": "删除", "delete": "删除",
"action": "操作", "action": "操作",

View File

@ -2,16 +2,13 @@ import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:solian/screens/auth.dart';
import 'package:oauth2/oauth2.dart' as oauth2; import 'package:oauth2/oauth2.dart' as oauth2;
import 'package:solian/utils/service_url.dart'; import 'package:solian/utils/service_url.dart';
class AuthProvider { class AuthProvider {
AuthProvider(); AuthProvider();
final deviceEndpoint = final deviceEndpoint = getRequestUri('passport', '/api/notifications/subscribe');
getRequestUri('passport', '/api/notifications/subscribe');
final authorizationEndpoint = getRequestUri('passport', '/auth/o/connect');
final tokenEndpoint = getRequestUri('passport', '/api/auth/token'); final tokenEndpoint = getRequestUri('passport', '/api/auth/token');
final userinfoEndpoint = getRequestUri('passport', '/api/users/me'); final userinfoEndpoint = getRequestUri('passport', '/api/users/me');
final redirectUrl = Uri.parse('solian://auth'); final redirectUrl = Uri.parse('solian://auth');
@ -32,14 +29,12 @@ class AuthProvider {
Future<bool> pickClient() async { Future<bool> pickClient() async {
if (await storage.containsKey(key: storageKey)) { if (await storage.containsKey(key: storageKey)) {
try { try {
final credentials = final credentials = oauth2.Credentials.fromJson((await storage.read(key: storageKey))!);
oauth2.Credentials.fromJson((await storage.read(key: storageKey))!); client = oauth2.Client(credentials, identifier: clientId, secret: clientSecret);
client = oauth2.Client(credentials,
identifier: clientId, secret: clientSecret);
await fetchProfiles(); await fetchProfiles();
return true; return true;
} catch (e) { } catch (e) {
signOff(); signoff();
return false; return false;
} }
} else { } else {
@ -47,31 +42,20 @@ class AuthProvider {
} }
} }
Future<oauth2.Client> createClient(BuildContext context) async { Future<oauth2.Client> createClient(BuildContext context, String username, String password) async {
// If logged in
if (await pickClient()) { if (await pickClient()) {
return client!; return client!;
} }
var grant = oauth2.AuthorizationCodeGrant( return await oauth2.resourceOwnerPasswordGrant(
clientId,
authorizationEndpoint,
tokenEndpoint, tokenEndpoint,
username,
password,
identifier: clientId,
secret: clientSecret, secret: clientSecret,
scopes: ["openid"],
basicAuth: false, basicAuth: false,
); );
var authorizationUrl =
grant.getAuthorizationUrl(redirectUrl, scopes: ["openid"]);
var responseUrl = await Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute(
builder: (context) => AuthorizationScreen(authorizationUrl),
),
);
var responseUri = Uri.parse(responseUrl);
return await grant.handleAuthorizationResponse(responseUri.queryParameters);
} }
Future<void> fetchProfiles() async { Future<void> fetchProfiles() async {
@ -83,22 +67,21 @@ class AuthProvider {
Future<void> refreshToken() async { Future<void> refreshToken() async {
if (client != null) { if (client != null) {
final credentials = await client!.credentials.refresh( final credentials =
identifier: clientId, secret: clientSecret, basicAuth: false); await client!.credentials.refresh(identifier: clientId, secret: clientSecret, basicAuth: false);
client = oauth2.Client(credentials, client = oauth2.Client(credentials, identifier: clientId, secret: clientSecret);
identifier: clientId, secret: clientSecret);
storage.write(key: storageKey, value: credentials.toJson()); storage.write(key: storageKey, value: credentials.toJson());
} }
} }
Future<void> signIn(BuildContext context) async { Future<void> signin(BuildContext context, String username, String password) async {
client = await createClient(context); client = await createClient(context, username, password);
storage.write(key: storageKey, value: client!.credentials.toJson()); storage.write(key: storageKey, value: client!.credentials.toJson());
await fetchProfiles(); await fetchProfiles();
} }
void signOff() { void signoff() {
storage.delete(key: profileKey); storage.delete(key: profileKey);
storage.delete(key: storageKey); storage.delete(key: storageKey);
} }
@ -109,10 +92,7 @@ class AuthProvider {
if (client == null) { if (client == null) {
await pickClient(); await pickClient();
} }
if (lastRefreshedAt == null || if (lastRefreshedAt == null || DateTime.now().subtract(const Duration(minutes: 3)).isAfter(lastRefreshedAt!)) {
DateTime.now()
.subtract(const Duration(minutes: 3))
.isAfter(lastRefreshedAt!)) {
await refreshToken(); await refreshToken();
lastRefreshedAt = DateTime.now(); lastRefreshedAt = DateTime.now();
} }

View File

@ -8,6 +8,7 @@ import 'package:solian/screens/explore.dart';
import 'package:solian/screens/posts/comment_editor.dart'; import 'package:solian/screens/posts/comment_editor.dart';
import 'package:solian/screens/posts/moment_editor.dart'; import 'package:solian/screens/posts/moment_editor.dart';
import 'package:solian/screens/posts/screen.dart'; import 'package:solian/screens/posts/screen.dart';
import 'package:solian/screens/signin.dart';
import 'package:solian/widgets/chat/channel_editor.dart'; import 'package:solian/widgets/chat/channel_editor.dart';
final router = GoRouter( final router = GoRouter(
@ -58,5 +59,10 @@ final router = GoRouter(
dataset: state.pathParameters['dataset'] as String, dataset: state.pathParameters['dataset'] as String,
), ),
), ),
GoRoute(
path: '/auth/sign-in',
name: 'auth.sign-in',
builder: (context, state) => SignInScreen(),
),
], ],
); );

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:solian/providers/auth.dart'; import 'package:solian/providers/auth.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:solian/router.dart';
import 'package:solian/utils/service_url.dart'; import 'package:solian/utils/service_url.dart';
import 'package:solian/widgets/common_wrapper.dart'; import 'package:solian/widgets/common_wrapper.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
@ -48,7 +49,7 @@ class _AccountScreenState extends State<AccountScreen> {
), ),
), ),
onTap: () { onTap: () {
auth.signOff(); auth.signoff();
setState(() { setState(() {
isAuthorized = false; isAuthorized = false;
}); });
@ -65,19 +66,19 @@ class _AccountScreenState extends State<AccountScreen> {
title: AppLocalizations.of(context)!.signIn, title: AppLocalizations.of(context)!.signIn,
caption: AppLocalizations.of(context)!.signInCaption, caption: AppLocalizations.of(context)!.signInCaption,
onTap: () { onTap: () {
auth.signIn(context).then((_) { router.pushNamed('auth.sign-in').then((did) {
auth.isAuthorized().then((val) { auth.isAuthorized().then((value) {
setState(() => isAuthorized = val); setState(() => isAuthorized = value);
}); });
}); });
}, },
), ),
ActionCard( ActionCard(
icon: const Icon(Icons.plus_one, color: Colors.white), icon: const Icon(Icons.add, color: Colors.white),
title: AppLocalizations.of(context)!.signUp, title: AppLocalizations.of(context)!.signUp,
caption: AppLocalizations.of(context)!.signUpCaption, caption: AppLocalizations.of(context)!.signUpCaption,
onTap: () { onTap: () {
launchUrl(getRequestUri('passport', '/auth/sign-up')); launchUrl(getRequestUri('passport', '/sign-up'));
}, },
), ),
], ],

74
lib/screens/signin.dart Normal file
View File

@ -0,0 +1,74 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
import 'package:solian/widgets/indent_wrapper.dart';
class SignInScreen extends StatelessWidget {
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
SignInScreen({super.key});
@override
Widget build(BuildContext context) {
final auth = context.read<AuthProvider>();
return IndentWrapper(
title: AppLocalizations.of(context)!.signIn,
hideDrawer: true,
child: Center(
child: Container(
width: MediaQuery.of(context).size.width * 0.6,
constraints: const BoxConstraints(maxWidth: 360),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
autocorrect: false,
enableSuggestions: false,
controller: _usernameController,
decoration: InputDecoration(
isDense: true,
border: const UnderlineInputBorder(),
hintText: AppLocalizations.of(context)!.username,
),
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
),
const SizedBox(height: 12),
TextField(
obscureText: true,
autocorrect: false,
enableSuggestions: false,
controller: _passwordController,
decoration: InputDecoration(
isDense: true,
border: const UnderlineInputBorder(),
hintText: AppLocalizations.of(context)!.password,
),
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
),
const SizedBox(height: 16),
TextButton(
child: Text(AppLocalizations.of(context)!.next),
onPressed: () {
final username = _usernameController.value.text;
final password = _passwordController.value.text;
if (username.isEmpty || password.isEmpty) return;
auth.signin(context, username, password).then((_) {
router.pop(true);
}).catchError((e) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(e.toString()),
));
});
},
)
],
),
),
),
);
}
}

View File

@ -433,10 +433,10 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: intl name: intl
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d" sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.18.1" version: "0.19.0"
js: js:
dependency: transitive dependency: transitive
description: description:
@ -457,10 +457,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: leak_tracker name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa" sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "10.0.0" version: "10.0.5"
leak_tracker_flutter_testing: leak_tracker_flutter_testing:
dependency: transitive dependency: transitive
description: description:
@ -473,10 +473,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: leak_tracker_testing name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47 sha256: d4c8f568c60af6b6daa74c80fc04411765769882600f6bf9cd4b391c96de42ce
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.1" version: "2.0.3"
lints: lints:
dependency: transitive dependency: transitive
description: description:
@ -593,10 +593,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: meta name: meta
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 sha256: "25dfcaf170a0190f47ca6355bdd4552cb8924b430512ff0cafb8db9bd41fe33b"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.11.0" version: "1.14.0"
mime: mime:
dependency: transitive dependency: transitive
description: description: