Advertisement
Guest User

Untitled

a guest
May 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.49 KB | None | 0 0
  1. /*
  2. * Copyright 2018 Ruben Talstra and Yvan Watchman
  3. *
  4. * Licensed under the GNU General Public License v3.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *    https://www.gnu.org/licenses/gpl-3.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import 'dart:async';
  17. import 'package:pterodactyl_app/page/auth/shared_preferences_helper.dart';
  18. import 'package:pterodactyl_app/page/client/home.dart';
  19. import 'package:pterodactyl_app/page/client/login.dart';
  20. import 'package:http/http.dart' as http;
  21. import 'package:flutter/material.dart';
  22. import 'package:shared_preferences/shared_preferences.dart';
  23. import 'package:pterodactyl_app/page/admin/adminhome.dart';
  24. import 'package:pterodactyl_app/page/admin/adminlogin.dart';
  25. import 'package:pterodactyl_app/page/company/deploys/client/home.dart';
  26. import 'package:pterodactyl_app/page/company/deploys/client/login.dart';
  27. import 'package:pterodactyl_app/page/auth/selecthost.dart';
  28.  
  29. class Splash extends StatefulWidget {
  30.   @override
  31.   SplashState createState() => new SplashState();
  32. }
  33.  
  34. class SplashState extends State<Splash> {
  35.   BuildContext context;
  36.  
  37.   Future checkSelectHostSeen() async {
  38.     SharedPreferences prefs = await SharedPreferences.getInstance();
  39.     bool _host = (prefs.getBool('host') ?? false);
  40.     bool _seen = (prefs.getBool('seen') ?? false);
  41.     bool _seenadmin = (prefs.getBool('seenadmin') ?? false);
  42.     bool _deploys = (prefs.getBool('deploys') ?? false);
  43.  
  44.     if (_host == false) {
  45.       if (_seen) {
  46.         if (await isAuthenticated()) {
  47.           Navigator.of(context).pushReplacement(
  48.               new MaterialPageRoute(builder: (context) => new MyHomePage()));
  49.         }
  50.       } else {
  51.         prefs.setBool('seen', true);
  52.         Navigator.of(context).pushReplacement(
  53.             new MaterialPageRoute(builder: (context) => new LoginPage()));
  54.       }
  55.       if (_seenadmin && await isAuthenticated(isAdmin: true)) {
  56.         Navigator.of(context).pushReplacement(
  57.             new MaterialPageRoute(builder: (context) => new AdminHomePage()));
  58.       } else {
  59.         prefs.setBool('seenadmin', true);
  60.         Navigator.of(context).pushReplacement(
  61.             new MaterialPageRoute(builder: (context) => new AdminLoginPage()));
  62.       }
  63.       if (_deploys == false && await isAuthenticated(isDeploys: true)) {
  64.         Navigator.of(context).pushReplacement(new MaterialPageRoute(
  65.             builder: (context) => new MyDeploysHomePage()));
  66.       } else {
  67.         prefs.setBool('deploys', true);
  68.         Navigator.of(context).pushReplacement(new MaterialPageRoute(
  69.             builder: (context) => new LoginDeploysPage()));
  70.       }
  71.     } else {
  72.       prefs.setBool('host', false);
  73.       Navigator.of(context).pushReplacement(
  74.           new MaterialPageRoute(builder: (context) => new SelectHostPage()));
  75.     }
  76.   }
  77.  
  78.   Future<bool> isAuthenticated({isAdmin = false, isDeploys = false}) async {
  79.     if (isDeploys) {
  80.       String _apideployskey = await SharedPreferencesHelper.getString("api_deploys_Key");
  81.  
  82.       if (_apideployskey.isEmpty) {
  83.         SharedPreferencesHelper.remove('api_deploys_Key');
  84.         Navigator.of(context).pushNamedAndRemoveUntil(
  85.             '/deployslogin', (Route<dynamic> route) => false);
  86.         return false;
  87.       }
  88.  
  89.       http.Response response = await http.get(
  90.         "https://panel.deploys.io/api/client",
  91.         headers: {
  92.           "Accept": "Application/vnd.pterodactyl.v1+json",
  93.           "Authorization": "Bearer $_apideployskey"
  94.         },
  95.       );
  96.  
  97.       if (response.statusCode == 401) {
  98.         // Todo fix Navigation context for logging out if key isn't available
  99.         SharedPreferencesHelper.remove('api_deploys_Key');
  100.         Navigator.of(context).pushNamedAndRemoveUntil(
  101.             '/deployslogin', (Route<dynamic> route) => false);
  102.         return false;
  103.       } else {
  104.         return true;
  105.       }
  106.     } if (isAdmin) {
  107.       String _apiadmin = await SharedPreferencesHelper.getString("apiAdminKey");
  108.       String _adminhttps =
  109.           await SharedPreferencesHelper.getString("adminhttps");
  110.       String _urladmin =
  111.           await SharedPreferencesHelper.getString("panelAdminUrl");
  112.  
  113.       if (_urladmin.isEmpty) {
  114.         SharedPreferencesHelper.remove('apiAdminKey');
  115.         Navigator.of(context).pushNamedAndRemoveUntil(
  116.             '/adminlogin', (Route<dynamic> route) => false);
  117.         return false;
  118.       }
  119.  
  120.       http.Response response = await http.get(
  121.         "$_adminhttps$_urladmin/api/application/servers",
  122.         headers: {
  123.           "Accept": "Application/vnd.pterodactyl.v1+json",
  124.           "Authorization": "Bearer $_apiadmin"
  125.         },
  126.       );
  127.  
  128.       if (response.statusCode == 401) {
  129.         // Todo fix Navigation context for logging out if key isn't available
  130.         SharedPreferencesHelper.remove('apiAdminKey');
  131.         Navigator.of(context).pushNamedAndRemoveUntil(
  132.             '/adminlogin', (Route<dynamic> route) => false);
  133.         return false;
  134.       } else {
  135.         return true;
  136.       }
  137.     } else {
  138.       String _apikey = await SharedPreferencesHelper.getString("apiKey");
  139.       String _https = await SharedPreferencesHelper.getString("https");
  140.       String _url = await SharedPreferencesHelper.getString("panelUrl");
  141.  
  142.       if (_url.isEmpty) {
  143.         SharedPreferencesHelper.remove('apiKey');
  144.         Navigator.of(context)
  145.             .pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
  146.         return false;
  147.       }
  148.  
  149.       http.Response response = await http.get(
  150.         "$_https$_url/api/client",
  151.         headers: {
  152.           "Accept": "Application/vnd.pterodactyl.v1+json",
  153.           "Authorization": "Bearer $_apikey"
  154.         },
  155.       );
  156.  
  157.       print(response.statusCode);
  158.  
  159.       if (response.statusCode == 401) {
  160.         // Todo fix Navigation context for logging out if key isn't available
  161.         SharedPreferencesHelper.remove('apiKey');
  162.         Navigator.of(context)
  163.             .pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
  164.         return false;
  165.       } else {
  166.         return true;
  167.       }
  168.     }
  169.   }
  170.  
  171.   @override
  172.   void initState() {
  173.     super.initState();
  174.     checkSelectHostSeen();
  175.   }
  176.  
  177.   @override
  178.   Widget build(BuildContext context) {
  179.     this.context = context;
  180.     return new Scaffold(
  181.       body: new Center(
  182.         child: new Text('Loading...'),
  183.       ),
  184.     );
  185.   }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement