Skip to content

Commit

Permalink
auth0 implementation ongoing
Browse files Browse the repository at this point in the history
  • Loading branch information
romme86 committed May 20, 2022
1 parent 265d8cb commit 0eca624
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 19 deletions.
1 change: 1 addition & 0 deletions vidaia/lib/helpers/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ const AUTH0_CLIENT_ID =
const AUTH0_ISSUER = 'https://$AUTH0_DOMAIN';
const BUNDLE_IDENTIFIER = 'ch.saynode.vidaia';
const AUTH0_REDIRECT_URI = '$BUNDLE_IDENTIFIER://login-callback';
const REFRESH_TOKEN_KEY = 'refresh_token';
4 changes: 3 additions & 1 deletion vidaia/lib/models/HistoryEntry.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions vidaia/lib/models/auth0_id_token.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:json_annotation/json_annotation.dart';
part 'auth0_id_token.g.dart';

@JsonSerializable()
class Auth0IdToken {
Auth0IdToken({
required this.nickname,
required this.name,
required this.email,
required this.picture,
required this.updatedAt,
required this.iss,
required this.sub, // aka subject... the userId.
required this.aud,
required this.iat,
required this.exp,
this.authTime,
});

final String nickname;
final String name;
final String picture;

@JsonKey(name: 'updated_at')
final String updatedAt;

final String iss;

// In OIDC, "sub" means "subject identifier",
// which for our purposes is the user ID.
// This getter makes it easier to understand.
String get userId => sub;
final String sub;

final String aud;
final String email;
final int iat;
final int exp;

@JsonKey(name: 'auth_time')
final int? authTime; // this might be null for the first time login

factory Auth0IdToken.fromJson(Map<String, dynamic> json) =>
_$Auth0IdTokenFromJson(json);

Map<String, dynamic> toJson() => _$Auth0IdTokenToJson(this);
}
36 changes: 36 additions & 0 deletions vidaia/lib/models/auth0_id_token.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vidaia/lib/models/auth0_user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:json_annotation/json_annotation.dart';
part 'auth0_user.g.dart';

@JsonSerializable()
class Auth0User {
Auth0User({
required this.nickname,
required this.name,
required this.email,
required this.picture,
required this.updatedAt,
required this.sub,
});
final String nickname;
final String name;
final String picture;

@JsonKey(name: 'updated_at')
final String updatedAt;

// userID getter to understand it easier
String get id => sub;
final String sub;

final String email;

factory Auth0User.fromJson(Map<String, dynamic> json) =>
_$Auth0UserFromJson(json);

Map<String, dynamic> toJson() => _$Auth0UserToJson(this);
}
25 changes: 25 additions & 0 deletions vidaia/lib/models/auth0_user.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 97 additions & 18 deletions vidaia/lib/pages/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,31 @@ import 'package:vidaia/main.dart';
import 'package:vidaia/pages/home/home_page_loader.dart';
import 'package:vidaia/utils/globals.dart';
import 'package:vidaia/utils/wallet.dart';
import 'package:vidaia/services/auth_service.dart';

const users = {
'a@a.com': 'password',
};

class LoginPage extends StatelessWidget {
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);

@override
State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
Duration get loginTime => const Duration(milliseconds: 2250);
bool isProgressing = false;
bool isLoggedIn = false;
String errorMessage = '';
String? name;

@override
void initState() {
initAction();
super.initState();
}

Future<String?> _authUser(LoginData data) {
debugPrint('Name: ${data.name}, Password: ${data.password}');
Expand Down Expand Up @@ -54,24 +71,86 @@ class LoginPage extends StatelessWidget {
Widget build(BuildContext context) {
return Container(
color: primaryColor,
child: FlutterLogin(
userType: LoginUserType.name,
logo: const AssetImage('assets/images/vidaia-live-sustainably.png'),
messages:
LoginMessages(userHint: 'Email', passwordHint: 'password'.tr()),
onLogin: _authUser,
onSignup: _signupUser,
onSubmitAnimationCompleted: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage2()),
);
if (!mnemonicNoted) {
showMnemonicAlert(context);
}
},
onRecoverPassword: _recoverPassword,
child: Column(
children: [
// FlutterLogin(
// userType: LoginUserType.name,
// logo: const AssetImage('assets/images/vidaia-live-sustainably.png'),
// messages:
// LoginMessages(userHint: 'Email', passwordHint: 'password'.tr()),
// onLogin: _authUser,
// onSignup: _signupUser,
// onSubmitAnimationCompleted: () {
// Navigator.push(
// context,
// MaterialPageRoute(builder: (context) => HomePage2()),
// );
// if (!mnemonicNoted) {
// showMnemonicAlert(context);
// }
// },
// onRecoverPassword: _recoverPassword,
// ),
TextButton(
onPressed: loginAction,
child: const Text('Auth0 Login | Register'),
),
if (isProgressing)
CircularProgressIndicator()
else if (!isLoggedIn)
TextButton(
onPressed: loginAction,
child: const Text('Auth0 Login | Register'),
)
else
Text('Welcome $name'),
],
),
);
}

setSuccessAuthState() {
setState(() {
isProgressing = false;
isLoggedIn = true;
name = AuthService.instance.idToken?.name;
});

Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage2()),
);
}

setLoadingState() {
setState(() {
isProgressing = true;
errorMessage = '';
});
}

Future<void> loginAction() async {
setLoadingState();
final message = await AuthService.instance.login();
if (message == 'Success') {
setSuccessAuthState();
} else {
setState(() {
isProgressing = false;
errorMessage = message;
});
}
}

initAction() async {
setLoadingState();
final bool isAuth = await AuthService.instance.init();
if (isAuth) {
setSuccessAuthState();
} else {
setState(() {
isProgressing = false;
});
}
}
}
Loading

0 comments on commit 0eca624

Please sign in to comment.