Create Passkey Over Open Session
The simplest and most unrestricted user signup flow, where username and passkey credential is created immediately upon successful sdk called.
To ensure open session, make sure you application is setup with Registration Requires Authentication Token is disabled in your application settings.
When this setting is disabled, users can be created and passkeys can be generated without requiring any authorization tokens. This allows you to simplify the registration in public-facing applications that don’t need strict authentication during the sign-up process.
Here is how it might look like:
Prerequisites
- Create an application to obtain a base URL
Setup SDK
- Javascript
- Kotlin
- Swift
npm i @loginid/websdk3
Import and initialize an instance:
import { LoginIDWebSDK } from "@loginid/websdk3";
const lid = new LoginIDWebSDK({
baseUrl: process.env.LOGINID_BASE_URL,
});
Initialize a singleton configuration for the LoginID service in the application's onCreate
method.
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
// Initialize the singleton instance
val config = LoginIDServiceConfig("<BASE_URL>")
LoginID.configure(config).client()
}
}
Configure the singleton instance of LIDClient
with a base URL during the app's launch in the AppDelegate
.
import LoginID
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
// Initialize the singleton instance
LIDClient.shared.configure(baseURL: "<BASE_URL>")
return true
}
}
Creating a Passkey For a New User
Call the createPasskey method by passing in the email or username.
- Javascript
- Kotlin
- Swift
import { LoginIDWebSDK } from "@loginid/websdk3";
// Obtain credentials from LoginID Tenant
const BASE_URL = process.env.BASE_URL;
const APP_ID = process.env.APP_ID;
// Initialize the SDK with your configuration
const config = {
baseUrl: BASE_URL,
appId: APP_ID,
};
// Use the SDK components for signup and signin
const lid = new LoginIDWebSDK(config);
// Button click handler
async function handleSignupButtonClick() {
const username = "billy@loginid.io";
try {
// Sign up with a passkey
const authResult = await lid.createPasskey(username);
setAuthUser(user);
} catch (error) {
// Handle errors
console.error("Error during signup:", error);
}
}
// Attach the click handler to a button
const signupButton = document.getElementById("signupButton");
signinButton.addEventListener("click", handleSigninButtonClick);
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import loginid.LoginID
import services.LoginIDServiceConfig
import services.exceptions.LoginIDError
import services.modals.CreatePasskeyOptions
class MainActivity : AppCompatActivity() {
companion object {
private val config = LoginIDServiceConfig("<BASE_URL>", "<APP_ID>")
val lid = LoginID(config)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val registerButton = findViewById<Button>(R.id.registerBtn)
registerButton.setOnClickListener {
registerUser("billy@loginid.io", null)
}
}
private fun registerUser(username: String, options: CreatePasskeyOptions?) {
lifecycleScope.launch {
try {
lid.registerWithPasskey(this@MainActivity, username, options)
} catch (e: Exception) {
if (e is LoginIDError) {
// Handle LoginIDError specifically
} else {
// Handle other exceptions
}
}
}
}
}
// AppDelegate.swift
import UIKit
import PasskeySDK
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Configure the LoginID SDK with your base URL and app ID
LIDClient.shared.configure(baseURL: "<BASE_URL>", appID: "<APP_ID>")
return true
}
// Include any other necessary AppDelegate methods...
}
// ViewController.swift
import UIKit
import PasskeySDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Your UI setup code here...
}
@IBAction func registerButtonTapped(_ sender: Any) {
// Assuming you have a button in your UI that triggers this action
let username = "billy@loginid.io"
let options = LIDRegistrationOptions() // or nil
LIDClient.shared.registerWithPasskey(
anchor: self.view.window!,
username: username,
options: options,
onComplete: { authResponse, error in
DispatchQueue.main.async {
if let error = error {
// Handle error scenario
print("Registration failed with error: \(error.errorMessage)")
} else {
// Handle successful registration
print("Registration successful with response: \(response.jwtAccess)")
}
}
}
)
}
}