✨ Sign in & Sign up redirection
This commit is contained in:
@ -1,4 +1,9 @@
|
||||
{
|
||||
"solian": "Solian",
|
||||
"explore": "Explore"
|
||||
"explore": "Explore",
|
||||
"account": "Account",
|
||||
"signIn": "Sign In",
|
||||
"signInCaption": "Sign in to create post, start a realm, message your friend and more!",
|
||||
"signUp": "Sign Up",
|
||||
"signUpCaption": "Create an account on Solarpass and then get the access of entire Solar Networks!"
|
||||
}
|
@ -1,4 +1,9 @@
|
||||
{
|
||||
"solian": "索链",
|
||||
"explore": "探索"
|
||||
"explore": "探索",
|
||||
"account": "账号",
|
||||
"signIn": "登陆",
|
||||
"signInCaption": "登陆以发表帖子、文章、创建领域、和你的朋友聊天,以及获取更多功能!",
|
||||
"signUp": "注册",
|
||||
"signUpCaption": "在 Solarpass 注册一个账号以获得整个 Solar Networks 的存取权!"
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/providers/layout_provider.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:solian/utils/timeago.dart';
|
||||
import 'package:solian/widgets/wrapper.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
void main() {
|
||||
@ -33,11 +32,9 @@ class SolianApp extends StatelessWidget {
|
||||
OverlayEntry(builder: (context) {
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
Provider(create: (_) => LayoutConfig(context))
|
||||
Provider(create: (_) => AuthProvider()),
|
||||
],
|
||||
child: LayoutWrapper(
|
||||
child: child,
|
||||
),
|
||||
child: child,
|
||||
);
|
||||
})
|
||||
],
|
||||
|
132
lib/providers/auth.dart
Executable file
132
lib/providers/auth.dart
Executable file
@ -0,0 +1,132 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.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:solian/utils/service_url.dart';
|
||||
|
||||
final authClient = AuthProvider();
|
||||
|
||||
class AuthProvider {
|
||||
AuthProvider();
|
||||
|
||||
final deviceEndpoint =
|
||||
getRequestUri('passport', '/api/notifications/subscribe');
|
||||
final authorizationEndpoint = getRequestUri('passport', '/auth/o/connect');
|
||||
final tokenEndpoint = getRequestUri('passport', '/api/auth/token');
|
||||
final userinfoEndpoint = getRequestUri('passport', '/api/users/me');
|
||||
final redirectUrl = Uri.parse('solian://auth');
|
||||
|
||||
static const clientId = "solian";
|
||||
static const clientSecret = "_F4%q2Eea3";
|
||||
|
||||
static const storage = FlutterSecureStorage();
|
||||
static const storageKey = "identity";
|
||||
static const profileKey = "profiles";
|
||||
|
||||
oauth2.Client? client;
|
||||
DateTime? lastRefreshedAt;
|
||||
|
||||
Future<bool> pickClient() async {
|
||||
if (await storage.containsKey(key: storageKey)) {
|
||||
try {
|
||||
var credentials =
|
||||
oauth2.Credentials.fromJson((await storage.read(key: storageKey))!);
|
||||
client = oauth2.Client(credentials,
|
||||
identifier: clientId, secret: clientSecret);
|
||||
await fetchProfiles();
|
||||
return true;
|
||||
} catch (e) {
|
||||
signOff();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<oauth2.Client> createClient(BuildContext context) async {
|
||||
// If logged in
|
||||
if (await pickClient()) {
|
||||
return client!;
|
||||
}
|
||||
|
||||
var grant = oauth2.AuthorizationCodeGrant(
|
||||
clientId,
|
||||
authorizationEndpoint,
|
||||
tokenEndpoint,
|
||||
secret: clientSecret,
|
||||
basicAuth: false,
|
||||
);
|
||||
|
||||
var authorizationUrl = grant.getAuthorizationUrl(redirectUrl, scopes: ["openid"]);
|
||||
|
||||
if (Platform.isAndroid || Platform.isIOS) {
|
||||
// Use WebView to get authorization url
|
||||
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);
|
||||
} else {
|
||||
throw UnimplementedError("unsupported platform");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> fetchProfiles() async {
|
||||
if (client != null) {
|
||||
var userinfo = await client!.get(userinfoEndpoint);
|
||||
storage.write(key: profileKey, value: utf8.decode(userinfo.bodyBytes));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshToken() async {
|
||||
if (client != null) {
|
||||
var credentials = await client?.credentials.refresh(
|
||||
identifier: clientId, secret: clientSecret, basicAuth: false);
|
||||
|
||||
storage.write(key: storageKey, value: credentials!.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> signIn(BuildContext context) async {
|
||||
client = await createClient(context);
|
||||
storage.write(key: storageKey, value: client!.credentials.toJson());
|
||||
|
||||
await fetchProfiles();
|
||||
}
|
||||
|
||||
void signOff() {
|
||||
storage.delete(key: profileKey);
|
||||
storage.delete(key: storageKey);
|
||||
}
|
||||
|
||||
Future<bool> isAuthorized() async {
|
||||
const storage = FlutterSecureStorage();
|
||||
if (await storage.containsKey(key: storageKey)) {
|
||||
if (client != null) {
|
||||
if (lastRefreshedAt == null ||
|
||||
lastRefreshedAt!
|
||||
.add(const Duration(minutes: 3))
|
||||
.isAfter(DateTime.now())) {
|
||||
await refreshToken();
|
||||
lastRefreshedAt = DateTime.now();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> getProfiles() async {
|
||||
const storage = FlutterSecureStorage();
|
||||
return jsonDecode(await storage.read(key: profileKey) ?? "{}");
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class LayoutConfig {
|
||||
String title = "Solian";
|
||||
|
||||
LayoutConfig(BuildContext context) {
|
||||
title = AppLocalizations.of(context)!.solian;
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:solian/screens/account.dart';
|
||||
import 'package:solian/screens/explore.dart';
|
||||
|
||||
final router = GoRouter(
|
||||
@ -8,5 +9,10 @@ final router = GoRouter(
|
||||
name: 'explore',
|
||||
builder: (context, state) => const ExploreScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/account',
|
||||
name: 'account',
|
||||
builder: (context, state) => const AccountScreen(),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
202
lib/screens/account.dart
Normal file
202
lib/screens/account.dart
Normal file
@ -0,0 +1,202 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:solian/utils/service_url.dart';
|
||||
import 'package:solian/widgets/wrapper.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
class AccountScreen extends StatefulWidget {
|
||||
const AccountScreen({super.key});
|
||||
|
||||
@override
|
||||
State<AccountScreen> createState() => _AccountScreenState();
|
||||
}
|
||||
|
||||
class _AccountScreenState extends State<AccountScreen> {
|
||||
bool isAuthorized = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
Future.delayed(Duration.zero, () async {
|
||||
var authorized = await context.read<AuthProvider>().isAuthorized();
|
||||
setState(() => isAuthorized = authorized);
|
||||
});
|
||||
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final auth = context.watch<AuthProvider>();
|
||||
|
||||
return LayoutWrapper(
|
||||
title: AppLocalizations.of(context)!.account,
|
||||
child: isAuthorized
|
||||
? Column(
|
||||
children: [
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 24),
|
||||
child: NameCard(),
|
||||
),
|
||||
InkWell(
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 18),
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.logout),
|
||||
title: Text("Sign out"),
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
auth.signOff();
|
||||
setState(() {
|
||||
isAuthorized = false;
|
||||
});
|
||||
},
|
||||
)
|
||||
],
|
||||
)
|
||||
: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ActionCard(
|
||||
icon: const Icon(Icons.login, color: Colors.white),
|
||||
title: AppLocalizations.of(context)!.signIn,
|
||||
caption: AppLocalizations.of(context)!.signInCaption,
|
||||
onTap: () {
|
||||
auth.signIn(context).then((_) {
|
||||
authClient.isAuthorized().then((val) {
|
||||
setState(() => isAuthorized = val);
|
||||
});
|
||||
});
|
||||
},
|
||||
),
|
||||
ActionCard(
|
||||
icon: const Icon(Icons.plus_one, color: Colors.white),
|
||||
title: AppLocalizations.of(context)!.signUp,
|
||||
caption: AppLocalizations.of(context)!.signUpCaption,
|
||||
onTap: () {
|
||||
launchUrl(getRequestUri('passport', '/auth/sign-up'));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class NameCard extends StatelessWidget {
|
||||
const NameCard({super.key});
|
||||
|
||||
Future<Widget> renderAvatar() async {
|
||||
final profiles = await authClient.getProfiles();
|
||||
return CircleAvatar(backgroundImage: NetworkImage(profiles["picture"]));
|
||||
}
|
||||
|
||||
Future<Column> renderLabel() async {
|
||||
final profiles = await authClient.getProfiles();
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
profiles["nick"],
|
||||
style: const TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(profiles["email"])
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: InkWell(
|
||||
splashColor: Colors.indigo.withAlpha(30),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Row(
|
||||
children: [
|
||||
FutureBuilder(
|
||||
future: renderAvatar(),
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<Widget> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return snapshot.data!;
|
||||
} else {
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 20),
|
||||
FutureBuilder(
|
||||
future: renderLabel(),
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<Column> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return snapshot.data!;
|
||||
} else {
|
||||
return const Column();
|
||||
}
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ActionCard extends StatelessWidget {
|
||||
final Widget icon;
|
||||
final String title;
|
||||
final String caption;
|
||||
final Function onTap;
|
||||
|
||||
const ActionCard(
|
||||
{super.key,
|
||||
required this.onTap,
|
||||
required this.title,
|
||||
required this.caption,
|
||||
required this.icon});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
onTap: () => onTap(),
|
||||
child: Container(
|
||||
width: 320,
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: CircleAvatar(
|
||||
backgroundColor: Colors.indigo,
|
||||
child: icon,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
),
|
||||
Text(caption),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
41
lib/screens/auth.dart
Executable file
41
lib/screens/auth.dart
Executable file
@ -0,0 +1,41 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class AuthorizationScreen extends StatelessWidget {
|
||||
final Uri authorizationUrl;
|
||||
|
||||
const AuthorizationScreen(this.authorizationUrl, {super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)!.signIn),
|
||||
),
|
||||
body: Stack(children: [
|
||||
WebViewWidget(
|
||||
controller: WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..setBackgroundColor(Colors.white)
|
||||
..setNavigationDelegate(NavigationDelegate(
|
||||
onNavigationRequest: (NavigationRequest request) {
|
||||
if (request.url.startsWith('solian')) {
|
||||
Navigator.of(context).pop(request.url);
|
||||
WebViewCookieManager().clearCookies();
|
||||
return NavigationDecision.prevent;
|
||||
} else if (request.url.contains("sign-up")) {
|
||||
launchUrl(Uri.parse(request.url));
|
||||
return NavigationDecision.prevent;
|
||||
}
|
||||
return NavigationDecision.navigate;
|
||||
},
|
||||
))
|
||||
..loadRequest(authorizationUrl)
|
||||
..clearCache(),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,15 +1,14 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/models/pagination.dart';
|
||||
import 'package:solian/models/post.dart';
|
||||
import 'package:solian/utils/service_url.dart';
|
||||
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
||||
import 'package:solian/providers/layout_provider.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:solian/widgets/posts/item.dart';
|
||||
import 'package:solian/widgets/wrapper.dart';
|
||||
|
||||
class ExploreScreen extends StatefulWidget {
|
||||
const ExploreScreen({super.key});
|
||||
@ -51,12 +50,6 @@ class _ExploreScreenState extends State<ExploreScreen> {
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
Future.delayed(Duration.zero, () {
|
||||
// Wait for the context
|
||||
context.read<LayoutConfig>().title =
|
||||
AppLocalizations.of(context)!.explore;
|
||||
});
|
||||
|
||||
super.initState();
|
||||
|
||||
_pagingController.addPageRequestListener((pageKey) => fetchFeed(pageKey));
|
||||
@ -64,18 +57,21 @@ class _ExploreScreenState extends State<ExploreScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RefreshIndicator(
|
||||
onRefresh: () => Future.sync(
|
||||
() => _pagingController.refresh(),
|
||||
),
|
||||
child: Center(
|
||||
child: Container(
|
||||
constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: PagedListView<int, Post>.separated(
|
||||
pagingController: _pagingController,
|
||||
separatorBuilder: (context, index) => const Divider(thickness: 0.3),
|
||||
builderDelegate: PagedChildBuilderDelegate<Post>(
|
||||
itemBuilder: (context, item, index) => PostItem(item: item),
|
||||
return LayoutWrapper(
|
||||
title: AppLocalizations.of(context)!.explore,
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () => Future.sync(
|
||||
() => _pagingController.refresh(),
|
||||
),
|
||||
child: Center(
|
||||
child: Container(
|
||||
constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: PagedListView<int, Post>.separated(
|
||||
pagingController: _pagingController,
|
||||
separatorBuilder: (context, index) => const Divider(thickness: 0.3),
|
||||
builderDelegate: PagedChildBuilderDelegate<Post>(
|
||||
itemBuilder: (context, item, index) => PostItem(item: item),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -27,6 +27,13 @@ class _SolianNavigationDrawerState extends State<SolianNavigationDrawer> {
|
||||
),
|
||||
"explore",
|
||||
),
|
||||
(
|
||||
NavigationDrawerDestination(
|
||||
icon: const Icon(Icons.account_circle),
|
||||
label: Text(AppLocalizations.of(context)!.account),
|
||||
),
|
||||
"account",
|
||||
),
|
||||
];
|
||||
|
||||
return NavigationDrawer(
|
||||
|
@ -36,8 +36,6 @@ class _PostItemState extends State<PostItem> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const borderRadius = Radius.circular(16);
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
||||
child: Column(
|
||||
|
@ -1,20 +1,17 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/providers/layout_provider.dart';
|
||||
import 'package:solian/widgets/navigation_drawer.dart';
|
||||
|
||||
class LayoutWrapper extends StatelessWidget {
|
||||
final Widget? child;
|
||||
final String title;
|
||||
|
||||
const LayoutWrapper({super.key, this.child});
|
||||
const LayoutWrapper({super.key, this.child, required this.title});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var cfg = context.watch<LayoutConfig>();
|
||||
|
||||
return Scaffold(
|
||||
drawer: const SolianNavigationDrawer(),
|
||||
appBar: AppBar(title: Text(cfg.title)),
|
||||
appBar: AppBar(title: Text(title)),
|
||||
body: child ?? Container(),
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user