简体   繁体   中英

How to implement Login with Google in a Nativescript iOS app?

I'm trying to implement google's id provider login in Nativescript through the nativescript-social-login plugin, so far it works well in Android, but it doesn't work at all in iOS.

Following the plugin creator's instructions i have noticed that some classes needed additional definition files for typescript, so researching the issue i came across the notion that the approach to achieve login with google has changed recently in iOS, and some pods have been deprecated. I've tried to follow the new method, i have defined by myself a d.ts file for the GIDSignIn delegate like this

declare class GIDSignIn{
   public static sharedInstance(): GIDSignIn;
   public handleURLSourceApplication(url: NSURL, sourceApplication:NSString, annotiation: id): boolean;
}

But even if this way the application compiles, it crashes as soon as i tap the login button.

By checking the build in xcode i get a warning that states that the Firebase instace has not been configured, how should i go about configuring it in the Typescript code

Use nativescript-oauth plugin for login with social.

Example

login.component.ts:-

`import * as tnsOAuthModule from 'nativescript-oauth';

public login() {

    tnsOAuthModule.login()

        .then(() => {

            let tokenModule = JSON.stringify(tnsOAuthModule);

            let refereshToken = JSON.parse(tokenModule).instance.tokenResult.refreshToken;

            this.getRefereshActiveToken(refereshToken);
        })

        .catch((er) => {

            console.error('error logging in');

            console.dir(er);

        });

}`

Add google authentication configuration in main.ts file Like.

`import * as tnsOAuthModule from 'nativescript-oauth';

var myInitOptions: tnsOAuthModule.ITnsOAuthCredentials = {

authority: '',

authorizeEndpoint: '',

tokenEndpoint: '',

clientId: '',

redirectUri: 'urn:ietf:wg:oauth:2.0:oob',

responseType: "code",

scope: 'openid',

};

tnsOAuthModule.initCustom({

credentials: myInitOptions,

cookieDomains: ['http://demoweb.net'],

});`

For IOS only add this code for redirection after successfully authenticate on google in App_Resources -> iOS -> info.plist file.

<key>CFBundleURLTypes</key>

<array>

    <dict>

        <key>CFBundleTypeRole</key>

        <string>Editor</string>

        <key>CFBundleURLName</key>

        <string>app_id</string>

        <key>CFBundleURLSchemes</key>

        <array>

            <string>appauth</string>

        </array>

    </dict>

</array>

<key>NSAppTransportSecurity</key>

<dict>

    <key>NSAllowsArbitraryLoads</key>

    <true/>

</dict>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM