Coder Social home page Coder Social logo

familygw / swift-ui-auth-flow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apramodya/swift-ui-auth-flow

0.0 0.0 0.0 17 KB

This is an demo to show how to do an authentication flow in SwiftUI. Using PassthroughSubject provided by Combine Framework.

Swift 100.00%

swift-ui-auth-flow's Introduction

SwiftUI Authentication Flow

Handle authentication easily with a PassthroughSubject

This is a demo to show how you can do a login flow in SwiftUI. Includes how to take into a View after logged in successfully and how to take back to Login View after logging out from the app.

Used frameworks

-- SwiftUI

-- Combine

Features

  • Login

-- On Success -> go to Home View

-- On Fail -> give an Alert

  • Logout

-- Go back to Login View

Functionality

AppManager It has the variables to keep track on whether user is Logged In. IsAuthenticated() is returning whether user is logged in by checking the Token is available yet. Authenticated is used as a PassthroughSubject of Bool type. We can send values (true/false) when ever user is logged or logged out. A Group can listen to its changes. It will be coved below

ContentView All View are embedded in a Group. A group can listen to Publishers such as a PassthroughSubject in the onReceive performer and do actions.

LoginView It has two TextFields to get inputs (email/passowrd) and binded to Publishers in its ViewModel. On the Button click, it will run the loginUser() method in the VM.

LoginViewModel This has the logic to check whether login credentials are correct in loginUser() method. For the demo purpose, credentials are hardcoded. If the login is successful, AppManager.Authenticated.send(true) is used. This sends a True value to the PassthroughSubject declared in the AppManager. So any listner is notified. (ContentView Group is listening for changes on this). Also in this method, a UserDefaults value is created with the key token. It is same as the accessToken we use in real apps.

HomeView This is the view shown when user is authenticated. This has a Logout button and triggers logoutUser() method in its VM.

HomeViewModel Once user press on Logout button, logoutUser() is called. It removed the token from the UserDefaults and emmits a false value to AppManagers Authenticated PassthroughSubject.

License

Free to modify/use

swift-ui-auth-flow's People

Contributors

apramodya 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.