From f3fe7d1fbd87a3d430acd10cfcb6d99881a6d739 Mon Sep 17 00:00:00 2001 From: SirusCodes Date: Sat, 3 Oct 2020 18:36:43 +0530 Subject: [PATCH] linkedin login completed --- .../lib/services/auth.dart | 80 +++++++++++++++---- .../lib/ui/screens/auth/signUpScreen.dart | 49 ++++++++---- community_app_boilerplate/pubspec.yaml | 5 +- 3 files changed, 100 insertions(+), 34 deletions(-) diff --git a/community_app_boilerplate/lib/services/auth.dart b/community_app_boilerplate/lib/services/auth.dart index dbd27d0a..dc5aec2a 100644 --- a/community_app_boilerplate/lib/services/auth.dart +++ b/community_app_boilerplate/lib/services/auth.dart @@ -2,10 +2,12 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:communityappboilerplate/services/user.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'package:github_sign_in/github_sign_in.dart'; //TODO +import 'package:linkedin_login/linkedin_login.dart'; -//TODO: import '../secret.dart'; +import '../secret.dart'; final FirebaseAuth _auth = FirebaseAuth.instance; final GoogleSignIn googleSignIn = GoogleSignIn(); @@ -18,14 +20,12 @@ class AuthService { } Stream get user { - return _auth.onAuthStateChanged - .map((FirebaseUser user) => _userFromFirebaseUser(user)); + return _auth.onAuthStateChanged.map((FirebaseUser user) => _userFromFirebaseUser(user)); } Future signInWithEmailAndPassword(String email, String password) async { try { - AuthResult result = await _auth.signInWithEmailAndPassword( - email: email, password: password); + AuthResult result = await _auth.signInWithEmailAndPassword(email: email, password: password); FirebaseUser user = result.user; _userFromFirebaseUser(user); return user.uid; @@ -35,11 +35,10 @@ class AuthService { } } - Future registerWithEmailAndPassword( - String name, String email, String password) async { + Future registerWithEmailAndPassword(String name, String email, String password) async { try { - AuthResult result = await _auth.createUserWithEmailAndPassword( - email: email, password: password); + AuthResult result = + await _auth.createUserWithEmailAndPassword(email: email, password: password); FirebaseUser user = result.user; if (user != null) { _firestore.collection('/users').document(user.uid).setData({ @@ -125,10 +124,8 @@ class AuthService { /* Future signInWithGitHub(BuildContext context) async { // Create a GitHubSignIn instance - final GitHubSignIn gitHubSignIn = GitHubSignIn( - clientId: CLIENT_ID, - clientSecret: CLIENT_SECRET, - redirectUrl: REDIRECT_URL); + final GitHubSignIn gitHubSignIn = + GitHubSignIn(clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, redirectUrl: REDIRECT_URL); // Trigger the sign-in flow final result = await gitHubSignIn.signIn(context); @@ -139,8 +136,7 @@ class AuthService { // Once signed in, return the UserCredential // - var user = - await FirebaseAuth.instance.signInWithCredential(githubAuthCredential); + var user = await FirebaseAuth.instance.signInWithCredential(githubAuthCredential); name = user.user.displayName; email = user.user.email; @@ -163,4 +159,58 @@ class AuthService { _userFromFirebaseUser(user.user); return user; }*/ + + Widget signInWithLinkedIn(BuildContext context) { + return LinkedInUserWidget( + appBar: AppBar( + title: Text('GirlScript'), + ), + destroySession: false, + redirectUrl: REDIRECT_URL_LINKEDIN, + clientId: CLIENT_ID_LINKEDIN, + clientSecret: CLIENT_SECRET_LINKEDIN, + projection: [ + ProjectionParameters.id, + ProjectionParameters.localizedFirstName, + ProjectionParameters.localizedLastName, + ProjectionParameters.firstName, + ProjectionParameters.lastName, + ProjectionParameters.profilePicture, + ], + onGetUserProfile: (LinkedInUserModel linkedInUser) { + final user = linkedInLoginFireBase(linkedInUser.token.accessToken); + if (user != null) Navigator.pop(context); + }, + catchError: (LinkedInErrorObject error) { + print('Error description: ${error.description},' + ' Error code: ${error.statusCode.toString()}'); + Navigator.pop(context); + }, + ); + } + + linkedInLoginFireBase(String token) async { + final user = await _auth.signInWithCustomToken(token: token); + + name = user.user.displayName; + email = user.user.email; + imageUrl = user.user.photoUrl; + + if (user != null) { + await _firestore.collection('/users').document(user.user.uid).setData({ + 'name': name, + 'email': email, + 'profileImageUrl': imageUrl, + }); + } + + assert(!user.user.isAnonymous); + assert(await user.user.getIdToken() != null); + + final FirebaseUser currentUser = await _auth.currentUser(); + assert(user.user.uid == currentUser.uid); + + _userFromFirebaseUser(user.user); + return user; + } } diff --git a/community_app_boilerplate/lib/ui/screens/auth/signUpScreen.dart b/community_app_boilerplate/lib/ui/screens/auth/signUpScreen.dart index 45e2763f..021469be 100644 --- a/community_app_boilerplate/lib/ui/screens/auth/signUpScreen.dart +++ b/community_app_boilerplate/lib/ui/screens/auth/signUpScreen.dart @@ -30,7 +30,8 @@ class _SignUpScreenState extends State { final AuthService _auth = AuthService(); String emailValidator(String value) { - Pattern pattern = r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$'; + Pattern pattern = + r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$'; RegExp regex = new RegExp(pattern); if (value.isEmpty) return '*Required'; if (!regex.hasMatch(value)) @@ -230,7 +231,10 @@ class _SignUpScreenState extends State { }); }, validator: (value) => value.isEmpty ? '*Required' : null, - decoration: inputTextDecoration.copyWith(labelText: "Username", icon: Icon(Icons.person_outline, color: Colors.black87, size: 30)), + decoration: inputTextDecoration.copyWith( + labelText: "Username", + icon: Icon(Icons.person_outline, + color: Colors.black87, size: 30)), ), ), SizedBox(height: height * 0.015), @@ -246,7 +250,10 @@ class _SignUpScreenState extends State { }); }, validator: (value) => value.isEmpty ? '*Required' : null, - decoration: inputTextDecoration.copyWith(labelText: "Password", icon: Icon(Icons.lock_outline, color: Colors.black87, size: 30)), + decoration: inputTextDecoration.copyWith( + labelText: "Password", + icon: Icon(Icons.lock_outline, + color: Colors.black87, size: 30)), ), ), SizedBox(height: height * 0.015), @@ -261,7 +268,10 @@ class _SignUpScreenState extends State { }); }, validator: emailValidator, - decoration: inputTextDecoration.copyWith(labelText: "Email", icon: Icon(Icons.mail_outline, color: Colors.black87, size: 30)), + decoration: inputTextDecoration.copyWith( + labelText: "Email", + icon: Icon(Icons.mail_outline, + color: Colors.black87, size: 30)), ), ), SizedBox(height: height * 0.015), @@ -306,7 +316,8 @@ class _SignUpScreenState extends State { setState(() { _loading = true; }); - dynamic result = await _auth.registerWithEmailAndPassword(_userName, _email, _password); + dynamic result = await _auth.registerWithEmailAndPassword( + _userName, _email, _password); print(result); switch (result) { case "ERROR_EMAIL_ALREADY_IN_USE": @@ -320,7 +331,8 @@ class _SignUpScreenState extends State { case "ERROR_WEAK_PASSWORD": { setState(() { - errorMsg = "The password must be 6 characters long or more."; + errorMsg = + "The password must be 6 characters long or more."; _loading = false; }); } @@ -434,15 +446,15 @@ class _SignUpScreenState extends State { elevation: 6, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)), onPressed: () async { - // await _authService.signInWithGitHub(context).whenComplete(() {//TODO:uncomment - Navigator.of(context).push( - MaterialPageRoute( - builder: (context) { - return Dashboard(); - }, - ), - ); - // });//TODO:uncomment + // await _authService.signInWithGitHub(context).whenComplete(() {//TODO:uncomment + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) { + return Dashboard(); + }, + ), + ); + // });//TODO:uncomment }, color: Colors.black, padding: EdgeInsets.all(9), @@ -474,7 +486,12 @@ class _SignUpScreenState extends State { child: RaisedButton( elevation: 6, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)), - onPressed: () {}, + onPressed: () => Navigator.push( + context, + MaterialPageRoute( + builder: (_) => _auth.signInWithLinkedIn(context), + ), + ), color: Color(0xff2867B2), padding: EdgeInsets.all(9), child: Row( diff --git a/community_app_boilerplate/pubspec.yaml b/community_app_boilerplate/pubspec.yaml index 3b408d26..dfd55e51 100644 --- a/community_app_boilerplate/pubspec.yaml +++ b/community_app_boilerplate/pubspec.yaml @@ -33,6 +33,7 @@ dependencies: uni_links: ^0.4.0 http: ^0.12.2 github_sign_in: ^0.0.3 + linkedin_login: ^1.2.2 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. @@ -44,7 +45,6 @@ dev_dependencies: # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec - # The following section is specific to Flutter. flutter: # The following line ensures that the Material Icons font is @@ -90,10 +90,8 @@ flutter: # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware. - # For details regarding adding assets from package dependencies, see # https://flutter.dev/assets-and-images/#from-packages - # To add custom fonts to your application, add a fonts section here, # in this "flutter" section. Each entry in this list should have a # "family" key with the font family name, and a "fonts" key with a @@ -116,3 +114,4 @@ flutter: # # For details regarding fonts from package dependencies, # see https://flutter.dev/custom-fonts/#from-packages +