Initial Commit
This commit is contained in:
28
lib/main.dart
Normal file
28
lib/main.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'views/home_screen.dart';
|
||||
import 'views/profile_screen.dart';
|
||||
import 'views/login_screen.dart';
|
||||
import 'methods/api.dart';
|
||||
import 'views/init_screen.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: '42 API Client',
|
||||
initialRoute: '/',
|
||||
routes: {
|
||||
'/': (context) => const InitScreen(),
|
||||
'/home': (context) => HomeScreen(),
|
||||
'/login': (context) => const LoginScreen(),
|
||||
'/profile': (context) => const ProfileScreen(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
24
lib/methods/api.dart
Normal file
24
lib/methods/api.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
Future<bool> checkToken() async
|
||||
{
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.containsKey('user_token');
|
||||
}
|
||||
|
||||
Future<void> saveToken(String token) async
|
||||
{
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString('user_token', token);
|
||||
}
|
||||
|
||||
Future<String?> getToken() async
|
||||
{
|
||||
final exists = await checkToken();
|
||||
if (exists) {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final token = prefs.getString('user_token');
|
||||
return token;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
21
lib/views/home_screen.dart
Normal file
21
lib/views/home_screen.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
// home_screen.dart
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class HomeScreen extends StatelessWidget {
|
||||
const HomeScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Home")),
|
||||
body: Center(
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(context, '/profile');
|
||||
},
|
||||
child: const Text('Go to Profile'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
66
lib/views/init_screen.dart
Normal file
66
lib/views/init_screen.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import '../methods/api.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
|
||||
Future<bool> initApp() async {
|
||||
return await checkToken();
|
||||
}
|
||||
class InitScreen extends StatelessWidget {
|
||||
const InitScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: FutureBuilder<bool?> (
|
||||
future: initApp(),
|
||||
builder: (context, snapshot)
|
||||
{
|
||||
if (snapshot.hasError){
|
||||
return Center(
|
||||
child: Text('Error: ${snapshot.error}'),
|
||||
);
|
||||
}
|
||||
switch (snapshot.connectionState)
|
||||
{
|
||||
case ConnectionState.waiting:
|
||||
return Center(
|
||||
child: Image(
|
||||
image: AssetImage("assets/images/42_logo.png"),
|
||||
width: 100,
|
||||
height: 100,
|
||||
),
|
||||
);
|
||||
case ConnectionState.done:
|
||||
if (snapshot.data == true) {
|
||||
Future.microtask(() {
|
||||
Navigator.pushReplacementNamed(context, '/home');
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Future.microtask(() {
|
||||
Navigator.pushReplacementNamed(context, '/login');
|
||||
});
|
||||
}
|
||||
return Center(
|
||||
child: Text(
|
||||
(snapshot.data == true) ? "1" : "0"
|
||||
)
|
||||
);
|
||||
default:
|
||||
Future.microtask(() {
|
||||
Navigator.pushReplacementNamed(context, '/login');
|
||||
});
|
||||
return Center(
|
||||
child: Text(
|
||||
"Redirect"
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
70
lib/views/login_screen.dart
Normal file
70
lib/views/login_screen.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_appauth/flutter_appauth.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
|
||||
const FlutterAppAuth _appAuth = FlutterAppAuth();
|
||||
|
||||
Future<void> redirect_to_oauth(BuildContext context) async {
|
||||
final String _clientId = dotenv.env['CLIENT-ID'] ?? '';
|
||||
final String _clientSecret = dotenv.env['CLIENT-SECRET'] ?? '';
|
||||
final String _redirectUrl = 'swifty-companion://oauth2/callback';
|
||||
final String _authorizationEndpoint = 'https://api.intra.42.fr/oauth/authorize';
|
||||
final String _tokenEndpoint = 'https://api.intra.42.fr/oauth/token';
|
||||
final request = AuthorizationRequest(
|
||||
_clientId,
|
||||
_redirectUrl,
|
||||
serviceConfiguration: AuthorizationServiceConfiguration(
|
||||
authorizationEndpoint: _authorizationEndpoint,
|
||||
tokenEndpoint: _tokenEndpoint,
|
||||
));
|
||||
print("swap");
|
||||
try {
|
||||
print("trying");
|
||||
final AuthorizationResponse? result = await _appAuth.authorize(request);
|
||||
print("tried");
|
||||
if (result != null) {
|
||||
print("nonull result");
|
||||
Navigator.pushReplacementNamed(context, "/home");
|
||||
print('Authorization Code: ${result.authorizationCode}');
|
||||
final token_request = TokenRequest(
|
||||
_clientId,
|
||||
_redirectUrl,
|
||||
clientSecret: _clientSecret,
|
||||
authorizationCode: result.authorizationCode,
|
||||
grantType: 'authorization_code',
|
||||
serviceConfiguration: AuthorizationServiceConfiguration(
|
||||
authorizationEndpoint: "https://api.intra.42.fr/oauth/authorize",
|
||||
tokenEndpoint: _tokenEndpoint,
|
||||
),
|
||||
);
|
||||
print("pre token");
|
||||
final TokenResponse? tokenResponse = await _appAuth.token(token_request);
|
||||
print("token");
|
||||
print(tokenResponse?.accessToken);
|
||||
print(tokenResponse?.refreshToken);
|
||||
// You can now use this code to exchange for an access token (via a separate API call to the token endpoint)
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
print("error $e");
|
||||
}
|
||||
}
|
||||
class LoginScreen extends StatelessWidget {
|
||||
const LoginScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Login")),
|
||||
body: Center(
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
redirect_to_oauth(context);
|
||||
},
|
||||
child: const Text('Login'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
21
lib/views/profile_screen.dart
Normal file
21
lib/views/profile_screen.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
// profile_screen.dart
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ProfileScreen extends StatelessWidget {
|
||||
const ProfileScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Profile")),
|
||||
body: Center(
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(context, '/');
|
||||
},
|
||||
child: const Text('Go to Home'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user