GithubHelp home page GithubHelp logo

shoheikawano / kmpauth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mirzemehdi/kmpauth

1.0 0.0 0.0 309 KB

Kotlin Multiplatform Authentication Library targetting android and iOS

Home Page: https://mirzemehdi.com/KMPAuth/

License: Apache License 2.0

Ruby 3.29% Kotlin 94.27% Swift 2.44%

kmpauth's Introduction

KMPAuth - Kotlin Multiplatform Authentication Library

Build Kotlin Maven Central

badge-android badge-ios

Simple and easy to use Kotlin Multiplatform Authentication library targeting iOS and Android. Supporting Google, Apple, Github authentication integrations using Firebase.
Because I am using KMPAuth in FindTravelNow production KMP project, I'll support development of this library :).
Related blog post: Integrating Google Sign-In into Kotlin Multiplatform
You can check out Documentation for full library api information.

Sample App and Code

SampleApp

@Composable
fun AuthUiHelperButtonsAndFirebaseAuth(
    modifier: Modifier = Modifier,
    onFirebaseResult: (Result<FirebaseUser?>) -> Unit,
) {
    Column(modifier = modifier,verticalArrangement = Arrangement.spacedBy(10.dp)) {

        //Google Sign-In Button and authentication with Firebase
        GoogleButtonUiContainerFirebase(onResult = onFirebaseResult) {
            GoogleSignInButton(modifier = Modifier.fillMaxWidth()) { this.onClick() }
        }

        //Apple Sign-In Button and authentication with Firebase
        AppleButtonUiContainer(onResult = onFirebaseResult) {
            AppleSignInButton(modifier = Modifier.fillMaxWidth()) { this.onClick() }
        }

        //Github Sign-In with Custom Button and authentication with Firebase
        GithubButtonUiContainer(onResult = onFirebaseResult) {
            Button(onClick = { this.onClick() }) { Text("Github Sign-In (Custom Design)") }
        }

    }
}

You can check out more sample codes here.

Features

Installation

KMPAuth is available on Maven Central. In your root project build.gradle.kts file (or settings.gradle file) add mavenCentral() to repositories.

repositories { 
  mavenCentral()
}

Then in your shared module add desired dependencies in commonMain. Latest version: Maven Central.

sourceSets {
  commonMain.dependencies {
    implementation("io.github.mirzemehdi:kmpauth-google:<version>") //Google One Tap Sign-In 
    implementation("io.github.mirzemehdi:kmpauth-firebase:<version>") //Integrated Authentications with Firebase
    implementation("io.github.mirzemehdi:kmpauth-uihelper:<version>") //UiHelper SignIn buttons (AppleSignIn, GoogleSignInButton)

  }
}

You will also need to include Google Sign-In and/or FirebaseAuth library to your ios app using Swift Package Manager or Cocoapods.

Note: If in iOS you get MissingResourceException, I wrote solution in this issue's comment section.


Google Sign-In

For Google Sign-In you can either use only one-tap sign in functionality, or also implementing firebase google authentication integration to that. You need to set up OAuth 2.0 in Google Cloud Platform Console. For steps you can follow this link. Pro Easy Tip: If you use Firebase and enable Google Sign-In authentication in Firebase it will automatically generate OAuth client IDs for each platform, and one will be Web Client ID which will be needed for identifying signed-in users in backend server.

Platform Setup

Create GoogleAuthProvider instance by providing Web Client Id as a serverID on Application start.

GoogleAuthProvider.create(credentials = GoogleAuthCredentials(serverId = WebClientId))
Android
Android Setup

There is not any platform specific setup in Android side.

iOS
iOS Setup

Add clientID, and serverId to your Info.plist file as below:

<key>GIDServerClientID</key>
<string>YOUR_SERVER_CLIENT_ID</string>

<key>GIDClientID</key>
<string>YOUR_IOS_CLIENT_ID</string>
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
    </array>
  </dict>
</array>

And finally, you need the code below to implement application delegate function calls on the Swift side.

import SwiftUI
import shared
import GoogleSignIn

class AppDelegate: NSObject, UIApplicationDelegate {

    func application(
      _ app: UIApplication,
      open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]
    ) -> Bool {
      var handled: Bool

      handled = GIDSignIn.sharedInstance.handle(url)
      if handled {
        return true
      }

      // Handle other custom URL types.

      // If not handled by this app, return false.
      return false
    }


}

@main
struct iOSApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
   var body: some Scene {
      WindowGroup {
            ContentView().onOpenURL(perform: { url in
                GIDSignIn.sharedInstance.handle(url)
            })
      }
   }
}

Usage

After configuring above steps this is how you can use:

//Google Sign-In with Custom Button (only one tap sign-in functionality)
GoogleButtonUiContainer(onGoogleSignInResult = { googleUser ->
  val idToken = googleUser?.idToken // Send this idToken to your backend to verify
}) {
  Button(onClick = { this.onClick() }) { Text("Google Sign-In(Custom Design)") }
}

Google Sign-In Button and authentication with Firebase. You need to implement kmpauth-uihelper dependency

GoogleButtonUiContainerFirebase(onResult = onFirebaseResult) {
  GoogleSignInButton(modifier = Modifier.fillMaxWidth()) { this.onClick() }
}

Google Sign-In IconOnly Button and authentication with Firebase. You need to implement kmpauth-uihelper dependency

GoogleButtonUiContainerFirebase(onResult = onFirebaseResult) {
  GoogleSignInButtonIconOnly(onClick = { this.onClick() })
}

Apple Sign-In

After enabling and configuring Apple Sign-In in Firebase, make sure you added "Sign In with Apple" capability in XCode. Then, you can use it as below in your @Composable function:

//Apple Sign-In with Custom Button and authentication with Firebase
AppleButtonUiContainer(onResult = onFirebaseResult) {
  //Any View, you just need to delegate child view's click to this UI Container's click method
  Button(onClick = { this.onClick() }) { Text("Apple Sign-In (Custom Design)") }
}

Apple Sign-In with AppleSignInButton. You need to implement kmpauth-uihelper dependency

AppleButtonUiContainer(onResult = onFirebaseResult) {
  AppleSignInButton(modifier = Modifier.fillMaxWidth()) { this.onClick() }
}

Apple Sign-In IconOnly Button. You need to implement kmpauth-uihelper dependency

AppleButtonUiContainer(onResult = onFirebaseResult) {
  AppleSignInButtonIconOnly(onClick = { this.onClick() })
}

Github Sign-In

After enabling and configuring Github Sign-In in Firebase, you can use it as below in your @Composable function:

//Github Sign-In with Custom Button and authentication with Firebase
GithubButtonUiContainer(onResult = onFirebaseResult) {
  //Any View, you just need to delegate child view's click to this UI Container's click method
  Button(onClick = { this.onClick() }) { Text("Github Sign-In (Custom Design)") }
}

kmpauth's People

Contributors

mirzemehdi avatar

Stargazers

BHawk  avatar

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.