Skip to content

Commit

Permalink
login using auth0
Browse files Browse the repository at this point in the history
  • Loading branch information
YannMarti committed May 13, 2022
1 parent 9c5b5d1 commit 87bcff2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
83 changes: 37 additions & 46 deletions vidaia/lib/pages/auth0_testing_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ 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 'package:vidaia/utils/globals.dart' as globals;

import 'home/home_page_loader.dart';
import 'login_testing.dart';

final FlutterAppAuth appAuth = FlutterAppAuth();
final FlutterSecureStorage secureStorage = const FlutterSecureStorage();
const AUTH0_DOMAIN = 'saynode.eu.auth0.com';
Expand All @@ -14,50 +17,41 @@ 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),
),
);
appBar: AppBar(
title: Text('Auth0 Test'),
),
body: Center(
child: globals.isBusy
? CircularProgressIndicator()
: Login(loginAction, errorMessage),
),
);
}
Future<void> loginAction() async {

Future<void> loginAction() async {
setState(() {
isBusy = true;
globals.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']
),
AuthorizationTokenRequest(AUTH0_CLIENT_ID, AUTH0_REDIRECT_URI,
issuer: 'https://$AUTH0_DOMAIN',
scopes: ['openid', 'profile', 'offline_access'],
promptValues: ['login']),
);

final idToken = parseIdToken(result!.idToken!);
Expand All @@ -67,30 +61,27 @@ class _Auth0TestPageState extends State<Auth0TestPage> {
key: 'refresh_token', value: result.refreshToken);

setState(() {
isBusy = false;
isLoggedIn = true;
name = idToken['name'];
picture = profile['picture'];
globals.isBusy = false;
globals.isLoggedIn = true;
globals.name = idToken['globals.name'];
globals.picture = profile['globals.picture'];
});
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage2()),
);
} catch (e, s) {
print('login error: $e - stack: $s');

setState(() {
isBusy = false;
isLoggedIn = false;
globals.isBusy = false;
globals.isLoggedIn = false;
errorMessage = e.toString();
});
}
}

void logoutAction() async {
await secureStorage.delete(key: 'refresh_token');
setState(() {
isLoggedIn = false;
isBusy = false;
});
}
@override
@override
void initState() {
initAction();
super.initState();
Expand All @@ -101,7 +92,7 @@ class _Auth0TestPageState extends State<Auth0TestPage> {
if (storedRefreshToken == null) return;

setState(() {
isBusy = true;
globals.isBusy = true;
});

try {
Expand All @@ -118,14 +109,14 @@ class _Auth0TestPageState extends State<Auth0TestPage> {
secureStorage.write(key: 'refresh_token', value: response.refreshToken);

setState(() {
isBusy = false;
isLoggedIn = true;
name = idToken['name'];
picture = profile['picture'];
globals.isBusy = false;
globals.isLoggedIn = true;
globals.name = idToken['globals.name'];
globals.picture = profile['globals.picture'];
});
} catch (e, s) {
print('error on refresh token: $e - stack: $s');
logoutAction();
logoutAction(context);
}
}
}
}
12 changes: 12 additions & 0 deletions vidaia/lib/utils/auth0.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:vidaia/pages/auth0_testing_page.dart';
import 'package:vidaia/utils/globals.dart' as globals;

Map<String, dynamic> parseIdToken(String idToken) {
final parts = idToken.split(r'.');
Expand All @@ -23,3 +25,13 @@ Future<Map<String, dynamic>> getUserDetails(String accessToken) async {
throw Exception('Failed to get user details');
}
}

void logoutAction(BuildContext context) async {
await secureStorage.delete(key: 'refresh_token');
globals.isLoggedIn = false;
globals.isBusy = false;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Auth0TestPage()),
);
}
6 changes: 6 additions & 0 deletions vidaia/lib/utils/globals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ String? qrCode;
bool mnemonicNoted = false;
String token = '';

//auth0
bool isBusy = false;
bool isLoggedIn = false;
String? name;
String? picture;


//priv c337cf0b3c7c3e4b7f5480b985724e0f221120554459b5c247870d2789726089
//address 0x17ACC76e4685AEA9d574705163E871b83e36697f
4 changes: 2 additions & 2 deletions vidaia/lib/widgets/vidaia_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:vidaia/pages/home/home_page_loader.dart';
import 'package:vidaia/pages/login_page.dart';
import 'package:vidaia/pages/settings_page.dart';
import 'package:vidaia/repositories/dataRepository.dart';
import 'package:vidaia/utils/auth0.dart';
import 'package:vidaia/utils/constants.dart';

class VidaiaDrawer extends StatelessWidget {
Expand Down Expand Up @@ -174,8 +175,7 @@ class VidaiaDrawer extends StatelessWidget {
style: Theme.of(context).textTheme.subtitle1,
),
onTap: () => {
Navigator.push(context,
MaterialPageRoute(builder: (context) => LoginPage()))
logoutAction(context)
}),
),
]),
Expand Down

0 comments on commit 87bcff2

Please sign in to comment.