Skip to content

Latest commit

 

History

History
108 lines (71 loc) · 3.29 KB

README.md

File metadata and controls

108 lines (71 loc) · 3.29 KB

MSALSwiftUI

This project intends to give an example of how the MSAL iOS library can be used in a SwiftUI App. It uses the same MSAL APIs but separated from the View. This projects reuses the sample code provided by Microsoft within their MSAL iOS Sample Project:

https://github.com/Azure-Samples/ms-identity-mobile-apple-swift-objc

Important: I do not guarantee that this project works 100% the same way as the MS Sample does! But I hope it gives some useful input while trying to rewrite the UIKIt sample in SwiftUI :)

Use your own Azure App

To use this project with your own registered Azure App you need to change the following:

  • Project Bundle Identifier (<app_name> -> <your_target> -> General -> Bundle Identifier)
  • Replace the credentials in `MSAuthCredentials.swift
    struct MSAuthCredentials {
      static let applicationId = "66855f8a-60cd-445e-a9bb-8cd8eadbd3fa" // aka clientID
      static let directoryId = "common" // aka tenantID
    }

Overview

The MSAuthAdapter contains the business logic for loading the account info silently or with interaction. While the MSAuthProxy encapsulates all the MSAL APIs, which mainly stayed the same as in the above mentioned UIKit sample.

class MSAuthState: ObservableObject {
    @Published var account: Account?
}

struct Account: Equatable {
    let email: String?
}

struct AuthView: View {
    @EnvironmentObject var msAuthState: MSAuthState

    private let msAuthAdapter: MSAuthAdapterProtocol = resolve()
    
    var body: some View {
        VStack(spacing: 40) {
            Text(msAuthState.account?.email ?? "Signed out")
                .frame(maxWidth: .infinity, alignment: .trailing)
                .foregroundColor(.gray)

            Button("Login with interaction") {
                msAuthAdapter.login(withInteraction: true)
            }

            Button("Logout") {
                msAuthAdapter.logout()
            }
            .disabled(msAuthState.account == nil)

            Button("Load device mode") {
                msAuthAdapter.loadDeviceMode()
            }
            
            Text("See logged console output for more info")
                .font(.caption)

            Spacer()
        }
        .padding()
    }
}

Dependencies

  • Resolver for simple dependency injection

Roadmap

I do not intend to update this project or keep it up to date. Microsoft itself is likely to introduce their own Sample Project for SwiftUI soon (state August 2021). See issue regarding SwiftUI Sample for a similar project Azure-Samples/active-directory-b2c-ios-swift-native-msal#47

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch
  3. Commit your Changes
  4. Push to the Branch
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Alexander Schmutz - [email protected]

Acknowledgements