在Flutter中进行GitHub身份验证后的深层链接

问题描述 投票:0回答:1

如果用户未登录,我希望显示我的登录屏幕,如果从设备登录,则要显示MainPage。对于GitHub身份验证,将打开浏览器,一旦身份验证成功,我希望将用户重定向到MainPage而不是登录屏幕,但是使用我的代码,用户将再次重定向到登录屏幕。登录页面的代码是

import 'package:flutter/material.dart';
import 'package:githubapp/custom_widgets/custom_login_page.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:async';
import 'package:uni_links/uni_links.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  StreamSubscription _subs;

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

  @override
  void dispose() {
    _disposeDeepLinkListener();
    super.dispose();
  }

  void _initDeepLinkListener() async {
    _subs = getLinksStream().listen((String link) {
      _checkDeepLink(link);
    }, cancelOnError: true);
  }

  void _checkDeepLink(String link) {
    if (link != null) {
      String code = link.substring(link.indexOf(RegExp('code=')) + 5);
      loginWithGitHub(code)
          .then((firebaseUser) {
        print("LOGGED IN AS: " + firebaseUser.displayName);
      }).catchError((e) {
        print("LOGIN ERROR: " + e.toString());
      });
    }
  }

  void _disposeDeepLinkListener() {
    if (_subs != null) {
      _subs.cancel();
      _subs = null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
   child: Center(
   RaisedButton(
   onTap: () => onClickGitHubLoginButton(),
)
)
);
  }
}

const String GITHUB_CLIENT_ID = "Client Id of GitHub";
const String GITHUB_CLIENT_SECRET = "Client Secret of GitHub";

void onClickGitHubLoginButton() async {
  const String url = "https://github.com/login/oauth/authorize" +
      "?client_id=" + GITHUB_CLIENT_ID +
      "&scope=public_repo%20read:user%20user:email";

  if (await canLaunch(url)) {
    await launch(
      url,
      forceSafariVC: false,
      forceWebView: false,
    );
  } else {
    print("CANNOT LAUNCH THIS URL!");
  }
}

Future<FirebaseUser> loginWithGitHub(String code) async {
  //ACCESS TOKEN REQUEST
  final response = await http.post(
    "https://github.com/login/oauth/access_token",
    headers: {
      "Content-Type": "application/json",
      "Accept": "application/json"
    },
    body: jsonEncode(GitHubLoginRequest(
      clientId: GITHUB_CLIENT_ID,
      clientSecret: GITHUB_CLIENT_SECRET,
      code: code,
    )),
  );

  GitHubLoginResponse loginResponse =
  GitHubLoginResponse.fromJson(json.decode(response.body));

  final AuthCredential credential = GithubAuthProvider.getCredential(
    token: loginResponse.accessToken,
  );

  final FirebaseUser user = (await FirebaseAuth.instance.signInWithCredential(credential)).user;
  return user;
}

class GitHubLoginRequest {
  String clientId;
  String clientSecret;
  String code;

  GitHubLoginRequest({this.clientId, this.clientSecret, this.code});

  dynamic toJson() => {
    "client_id": clientId,
    "client_secret": clientSecret,
    "code": code,
  };
}

class GitHubLoginResponse {
  String accessToken;
  String tokenType;
  String scope;

  GitHubLoginResponse({this.accessToken, this.tokenType, this.scope});

  factory GitHubLoginResponse.fromJson(Map<String, dynamic> json) =>
      GitHubLoginResponse(
        accessToken: json["access_token"],
        tokenType: json["token_type"],
        scope: json["scope"],
      );
}

主页上的代码是,

import 'package:flutter/material.dart';
import 'package:githubapp/auth/login_screen.dart';
import 'package:githubapp/screens/user_initial_screen.dart';
import 'package:githubapp/screens/user_profile_page.dart';
import 'package:firebase_auth/firebase_auth.dart';


void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  String routeToMainPage = 'mainPage';
  String routeToLoginPage = 'loginPage';
  String routeInitial = '';

  getCurrentUser() async {
    FirebaseUser user = await FirebaseAuth.instance.currentUser();
    return user;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(getCurrentUser() != null){
      routeInitial = routeToMainPage;
    } else {
      routeInitial = routeToLoginPage;
    }
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'GitHub',
     initialRoute: '/${routeInitial}',
      routes: {
        '/mainPage': (context) => MainPage(),
        '/loginPage': (context) => LoginPage(),
        '/userprofile': (context) => UserProfilePage(),
      },
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
    );
  }
}

对于在从GitHub获取ClientId和ClientSecret时必须输入的授权回调URL,appname:// auth?code = auth /

authentication github flutter deep-linking
1个回答
0
投票
void _checkDeepLink(String link) { if (link != null) { String code = link.substring(link.indexOf(RegExp('code=')) + 5); loginWithGitHub(code) .then((firebaseUser) { print("LOGGED IN AS: " + firebaseUser.displayName); Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (BuildContext context){ return MainPage(); })); }).catchError((e) { print("LOGIN ERROR: " + e.toString()); }); } }
© www.soinside.com 2019 - 2024. All rights reserved.