Skip to content

Custom scopes for different requirements (iOS) #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,54 @@ public class GoogleAuth: CAPPlugin {
var forceAuthCode: Bool = false;
var additionalScopes: [String]!;


public override func load() {
func loadSignInClient (
customClientId: String,
customScopes: [String]
) {
googleSignIn = GIDSignIn.sharedInstance;

let serverClientId = getServerClientIdValue();

guard let clientId = getClientIdValue() else {
NSLog("no client id found in config")
return;
}

googleSignInConfiguration = GIDConfiguration.init(clientID: clientId, serverClientID: serverClientId)
googleSignInConfiguration = GIDConfiguration.init(clientID: customClientId, serverClientID: serverClientId)

// these are scopes granted by default by the signIn method
let defaultGrantedScopes = ["email", "profile", "openid"];

// these are scopes we will need to request after sign in
additionalScopes = (getConfigValue("scopes") as? [String] ?? []).filter {
additionalScopes = customScopes.filter {
return !defaultGrantedScopes.contains($0);
};

if let forceAuthCodeConfig = getConfigValue("forceCodeForRefreshToken") as? Bool {
forceAuthCode = forceAuthCodeConfig;
}

NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(Notification.Name.capacitorOpenURL.rawValue), object: nil);
}


public override func load() {
}

@objc
func initialize(_ call: CAPPluginCall) {
// get client id from initialize, with client id from config file as fallback
guard let clientId = call.getString("clientId") ?? getClientIdValue() as? String else {
NSLog("no client id found in config");
call.resolve();
return;
}

// get scopes from initialize, with scopes from config file as fallback
let customScopes = call.getArray("scopes", String.self) ?? (
getConfigValue("scopes") as? [String] ?? []
);

// get force auth code from initialize, with config from config file as fallback
forceAuthCode = call.getBool("grantOfflineAccess") ?? (
getConfigValue("forceCodeForRefreshToken") as? Bool ?? false
);

// load client
self.loadSignInClient(
customClientId: clientId,
customScopes: customScopes
)
call.resolve();
}

Expand Down