-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthService.swift
73 lines (63 loc) · 2.43 KB
/
AuthService.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
import FirebaseAuth
import GoogleSignIn
import Firebase
import UIKit
class AuthService {
static let shared = AuthService()
/// Sign in using Google
func signInWithGoogle(presentingViewController: UIViewController, completion: @escaping (Bool, Error?) -> Void) {
guard let clientID = FirebaseApp.app()?.options.clientID else {
completion(false, NSError(domain: "GoogleSignIn", code: -1, userInfo: [NSLocalizedDescriptionKey: "Missing client ID"]))
return
}
let config = GIDConfiguration(clientID: clientID)
GIDSignIn.sharedInstance.configuration = config
GIDSignIn.sharedInstance.signIn(withPresenting: presentingViewController) { result, error in
if let error = error {
completion(false, error)
return
}
guard let user = result?.user, let idToken = user.idToken else {
completion(false, NSError(domain: "GoogleSignIn", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to get user ID token"]))
return
}
let credential = GoogleAuthProvider.credential(withIDToken: idToken.tokenString, accessToken: user.accessToken.tokenString)
Auth.auth().signIn(with: credential) { _, error in
if let error = error {
completion(false, error)
} else {
completion(true, nil)
}
}
}
}
/// Sign in using Email/Password
func signInWithEmail(email: String, password: String, completion: @escaping (Bool, Error?) -> Void) {
Auth.auth().signIn(withEmail: email, password: password) { _, error in
if let error = error {
completion(false, error)
} else {
completion(true, nil)
}
}
}
/// Continue as Guest (Anonymous Sign-in)
func signInAnonymously(completion: @escaping (Bool, Error?) -> Void) {
Auth.auth().signInAnonymously { _, error in
if let error = error {
completion(false, error)
} else {
completion(true, nil)
}
}
}
/// Sign out user
func signOut(completion: @escaping (Bool, Error?) -> Void) {
do {
try Auth.auth().signOut()
completion(true, nil)
} catch let error {
completion(false, error)
}
}
}