import SwiftUI //MARK: Intergrating Apple Sign in import AuthenticationServices import GoogleSignIn import GoogleSignInSwift import Firebase struct Login: View { @StateObject var LoginModel: LoginViewModel = .init() var body: some View { ZStack{ Image("iOS-16-wallpaper") .resizable().aspectRatio(contentMode: .fill) .frame(width: UIScreen.main.bounds.width) .overlay(Color.black.opacity(0.35)) .ignoresSafeArea() ScrollView(.vertical, showsIndicators: false) { VStack (alignment: .leading, spacing: 15) { Image(systemName: "triangle") .font(.system(size: 38)) .foregroundColor(.indigo) (Text("Welkom,") .foregroundColor(.black) + Text("\nLogin to continue") .foregroundColor(.gray)) .font(.title) .fontWeight(.semibold) .lineSpacing(10) .padding(.top,20) .padding(.trailing,15) // MARK: Custom TextField CustomTextField(hint: "+31 6 98675643", text: $LoginModel.mobileNo) .disabled(LoginModel.showOTPField) .opacity(LoginModel.showOTPField ? 0.4 : 1) .overlay(alignment: .trailing, content: { Button("Change"){ withAnimation(.easeInOut){ LoginModel.showOTPField = false LoginModel.otpCode = "" LoginModel.CLIENT_CODE = "" } } .font(.caption) .foregroundColor(.indigo) .opacity(LoginModel.showOTPField ? 1 : 0) .padding(.trailing,15) }) .padding(.top,50) CustomTextField(hint: "OTP Code", text: $LoginModel.otpCode) .disabled(!LoginModel.showOTPField) .opacity(!LoginModel.showOTPField ? 0.4 : 1) .padding(.top,20) Button(action: LoginModel.showOTPField ? LoginModel.verifyOTPCode : LoginModel.getOTPCode){ HStack(spacing: 15){ Text(LoginModel.showOTPField ? "Verify Code" : "Get Code") .fontWeight(.semibold) .contentTransition(.identity) Image(systemName: "line.diagonal.arrow") .font(.title3) .rotationEffect(.init(degrees: 50)) } .foregroundColor(.black) .padding(.horizontal,25) .padding(.vertical) .background { RoundedRectangle(cornerRadius: 10, style: .continuous) .fill(.black.opacity(0.05)) } } .padding(.top,30) Text("(OR)") .foregroundColor(.gray) .frame(maxWidth: .infinity) .padding(.top,30) .padding(.bottom,20) .padding(.leading,-60) .padding(.horizontal) .overlay{ HStack(spacing: 8){ //MARK: Custom Apple Sign in Button CustomButton() .overlay{ SignInWithAppleButton { (request) in LoginModel.nonce = $LoginModel.randomNonceString() request.requestedScopes = [.email,.fullName] request.nonce = $LoginModel.sha256(LoginModel.nonce) } onCompletion: {(result) in switch result{ case .success(let user): print("Success") guard let credential = user.credential as? ASAuthorizationAppleIDCredential else{ print("error with firebase") return } LoginModel.appleAuthenticate(credential: credential) case.failure(let error): print(error.localizedDescription) } } .frame(height: 55) .clipShape(Capsule()) } .clipped() //MARK: Custom Google Sign in Button CustomButton(isGoogle: true) .overlay{ //MARK: We Have Native Sign in Button if let clientID = FirebaseApp.app()?.options.clientID{ GoogleSignInButton{ GIDSignIn.sharedInstance.signIn(with: .init(clientID: clientID), presenting: UIApplication.shared.rootController()){user,error in if let error = error{ print(error.localizedDescription) return } //MARK: Logging Google Users into Firebase if let user{ LoginModel.logGoogleUsers(user: user) } } } .blendMode(.overlay) } } .clipped() } .padding(.leading,-60) .frame(maxWidth: .infinity) } .padding(.leading,60) .padding(.vertical,15) } .alert(LoginModel.errorMessage, isPresented: $LoginModel.showError) { } } } .overlay @ViewBuilder func CustomButton(isGoogle: Bool = false) -> some View{ HStack{ Group{ if isGoogle{ Image("Google-Logo-Transparent-Background.png") .resizable() .renderingMode(.template) }else{ Image(systemName: "applelogo") .resizable() } } .aspectRatio(contentMode: .fit) .frame(width: 25, height: 25) .frame(height: 45) .foregroundColor(.white) Text("\(isGoogle ? "Google" : "Apple")Sign in") .font(.callout) .foregroundColor(.white) .lineLimit(1) } .padding(.horizontal,15) .background{ RoundedRectangle(cornerRadius: 10, style: .continuous) } } struct Login_Previews: PreviewProvider { static var previews: some View { ContentView() } } } }
Replies
7
Boosts
0
Views
6.9k
Participants
9