Last active
November 10, 2020 06:19
-
-
Save vemarav/6ef01cbdae1e27dd540ad78dd337ab88 to your computer and use it in GitHub Desktop.
Flutter Authentication Flow
This file contains hidden or 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
import 'dart:async'; | |
import 'package:auth_flow/app/utils/auth_utils.dart'; | |
import 'package:auth_flow/app/utils/network_utils.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
class HomePage extends StatefulWidget { | |
static final String routeName = 'home'; | |
@override | |
State<StatefulWidget> createState() { | |
return new _HomePageState(); | |
} | |
} | |
class _HomePageState extends State<HomePage> { | |
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); | |
Future<SharedPreferences> _prefs = SharedPreferences.getInstance(); | |
SharedPreferences _sharedPreferences; | |
var _authToken, _id, _name, _homeResponse; | |
@override | |
void initState() { | |
super.initState(); | |
_fetchSessionAndNavigate(); | |
} | |
_fetchSessionAndNavigate() async { | |
_sharedPreferences = await _prefs; | |
String authToken = AuthUtils.getToken(_sharedPreferences); | |
var id = _sharedPreferences.getInt(AuthUtils.userIdKey); | |
var name = _sharedPreferences.getString(AuthUtils.nameKey); | |
print(authToken); | |
_fetchHome(authToken); | |
setState(() { | |
_authToken = authToken; | |
_id = id; | |
_name = name; | |
}); | |
if(_authToken == null) { | |
_logout(); | |
} | |
} | |
_fetchHome(String authToken) async { | |
var responseJson = await NetworkUtils.fetch(authToken, '/api/v1/home'); | |
if(responseJson == null) { | |
NetworkUtils.showSnackBar(_scaffoldKey, 'Something went wrong!'); | |
} else if(responseJson == 'NetworkError') { | |
NetworkUtils.showSnackBar(_scaffoldKey, null); | |
} else if(responseJson['errors'] != null) { | |
_logout(); | |
} | |
setState(() { | |
_homeResponse = responseJson.toString(); | |
}); | |
} | |
_logout() { | |
NetworkUtils.logoutUser(_scaffoldKey.currentContext, _sharedPreferences); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
key: _scaffoldKey, | |
appBar: new AppBar( | |
title: new Text('Home'), | |
), | |
body: new Container( | |
margin: const EdgeInsets.symmetric(horizontal: 16.0), | |
child: new Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
new Container( | |
padding: const EdgeInsets.all(8.0), | |
child: new Text( | |
"USER_ID: $_id \nUSER_NAME: $_name \nHOME_RESPONSE: $_homeResponse", | |
style: new TextStyle( | |
fontSize: 24.0, | |
color: Colors.grey.shade700 | |
), | |
), | |
), | |
new MaterialButton( | |
color: Theme.of(context).primaryColor, | |
child: new Text( | |
'Logout', | |
style: new TextStyle( | |
color: Colors.white | |
), | |
), | |
onPressed: _logout | |
), | |
] | |
), | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment