-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
274 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_appauth/flutter_appauth.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
import 'package:vidaia/pages/profile_testing.dart'; | ||
import 'package:vidaia/utils/auth0.dart'; | ||
|
||
import 'login_testing.dart'; | ||
final FlutterAppAuth appAuth = FlutterAppAuth(); | ||
final FlutterSecureStorage secureStorage = const FlutterSecureStorage(); | ||
const AUTH0_DOMAIN = 'saynode.eu.auth0.com'; | ||
const AUTH0_CLIENT_ID = 'NtesFp8kbFYekf05rleULMSdBS5hEWRN'; | ||
|
||
const AUTH0_REDIRECT_URI = 'com.auth0.vidaia://login-callback'; | ||
const AUTH0_ISSUER = 'https://$AUTH0_DOMAIN'; | ||
|
||
class Auth0TestPage extends StatefulWidget { | ||
|
||
Auth0TestPage(); | ||
|
||
@override | ||
State<Auth0TestPage> createState() => _Auth0TestPageState(); | ||
} | ||
|
||
class _Auth0TestPageState extends State<Auth0TestPage> { | ||
bool isBusy = false; | ||
bool isLoggedIn = false; | ||
String errorMessage = ''; | ||
String? name; | ||
String? picture; | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text('Auth0 Test'), | ||
), | ||
body: Center( | ||
child: isBusy | ||
? CircularProgressIndicator() | ||
: isLoggedIn | ||
? Profile(logoutAction, name!, picture!) | ||
: Login(loginAction, errorMessage), | ||
), | ||
); | ||
} | ||
Future<void> loginAction() async { | ||
setState(() { | ||
isBusy = true; | ||
errorMessage = ''; | ||
}); | ||
|
||
try { | ||
final AuthorizationTokenResponse? result = | ||
await appAuth.authorizeAndExchangeCode( | ||
AuthorizationTokenRequest( | ||
AUTH0_CLIENT_ID, | ||
AUTH0_REDIRECT_URI, | ||
issuer: 'https://$AUTH0_DOMAIN', | ||
scopes: ['openid', 'profile', 'offline_access'], | ||
// promptValues: ['login'] | ||
), | ||
); | ||
|
||
final idToken = parseIdToken(result!.idToken!); | ||
final profile = await getUserDetails(result.accessToken!); | ||
|
||
await secureStorage.write( | ||
key: 'refresh_token', value: result.refreshToken); | ||
|
||
setState(() { | ||
isBusy = false; | ||
isLoggedIn = true; | ||
name = idToken['name']; | ||
picture = profile['picture']; | ||
}); | ||
} catch (e, s) { | ||
print('login error: $e - stack: $s'); | ||
|
||
setState(() { | ||
isBusy = false; | ||
isLoggedIn = false; | ||
errorMessage = e.toString(); | ||
}); | ||
} | ||
} | ||
|
||
void logoutAction() async { | ||
await secureStorage.delete(key: 'refresh_token'); | ||
setState(() { | ||
isLoggedIn = false; | ||
isBusy = false; | ||
}); | ||
} | ||
@override | ||
void initState() { | ||
initAction(); | ||
super.initState(); | ||
} | ||
|
||
void initAction() async { | ||
final storedRefreshToken = await secureStorage.read(key: 'refresh_token'); | ||
if (storedRefreshToken == null) return; | ||
|
||
setState(() { | ||
isBusy = true; | ||
}); | ||
|
||
try { | ||
final response = await appAuth.token(TokenRequest( | ||
AUTH0_CLIENT_ID, | ||
AUTH0_REDIRECT_URI, | ||
issuer: AUTH0_ISSUER, | ||
refreshToken: storedRefreshToken, | ||
)); | ||
|
||
final idToken = parseIdToken(response!.idToken!); | ||
final profile = await getUserDetails(response.accessToken!); | ||
|
||
secureStorage.write(key: 'refresh_token', value: response.refreshToken); | ||
|
||
setState(() { | ||
isBusy = false; | ||
isLoggedIn = true; | ||
name = idToken['name']; | ||
picture = profile['picture']; | ||
}); | ||
} catch (e, s) { | ||
print('error on refresh token: $e - stack: $s'); | ||
logoutAction(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class Login extends StatelessWidget { | ||
final loginAction; | ||
final String loginError; | ||
|
||
const Login(this.loginAction, this.loginError); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
ElevatedButton( | ||
onPressed: () { | ||
loginAction(); | ||
}, | ||
child: Text('Login'), | ||
), | ||
Text(loginError), | ||
|
||
|
||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class Profile extends StatelessWidget { | ||
final logoutAction; | ||
final String name; | ||
final String picture; | ||
|
||
Profile(this.logoutAction, this.name, this.picture); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
Container( | ||
width: 150, | ||
height: 150, | ||
decoration: BoxDecoration( | ||
border: Border.all(color: Colors.blue, width: 4.0), | ||
shape: BoxShape.circle, | ||
image: DecorationImage( | ||
fit: BoxFit.fill, | ||
image: NetworkImage(picture), | ||
), | ||
), | ||
), | ||
SizedBox(height: 24.0), | ||
Text('Name: $name'), | ||
SizedBox(height: 48.0), | ||
ElevatedButton( | ||
onPressed: () { | ||
logoutAction(); | ||
}, | ||
child: Text('Logout'), | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'dart:convert'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:vidaia/pages/auth0_testing_page.dart'; | ||
|
||
Map<String, dynamic> parseIdToken(String idToken) { | ||
final parts = idToken.split(r'.'); | ||
assert(parts.length == 3); | ||
|
||
return jsonDecode( | ||
utf8.decode(base64Url.decode(base64Url.normalize(parts[1])))); | ||
} | ||
|
||
Future<Map<String, dynamic>> getUserDetails(String accessToken) async { | ||
final url = Uri.parse('https://$AUTH0_DOMAIN/userinfo'); | ||
final response = await http.get( | ||
url, | ||
headers: {'Authorization': 'Bearer $accessToken'}, | ||
); | ||
|
||
if (response.statusCode == 200) { | ||
return jsonDecode(response.body); | ||
} else { | ||
throw Exception('Failed to get user details'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters