Coder Social home page Coder Social logo

xamarin.googleauth's Introduction

Xamarin.GoogleAuth

Cross platform plugin for doing Google Authentication for Xamarin iOS and Android apps

Introduction

Google has recently updated its security restrictions for OAuth flow. They are not going to allow native web-views to initiate OAuth flows, but rather are encouraging people to use the OS browsers to do so. The current implementation of the Xamarin.Auth mechanism still uses web-views for initiating the Auth flow which is deprecated and will not work. This plugin attempts to solve that problem by implementing the recommended mechanism for doing Google Auth on native iOS and Android applications and porting it to Xamarin.

Integration guide

Here are the basic steps that you need to follow to add Google Authentication into your Xamarin forms application

  1. Setup your Application on the Google developer's console to use OAuth2. Guide here. Make sure you select the Web Application option.

  2. Download this repository and copy over the GoogleAuth, Plugin.GoogleAuth, Plugin.GoogleAuth.Android and Plugin.GoogleAuth.iOS folders into your application and add them into your solution in VS or Xamarin Studio.

  3. Add references to the Plugin.GoogleAuth and Plugin.GoogleAuth.Abstractions projects into your PCL project.

  4. Add references to the Plugin.GoogleAuth.Android and Plugin.GoogleAuth.Abstractions projects into your Android project.

  5. Add references to the Plugin.GoogleAuth.iOS and Plugin.GoogleAuth.Abstractions projects into your iOS project. Also add a reference to the GoogleSignin project which resides inside the Plugin.GoogleAuth.iOS/GoogleSignInWrapper folder.

  6. Add the Xamarin Google Play Services - Base, Basement and Auth Nuget packages (recommended version : 32.961.0) into your Android project.

  7. In your iOS project, open the Build Settings pane and add these compiler flags under Additional mtouch arguments:

         -gcc_flags "-ObjC -framework CoreText -framework SafariServices -framework SystemConfiguration"
    
  8. Also, make sure to add the URL types required for handling Google SignIn in the Info.plist file of your iOS application.

  9. Inside your platform specific projects initialize the Google Authentication service as shown below.

     	IGoogleAuthenticationService _service;
     	_service = CrossGoogleAuth.Current;
     	Dictionary<string, object> googleSignInConfig = new Dictionary<string, object> {
     		{"clientId", "<client-id>"},
     		{"context", this} // Android only
     	};
     	_service.Init(googleSignInConfig);
    
  10. For iOS, override the OpenUrl method call in the AppDelegate.cs file and call the HandleURL method of the plugin.

     public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
     {
     	string sourceApplication = options["UIApplicationOpenURLOptionsSourceApplicationKey"].ToString();
     	NSObject annotation = options["UIApplicationOpenURLOptionsAnnotationKey"] ?? new NSObject();
     	return _service.HandleURL(url, sourceApplication, annotation);
     }
    
  11. For Android, override the OnActivityResult method call in the MainActivity.cs file and call the OnActivityResult method of the Android plugin.

     protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
     {
     	base.OnActivityResult(requestCode, resultCode, data);
     	var googleAuthService = _service as GoogleAuthImpl;
     	googleAuthService.OnActivityResult(requestCode, resultCode, data);
     }
    

Usage

Init

Initialize the Google Auth plugin by providing the required keys in the IGoogleAuthenticationService.Init() method. Currently supported keys :

  1. clientId : Client ID for authentication with Google Auth
  2. context : Application context (Android Only)

Connect and Signin

To connect to the Google SignIn service and provide a way for the user to sign in to your application, use the IGoogleAuthenticationService.SignIn() method call. If the application has already connected, the plugin will try and perform a sign in silently. To sign out, please call the IGoogleAuthenticationService.SignOut()

Callbacks

To get the status of the sign in call, please implement the IGoogleAuthenticationCallbacks interface and its methods.

void OnConnectionSucceeded();
void OnConnectionFailed(string errorMessage);

Set the Authentication callbacks via the IGoogleAuthenticationCallbacks.SetAuthenticationCallbacks() method.

Fetch Information

Your application can fetch the following user information in the OnConnectionSucceeded method.

  1. Use the IGoogleAuthenticationCallbacks.GetIdToken method to get the ID token for the signed in user. This ID token can be used to authenticate the user on the server side.
  2. Use the IGoogleAuthenticationCallbacks.GetAccountName method to get the name of the signed in user.
  3. Use the IGoogleAuthenticationCallbacks.Disconnect method to disconnect the user account from this app in Google Auth.

Gotchas

  1. iOS requires the client-id of an iOS client application created on the Google Developer console.
  2. Android on the other hand requires the client-id of a Web application.

TODOs

  1. Convert this into a NuGet package !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.