Finalized OAuth flow, added /profile/{LOGIN} routes, improved home_view with a background

This commit is contained in:
2025-05-14 21:01:47 +02:00
parent ecd2446380
commit 867b617a41
7 changed files with 121 additions and 46 deletions

View File

@@ -1,13 +1,15 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_appauth/flutter_appauth.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:swifty/methods/api.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 _clientId = "CLIENT TODO";
final String _clientSecret = "SECRET TODO";
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';
@@ -18,36 +20,30 @@ Future<void> redirect_to_oauth(BuildContext context) async {
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)
}
if (result == null) { throw PlatformException(code: "fatal_error"); }
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,
),
);
final TokenResponse? tokenResponse = await _appAuth.token(token_request);
saveToken(tokenResponse?.accessToken ?? '', tokenResponse?.refreshToken ?? '', tokenResponse?.accessTokenExpirationDateTime ?? DateTime.now());
Navigator.pushReplacementNamed(context, "/home");
}
on PlatformException catch (e)
{
if (e.code == "authorize_failed" && e.message?.contains('User cancelled flow') == true) { return; }
}
catch (e) {
print("error $e");
return;
}
}
class LoginScreen extends StatelessWidget {