-
Notifications
You must be signed in to change notification settings - Fork 1
/
SignUpViewController.swift
82 lines (59 loc) · 3.06 KB
/
SignUpViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// SignUpViewController.swift
// vitalizer
//
// Created by riddhi gupta on 10/10/20.
// Copyright © 2020 riddhi gupta. All rights reserved.
//
import UIKit
import FirebaseAuth
import FirebaseDatabase
class SignUpViewController: UIViewController {
var ref : DatabaseReference!
@IBOutlet weak var signUp: UIButton!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var confirmPass: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
signUp.applyDesign()
signUp.setGradientBackground(colorOne: Colors.buttonRight, colorTwo: Colors.buttonLeft)
view.setGradientBackground(colorOne: Colors.bottomRight, colorTwo: Colors.topLeft)
}
@IBAction func signUp(_ sender: Any) {
if password.text != confirmPass.text {
let alertController = UIAlertController(title: "Password Incorrect", message: "Please re-type password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
else{
signIn()
}
}
func signIn() {
Auth.auth().createUser(withEmail: email.text!, password: password.text!) { (authResult, error) in
if error == nil {
guard let emailText = self.email.text, !emailText.isEmpty else { return}
guard let passwordText = self.password.text, !passwordText.isEmpty else { return}
let values = ["email": emailText, "password": passwordText]
Database.database().reference().child("users").child("\(Auth.auth().currentUser!.uid)").updateChildValues(values) {
(error:Error?, ref:DatabaseReference) in
if let error = error {
print("Data could not be saved: \(error).")
} else {
print("Data saved successfully!")
}
}
self.performSegue(withIdentifier: "signuptoprofile", sender: self)
}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}