Skip to content

Commit

Permalink
Added:Integration with firebase #1
Browse files Browse the repository at this point in the history
  • Loading branch information
tkshsbcue committed Jun 1, 2024
1 parent 4ef4376 commit 268a532
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 12 deletions.
12 changes: 12 additions & 0 deletions Tickle.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
E41219E72C06FC1500873F6B /* FirebaseAuth in Frameworks */ = {isa = PBXBuildFile; productRef = E41219E62C06FC1500873F6B /* FirebaseAuth */; };
E41219E92C06FC1500873F6B /* FirebaseFirestore in Frameworks */ = {isa = PBXBuildFile; productRef = E41219E82C06FC1500873F6B /* FirebaseFirestore */; };
E41219EB2C06FC1500873F6B /* FirebaseFirestoreSwift in Frameworks */ = {isa = PBXBuildFile; productRef = E41219EA2C06FC1500873F6B /* FirebaseFirestoreSwift */; };
E41401382C0B48A40025A519 /* RegisterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41401372C0B48A40025A519 /* RegisterViewModel.swift */; };
E414013A2C0B6C650025A519 /* user.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41401392C0B6C650025A519 /* user.swift */; };
E414013C2C0B6EC60025A519 /* extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E414013B2C0B6EC60025A519 /* extension.swift */; };
E42CD48C2C074640003F50D3 /* GoogleService-Info (1).plist in Resources */ = {isa = PBXBuildFile; fileRef = E42CD48B2C074640003F50D3 /* GoogleService-Info (1).plist */; };
E42CD4902C07497F003F50D3 /* LoginViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E42CD48F2C07497F003F50D3 /* LoginViewModel.swift */; };
E42CD4932C074996003F50D3 /* MainViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E42CD4922C074996003F50D3 /* MainViewModel.swift */; };
Expand Down Expand Up @@ -53,6 +56,9 @@
E41219D22C05EE8E00873F6B /* TickleUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TickleUITests.swift; sourceTree = "<group>"; };
E41219D42C05EE8E00873F6B /* TickleUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TickleUITestsLaunchTests.swift; sourceTree = "<group>"; };
E41219E12C05F30200873F6B /* Register.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Register.swift; sourceTree = "<group>"; };
E41401372C0B48A40025A519 /* RegisterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegisterViewModel.swift; sourceTree = "<group>"; };
E41401392C0B6C650025A519 /* user.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = user.swift; sourceTree = "<group>"; };
E414013B2C0B6EC60025A519 /* extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = extension.swift; sourceTree = "<group>"; };
E42CD48B2C074640003F50D3 /* GoogleService-Info (1).plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info (1).plist"; path = "../../../Downloads/GoogleService-Info (1).plist"; sourceTree = "<group>"; };
E42CD48F2C07497F003F50D3 /* LoginViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewModel.swift; sourceTree = "<group>"; };
E42CD4922C074996003F50D3 /* MainViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -110,10 +116,13 @@
isa = PBXGroup;
children = (
E41219B62C05EE8D00873F6B /* TickleApp.swift */,
E414013B2C0B6EC60025A519 /* extension.swift */,
E41219E12C05F30200873F6B /* Register.swift */,
E41401372C0B48A40025A519 /* RegisterViewModel.swift */,
E41219B82C05EE8D00873F6B /* loginView.swift */,
E42CD48F2C07497F003F50D3 /* LoginViewModel.swift */,
E42CD4922C074996003F50D3 /* MainViewModel.swift */,
E41401392C0B6C650025A519 /* user.swift */,
E41219BA2C05EE8E00873F6B /* Assets.xcassets */,
E41219BC2C05EE8E00873F6B /* Tickle.entitlements */,
E42CD48B2C074640003F50D3 /* GoogleService-Info (1).plist */,
Expand Down Expand Up @@ -287,10 +296,13 @@
buildActionMask = 2147483647;
files = (
E42CD4932C074996003F50D3 /* MainViewModel.swift in Sources */,
E41401382C0B48A40025A519 /* RegisterViewModel.swift in Sources */,
E42CD4902C07497F003F50D3 /* LoginViewModel.swift in Sources */,
E41219E22C05F30200873F6B /* Register.swift in Sources */,
E414013A2C0B6C650025A519 /* user.swift in Sources */,
E41219B92C05EE8D00873F6B /* loginView.swift in Sources */,
E41219B72C05EE8D00873F6B /* TickleApp.swift in Sources */,
E414013C2C0B6EC60025A519 /* extension.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
10 changes: 5 additions & 5 deletions Tickle/LoginViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
//
// Created by Kumar Tanay on 29/05/24.
//

import FirebaseAuth
import Foundation
import Firebase
class LoginViewModel:ObservableObject{
@Published var email = ""
@Published var password = ""
@Published var errormsg = ""
init(){}

func login(){
guard !email.isEmpty, !password.isEmpty else{
return
}
print("Hello there")
Auth.auth().signIn(withEmail:email,password: password)

}
func validate(){}

Expand Down
11 changes: 5 additions & 6 deletions Tickle/Register.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import SwiftUI

struct Register: View {
@State var name = ""
@State var email = ""
@State var password = ""
@StateObject var viewModel = RegisterViewModel()

var body: some View {
NavigationView{
VStack{
Form{
TextField("Name",text:$name)
TextField("Email",text:$email)
SecureField("Password",text:$password)
TextField("Name",text:$viewModel.name)
TextField("Email",text:$viewModel.email)
SecureField("Password",text:$viewModel.password )

}
Button{
Expand Down
60 changes: 60 additions & 0 deletions Tickle/RegisterViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// RegisterViewModel.swift
// Tickle
//
// Created by Kumar Tanay on 01/06/24.
//

import Foundation
import FirebaseAuth
import Firebase


class RegisterViewModel: ObservableObject{

//state property
@Published var name = ""
@Published var email = ""
@Published var password = ""
init (){}

func register(){
guard validate() else{
return
}


Auth.auth().createUser(withEmail: email, password: password){result, error in
guard let userId = result?.user.uid else{
return
}
self.insertUserRecord(id: userId)
}


}

private func insertUserRecord(id:String){
let newUser = User(id:id,name: name,email: email,joined: Date().timeIntervalSince1970)
let db = Firestore.firestore()
db.collection("users").document(id).setData(newUser.asDictionary())
}


private func validate() -> Bool {
guard !name.trimmingCharacters(in: .whitespaces).isEmpty,
!email.trimmingCharacters(in: .whitespaces).isEmpty,
!password.trimmingCharacters(in: .whitespaces).isEmpty else{
return false
}
guard email.contains("@") && email.contains(".")else{
return false
}
guard password.count >= 6 else{
return false
}
return true
}


}
1 change: 1 addition & 0 deletions Tickle/TickleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
@main
struct TickleApp: App {
var body: some Scene {

WindowGroup {
LoginView()
}
Expand Down
23 changes: 23 additions & 0 deletions Tickle/extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// extension.swift
// Tickle
//
// Created by Kumar Tanay on 01/06/24.
//

import Foundation


extension Encodable{
func asDictionary() -> [String:Any]{
guard let data = try? JSONEncoder().encode(self) else{
return [:]
}
do{
let json = try JSONSerialization.jsonObject(with: data) as? [String:Any]
return json ?? [:]
}catch{
return [:]
}
}
}
6 changes: 5 additions & 1 deletion Tickle/loginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ struct LoginView: View {
RoundedRectangle(cornerRadius:1).frame(width: 600,height:300).offset(y:-100).foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
Text("Free VBUCK").offset(y:-100).bold().foregroundColor(.green)
ZStack{

Form{
if !viewModel.errormsg.isEmpty {
Text(viewModel.errormsg).foregroundColor(Color.red)
}
TextField("Name",text:$viewModel.email)
SecureField("Password",text:$viewModel.password)
NavigationLink(destination:Register()){
Text("New Here")
}
}.offset(y:-100).frame(height:230)
}.offset(y:-50).frame(height:300)
Button{
viewModel.login()
}label:{
Expand Down
16 changes: 16 additions & 0 deletions Tickle/user.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// user.swift
// Tickle
//
// Created by Kumar Tanay on 01/06/24.
//

import Foundation


struct User: Codable{
let id: String
let name:String
let email:String
let joined:TimeInterval
}

0 comments on commit 268a532

Please sign in to comment.