Building an Okta Clone (Part 2): Making the App

Now that the engine is essentially built and we are able to generate a new code, let’s actually implement this into an app. We are going to make our app as simple as possible so we can focus on what’s happening behind the scenes here. The UI will only have a text view for the seconds, a text view for the code, and a text view for the progress (We’ll make the progress indicator later, and I’ll leave the default image for fun). I’ll explain the code as if you did not have access to this repo. *If you would like to jump in feet first, the code is here https://github.com/theapphideaway/OktaClone and this article follows the Initial Commit.* ## Getting started We kept it very simple and added two files; a place for our engine (TOTPService) and a place to connect the engine to the view (TokenViewModel). If you still have the code from part one, you can just copy and paste that into TOTPService. I added some basic print statements just to peek at what is happening here but other than that it’s the same. ## Connecting the Service to the View Jumping right in we have our basic view model setup: import SwiftUI import Combine class TokenViewModel: ObservableObject { … } The main data that we are going to be sharing with our view: @Published var code: String = "--- ---" @Published var progress: Double = 1.0 @Published var secondsRemaining: Int = 30 We have our secret key which is used to generate a new code. We are going to hard code this for now just to demonstrate how to get this up and running: private let secret: Data = Data([ 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 ]) Next we have our timer logic. We are going to tick every 0.1 seconds so the progress looks smooth. If you copy this in, self?.update will give you an error. We’ll be adding that function next. private var timer: AnyCancellable? init() { startTimer() } func startTimer() { // We tick every 0.1 seconds for smooth animation timer = Timer.publish(every: 0.1, on: .main, in: .common) .autoconnect() .sink { [weak self] _ in self?.update() } } Now let’s actually make the core bridge here, which is the update function. We are going to start by getting the current time and setting our time interval to 30 (for 30 seconds): private func update() { let now = Date() let period: TimeInterval = 30 … } The major step in this function is to actually generate our code using the engine and format it: private func update() { … if let newCode = TOTP.generate(secret: secret, time: now) { let firstChunk = newCode.prefix(3) let lastChunk = newCode.suffix(3) self.code = "\(firstChunk) \(lastChunk)" } … } The last step here is to get how much remaining time we have left: private func update() { … let remaining = period - remainder self.secondsRemaining = Int(ceil(remaining)) self.progress = remaining / period } And finally to display this, there was as little UI work done as possible just to show the functionality: struct ContentView: View { @StateObject private var vm = TokenViewModel() var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("\(vm.secondsRemaining)") Text("\(vm.code)") Text("\(vm.progress)") } .padding() } } So that's it, you should be able to run it! Currently we have the secret hardcoded in our view model. We are going to generate that and store it in the keychain next.