Giter Club home page Giter Club logo

msalswiftui's Introduction

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

msalswiftui's People

Contributors

alschmut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ascot31 joietej

msalswiftui's Issues

'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

I am getting this warning in the IDE:

'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

on this line of code:

let window = UIApplication.shared.windows.filter { $0.isKeyWindow }.first

and when I try:

let window = UIWindowScene.windows.filter { $0.isKeyWindow }.first

I get this error in the IDE:

Instance member 'windows' cannot be used on type 'UIWindowScene'; did you mean to use a value of this type instead?

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.