forked from joonhocho/react-native-google-sign-in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNGoogleSignIn.swift
115 lines (95 loc) · 3.21 KB
/
RNGoogleSignIn.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// RNGoogleSignIn.swift
//
// Created by Joon Ho Cho on 1/16/17.
// Copyright © 2017 Facebook. All rights reserved.
//
import Foundation
@objc(RNGoogleSignIn)
class RNGoogleSignIn: NSObject, GIDSignInUIDelegate {
static let sharedInstance = RNGoogleSignIn()
weak var events: RNGoogleSignInEvents?
override init() {
super.init()
GIDSignIn.sharedInstance().uiDelegate = self
}
// @objc func addEvent(_ name: String, location: String, date: NSNumber, callback: @escaping (Array<String>) -> ()) -> Void {
// NSLog("%@ %@ %@", name, location, date)
// self.callback = callback
// }
@objc func configure(_ config: [String: Any]) {
if let instance = GIDSignIn.sharedInstance() {
if let clientID = config["clientID"] as? String {
instance.clientID = clientID
}
if let scopes = config["scopes"] as? [String] {
instance.scopes = scopes
}
if let shouldFetchBasicProfile = config["shouldFetchBasicProfile"] as? Bool {
instance.shouldFetchBasicProfile = shouldFetchBasicProfile
}
if let language = config["language"] as? String {
instance.language = language
}
if let loginHint = config["loginHint"] as? String {
instance.loginHint = loginHint
}
if let serverClientID = config["serverClientID"] as? String {
instance.serverClientID = serverClientID
}
if let openIDRealm = config["openIDRealm"] as? String {
instance.openIDRealm = openIDRealm
}
if let hostedDomain = config["hostedDomain"] as? String {
instance.hostedDomain = hostedDomain
}
}
}
@objc func signIn() {
DispatchQueue.main.async {
GIDSignIn.sharedInstance().signIn()
}
}
@objc func signOut() {
// edit by YousefED: signOut is not an async operation and doesn't raise any events
//DispatchQueue.main.async {
GIDSignIn.sharedInstance().signOut()
//}
}
@objc func signInSilently() {
DispatchQueue.main.async {
GIDSignIn.sharedInstance().signInSilently()
}
}
@objc func disconnect() {
DispatchQueue.main.async {
GIDSignIn.sharedInstance().disconnect()
}
}
@objc func currentUser(_ resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
resolve(RNGoogleSignInEvents.userToJSON(GIDSignIn.sharedInstance().currentUser))
}
@objc func hasAuthInKeychain(_ resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
resolve(GIDSignIn.sharedInstance().hasAuthInKeychain())
}
@objc func constantsToExport() -> [String: Any] {
return [
"dark": "dark",
"light": "light",
"iconOnly": "iconOnly",
"standard": "standard",
"wide": "wide",
]
}
// START: GIDSignInUIDelegate
func sign(inWillDispatch signIn: GIDSignIn!, error: Error?) {
events?.dispatch(error: error)
}
func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
viewController.dismiss(animated: true, completion: nil)
}
func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
UIApplication.shared.keyWindow?.rootViewController?.present(viewController, animated: true, completion: nil)
}
// END: GIDSignInUIDelegate
}