This repository was archived by the owner on Aug 6, 2024. It is now read-only.
forked from CodetrixStudio/CapacitorGoogleAuth
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPlugin.swift
More file actions
126 lines (117 loc) · 4.39 KB
/
Plugin.swift
File metadata and controls
126 lines (117 loc) · 4.39 KB
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
116
117
118
119
120
121
122
123
124
125
126
import Foundation
import Capacitor
import GoogleSignIn
/**
* Please read the Capacitor iOS Plugin Development Guide
* here: https://capacitor.ionicframework.com/docs/plugins/ios
*/
@objc(GoogleAuth)
public class GoogleAuth: CAPPlugin {
var signInCall: CAPPluginCall?
let googleSignIn: GIDSignIn = GIDSignIn.sharedInstance();
var forceAuthCode: Bool = false;
public override func load() {
guard let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") else {
print("GoogleService-Info.plist not found");
return;
}
guard let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] else {return}
guard let clientId = dict["CLIENT_ID"] as? String else {return}
googleSignIn.clientID = clientId;
googleSignIn.delegate = self;
googleSignIn.presentingViewController = bridge?.viewController;
if let serverClientId = getConfigValue("serverClientId") as? String {
googleSignIn.serverClientID = serverClientId;
}
if let scopes = getConfigValue("scopes") as? [String] {
googleSignIn.scopes = scopes;
}
if let forceAuthCodeConfig = getConfigValue("forceCodeForRefreshToken") as? Bool {
forceAuthCode = forceAuthCodeConfig;
}
NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(CAPNotifications.URLOpen.name()), object: nil);
}
@objc
func init(_ call: CAPPluginCall) {
call.unimplemented("Not available on iOS")
}
@objc
func signIn(_ call: CAPPluginCall) {
signInCall = call;
DispatchQueue.main.async {
if self.googleSignIn.hasPreviousSignIn() && !self.forceAuthCode {
self.googleSignIn.restorePreviousSignIn();
} else {
self.googleSignIn.signIn();
}
}
}
@objc
func refresh(_ call: CAPPluginCall) {
DispatchQueue.main.async {
if self.googleSignIn.currentUser == nil {
call.reject("User not logged in.");
return
}
self.googleSignIn.currentUser.authentication.getTokensWithHandler { (authentication, error) in
guard let authentication = authentication else {
call.reject(error?.localizedDescription ?? "Something went wrong.");
return;
}
let authenticationData: [String: Any] = [
"accessToken": authentication.accessToken,
"idToken": authentication.idToken,
"refreshToken": authentication.refreshToken
]
call.resolve(authenticationData);
}
}
}
@objc
func signOut(_ call: CAPPluginCall) {
DispatchQueue.main.async {
self.googleSignIn.signOut();
}
call.resolve();
}
@objc
func handleOpenUrl(_ notification: Notification) {
guard let object = notification.object as? [String: Any] else {
print("There is no object on handleOpenUrl");
return;
}
guard let url = object["url"] as? URL else {
print("There is no url on handleOpenUrl");
return;
}
googleSignIn.handle(url);
}
func processCallback(user: GIDGoogleUser) {
var userData: [String: Any] = [
"authentication": [
"accessToken": user.authentication.accessToken,
"idToken": user.authentication.idToken,
"refreshToken": user.authentication.refreshToken
],
"serverAuthCode": user.serverAuthCode,
"email": user.profile.email,
"familyName": user.profile.familyName,
"givenName": user.profile.givenName,
"id": user.userID,
"name": user.profile.name
];
if let imageUrl = user.profile.imageURL(withDimension: 100)?.absoluteString {
userData["imageUrl"] = imageUrl;
}
signInCall?.resolve(userData);
}
}
extension GoogleAuth: GIDSignInDelegate {
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
signInCall?.reject(error.localizedDescription);
return;
}
processCallback(user: user);
}
}