Saltare al contenuto

Inizia

  1. Installa il pacchetto

    Finestra del terminale
    bun add @capgo/capacitor-social-login
  2. Sincronizza con progetti nativi

    Finestra del terminale
    bunx cap sync
  3. Inizializza all'avvio dell'applicazione

    import { SocialLogin } from '@capgo/capacitor-social-login';
    await SocialLogin.initialize({
    google: {
    webClientId: 'your-google-web-client-id',
    iOSServerClientId: 'your-google-server-client-id',
    mode: 'online',
    },
    apple: {
    clientId: 'your-apple-service-id',
    useProperTokenExchange: true,
    useBroadcastChannel: true,
    },
    facebook: {
    appId: 'your-facebook-app-id',
    },
    twitter: {
    clientId: 'your-twitter-client-id',
    redirectUrl: 'myapp://oauth/twitter',
    },
    oauth2: {
    github: {
    appId: 'your-github-client-id',
    authorizationBaseUrl: 'https://github.com/login/oauth/authorize',
    accessTokenEndpoint: 'https://github.com/login/oauth/access_token',
    redirectUrl: 'myapp://oauth/github',
    scope: 'read:user user:email',
    pkceEnabled: true,
    },
    },
    });
await SocialLogin.login({
provider: 'google',
options: { scopes: ['profile', 'email'] },
});
await SocialLogin.login({
provider: 'oauth2',
options: {
providerId: 'github',
scope: 'read:user user:email',
},
});
const status = await SocialLogin.isLoggedIn({ provider: 'google' });
await SocialLogin.logout({ provider: 'google' });
// For providers that support this mode
const authCodeResult = await SocialLogin.getAuthorizationCode({ provider: 'google' });
await SocialLogin.refresh({ provider: 'google', options: {} as never });
const jwt = await SocialLogin.decodeIdToken({
idToken: 'eyJhbGciOi...',
});
const { date } = await SocialLogin.getAccessTokenExpirationDate({
accessTokenExpirationDate: Date.now() + 3600 * 1000,
});
const expired = await SocialLogin.isAccessTokenExpired({
accessTokenExpirationDate: Date.now() + 1000,
});
const active = await SocialLogin.isRefreshTokenAvailable({ refreshToken: 'a-token' });

google.mode: 'offline' ritorna serverAuthCode dall'accesso. In questo modo non sono disponibili logout, isLoggedIn, getAuthorizationCode, e refresh.

Usa serverAuthCode solo come input per lo scambio di token nel tuo backend. Se hai bisogno di chiamare SocialLogin.refresh() invece. google.mode: 'online' Sezione intitolata “Apple”

per la configurazione semplificata di Android. useProperTokenExchange: true OAuth2 flow di reindirizzamento web useBroadcastChannel: true Use

Usa OAuth2LoginOptions.flow: 'redirect' per flussi web che si spostano via dalla pagina.

Puoi disabilitare i provider non utilizzati per ridurre i binari nativi:

import type { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
appId: 'com.example.app',
appName: 'My App',
webDir: 'dist',
plugins: {
SocialLogin: {
providers: {
google: true,
facebook: true,
apple: true,
twitter: false,
},
},
},
};