Hyphen
  • Hyphen
  • Concepts
    • Auth Methods
    • Hyphen Multi-Sig Account
      • Device Key
      • Recovery Key
      • Server Key
    • Paymaster
    • Hybrid Custody
  • Mechanism Paper
  • iOS SDK
    • Quick Start
    • Authenticating Users
    • Using Hyphen UI Kit
      • Account Management Screen
    • Sample Project
  • Android SDK
    • Quick Start
    • Authenticating Users
    • Handling 2FA / Transaction Request
    • Using Hyphen UI Kit
    • Sending Transactions
  • Flutter SDK
    • Quick Start
    • Authenticating Users
    • Using Hyphen UI Kit
    • Sending Transactions
    • Example App
  • Hybrid Custody
    • Into the Hybrid Custody
  • Without Using SDK
    • Authenticating Users
    • Handling 2FA Push
  • REST API
    • API Reference
      • Account
      • Auth
      • Device
      • Key
      • Sign
    • Swagger
Powered by GitBook
On this page

Was this helpful?

  1. iOS SDK

Authenticating Users

Use HyphenAuthenticate to sign in users

PreviousQuick StartNextUsing Hyphen UI Kit

Last updated 1 year ago

Was this helpful?

Authentication services are provided in the module of the SDK. You can sign in, create an account, or get the user's key and account information through the module.

Signing In

HyphenAuthenticate
https://github.com/hyphen-at/swirl-ios-client/blob/09cfc9e925517cf31fd40218e1a715a82ffb05cb/Projects/Feature/SignIn/Sources/SignInCore.swift
import ComposableArchitecture
import Dependencies
import SwirlAuth
import SwirlBlockchain

public struct SignIn: ReducerProtocol {
    public struct State: Equatable {
        public var isAuthenticating: Bool = false
        public var isHyphenAuthenticateChecking: Bool = true

        public init() {}
    }

    public enum Action {
        case onHyphenAuthenticateChecking

        case onLoggedIn
        case onSignInNeed

        case onContinueWithGoogleButtonClick

        case onNameCardCreateNeed
        case goToNameCardList

        case onError
    }

    @Dependency(\.swirlAuthClient) var authClient
    @Dependency(\.swirlBlockchainClient) var blockchainClient

    public var body: some ReducerProtocol<State, Action> {
        Reduce { state, action in
            switch action {
            case .onHyphenAuthenticateChecking:
                return .run { dispatch in
                    if try await authClient.isHyphenLoggedIn() {
                        try await Task.sleep(nanoseconds: 1_000_000_000)
                        await dispatch(.onLoggedIn)
                    } else {
                        await dispatch(.onSignInNeed)
                    }
                }
            case .onLoggedIn:
                return .run { dispatch in
                    try await blockchainClient.fetchFlowAccount()

                    do {
                        let myProfile = try await blockchainClient.getMyNameCard()

                        if myProfile == nil {
                            await dispatch(.onNameCardCreateNeed)
                        } else {
                            await dispatch(.goToNameCardList)
                        }
                    } catch {
                        await dispatch(.onNameCardCreateNeed)
                    }
                }
            case .onSignInNeed:
                state.isHyphenAuthenticateChecking = false
                return .none
            case .onContinueWithGoogleButtonClick:
                state.isAuthenticating = true
                return .run { dispatch in
                    do {
                        try await authClient.signInWithGoogle()
                        await dispatch(.onLoggedIn)
                    } catch {
                        await dispatch(.onError)
                    }
                }
            case .onError:
                state.isAuthenticating = false
                return .none
            default:
                return .none
            }
        }
    }

    public init() {}
}