@abacritt/angularx-social-login

中文文档
2.6.0发布于 2026/03/28MITTypeScriptES Module近 30 天下载 36.2万

Social login and authentication module for Angular 19. Supports authentication with Google, Facebook, Amazon, and VK. Can be extended to other providers also.

原始使用文档
文档目录18 节
  1. Compatibility Matrix
  2. Getting started
  3. Install via npm
  4. Import the module
  5. Sign in with Facebook
  6. Sign in with VK
  7. Sign in with Google
  8. Refresh google Auth Token
  9. Request google Access Token
  10. Refresh google Access Token
  11. Subscribe to the authentication state
  12. Display the user information
  13. Custom Provider
  14. Specifying custom scopes, fields etc. on initialization
  15. Specifying custom scopes, fields etc. on login
  16. Providers
  17. Running the demo app
  18. Error & Loading Handling

Angular Social Login

Use Discussions for questions.

Social login and authentication module for Angular 21. Supports authentication with Google, Facebook, Amazon, Microsoft, and VK out of the box. Can be extended to other providers also.

Check out the demo.

Compatibility Matrix

Library VersionAngular Version
@abacritt/angularx-social-login:2.6.X21
@abacritt/angularx-social-login:2.5.X20
@abacritt/angularx-social-login:2.4.X19
@abacritt/angularx-social-login:2.3.X18
@abacritt/angularx-social-login:2.2.X17
@abacritt/angularx-social-login:2.1.X16, 17
@abacritt/angularx-social-login:2.0.X15, 16
@abacritt/angularx-social-login:113, 14, 15
angularx-social-login:412, 13
angularx-social-login:39, 10, 11
angularx-social-login:25, 6, 7, 8

Getting started

Install via npm

npm i @abacritt/angularx-social-login

Import the module

In your AppModule, import the SocialLoginModule

import { SocialLoginModule, SocialAuthServiceConfig } from '@abacritt/angularx-social-login';
import {
  GoogleLoginProvider,
  FacebookLoginProvider
} from '@abacritt/angularx-social-login';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    SocialLoginModule
  ],
  providers: [
    {
      provide: 'SocialAuthServiceConfig',
      useValue: {
        autoLogin: false,
        lang: 'en',
        providers: [
          {
            id: GoogleLoginProvider.PROVIDER_ID,
            provider: new GoogleLoginProvider(
              'clientId'
            )
          },
          {
            id: FacebookLoginProvider.PROVIDER_ID,
            provider: new FacebookLoginProvider('clientId')
          }
        ],
        onError: (err) => {
          console.error(err);
        }
      } as SocialAuthServiceConfig,
    }
  ],
  bootstrap: [...]
})
export class AppModule { }

Sign in with Facebook


import { SocialAuthService } from "@abacritt/angularx-social-login";
import { FacebookLoginProvider } from "@abacritt/angularx-social-login";

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styleUrls: ['./demo.component.css']
})
export class DemoComponent {

  constructor(private authService: SocialAuthService) { }

  signInWithFB(): void {
    this.authService.signIn(FacebookLoginProvider.PROVIDER_ID);
  }

  signOut(): void {
    this.authService.signOut();
  }

}

Sign in with VK


import { SocialAuthService } from "@abacritt/angularx-social-login";
import { VKLoginProvider } from "@abacritt/angularx-social-login";

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styleUrls: ['./demo.component.css']
})
export class DemoComponent {

  constructor(private authService: SocialAuthService) { }

  signInWithVK(): void {
    // default usage without defining access level
    this.authService.signIn(VKLoginProvider.PROVIDER_ID);

    // define access level
    // https://dev.vk.com/reference/access-rights
    this.authService.signIn(VKLoginProvider.PROVIDER_ID, ['offline', 'email']);
  }

  signOut(): void {
    this.authService.signOut();
  }

}

Sign in with Google

GoogleLoginProvider has no signIn() method as other providers, the login flow is triggered by a button that the gis client is generating. Calling SocialAuthService.signIn(GoogleLoginProvider.PROVIDER_ID) will have no effect.

Instead, make sure you subscribed to the authentication state and add the following GoogleSigninButtonDirective to your component template to wrap the 'Sign In With Google' button:

ALSO

Check out the Google button generator. Link to Button attributes

<asl-google-signin-button type='icon' size='medium'></asl-google-signin-button>

Options:

NameTypeValueDefault
typestring'icon' or 'standard''icon'
sizestring'small', 'medium' or 'large''medium'
shapestring'square','circle','pill' or 'rectangular''rectangular'
textstring'signin_with','signup_with'or 'continue_with''signin_with'
widthstring'200 - 400 '
themestring'outline','filled_blue' or 'filled_black''outline'
logo_alignmentstring'left' or 'center''left'

This will only work if the GoogleLoginProvider is registered in SocialAuthServiceConfig.

Refresh google Auth Token

Once a user is logged in manual refresh token method can be triggered


import { SocialAuthService } from "@abacritt/angularx-social-login";
import { GoogleLoginProvider } from "@abacritt/angularx-social-login";

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styleUrls: ['./demo.component.css']
})
export class DemoComponent implements OnInit {

  constructor(private authService: SocialAuthService) { }

  refreshToken(): void {
    this.authService.refreshAuthToken(GoogleLoginProvider.PROVIDER_ID);
  }

}

Request google Access Token

Once a user is logged in an access token can be requested for the scopes we specified in GoogleInitOptions.scopes, you can then reuse this access token to make calls to google apis


import { HttpClient } from '@angular/common/http';
import { SocialAuthService } from "@abacritt/angularx-social-login";
import { GoogleLoginProvider } from "@abacritt/angularx-social-login";

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styleUrls: ['./demo.component.css']
})
export class DemoComponent implements OnInit {

  private accessToken = '';

  constructor(private authService: SocialAuthService, private httpClient: HttpClient) { }

  getAccessToken(): void {
    this.authService.getAccessToken(GoogleLoginProvider.PROVIDER_ID).then(accessToken => this.accessToken = accessToken);
  }

  getGoogleCalendarData(): void {
    if (!this.accessToken) return;

    this.httpClient
      .get('https://www.googleapis.com/calendar/v3/calendars/primary/events', {
        headers: { Authorization: `Bearer ${this.accessToken}` },
      })
      .subscribe((events) => {
        alert('Look at your console');
        console.log('events', events);
      });
  }
}

Refresh google Access Token

Once a user is logged in and an access token was obtained, the access token can be refreshed (revoked)


import { SocialAuthService } from "@abacritt/angularx-social-login";
import { GoogleLoginProvider } from "@abacritt/angularx-social-login";

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styleUrls: ['./demo.component.css']
})
export class DemoComponent implements OnInit {

  constructor(private authService: SocialAuthService) { }

  refreshToken(): void {
    this.authService.refreshAccessToken(GoogleLoginProvider.PROVIDER_ID);
  }

}

Subscribe to the authentication state

You are notified when user logs in or logs out. You receive a SocialUser object when the user logs in and a null when the user logs out. SocialUser object contains basic user information such as name, email, photo URL, etc. along with the auth_token. You can communicate the auth_token to your server to authenticate the user in server and make API calls from server.

import { SocialAuthService } from "@abacritt/angularx-social-login";
import { SocialUser } from "@abacritt/angularx-social-login";

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styleUrls: ['./demo.component.css']
})
export class DemoComponent implements OnInit {

  user: SocialUser;
  loggedIn: boolean;

  constructor(private authService: SocialAuthService) { }

  ngOnInit() {
    this.authService.authState.subscribe((user) => {
      this.user = user;
      this.loggedIn = (user != null);
    });
  }

}

Display the user information

<img src="{{ user.photoUrl }}" referrerpolicy="no-referrer">
<div>
  <h4>{{ user.name }}</h4>
  <p>{{ user.email }}</p>
</div>

Custom Provider

We can use a custom provider, implementing our own logic and needs like the following example:

@Injectable({ providedIn: 'root' })
export class MyCustomLoginProvider extends BaseLoginProvider {
  public static readonly PROVIDER_ID = 'CUSTOM' as const;

  constructor(/* infinite list of dependencies*/) {}
}
@NgModule({
  declarations: [AppComponent, NavbarComponent, DemoComponent],
  imports: [BrowserModule, FormsModule, SocialLoginModule],
  providers: [
    {
      provide: 'SocialAuthServiceConfig',
      useValue: {
        autoLogin: true,
        lang: 'en',
        providers: [
          {
            id: MyCustomLoginProvider.PROVIDER_ID,
            provider: MyCustomLoginProvider,
          },
        ],
      } as SocialAuthServiceConfig,
    },
  ],

Specifying custom scopes, fields etc. on initialization

const fbLoginOptions = {
  scope: 'pages_messaging,pages_messaging_subscriptions,email,pages_show_list,manage_pages',
  return_scopes: true,
  enable_profile_selector: true
}; // https://developers.facebook.com/docs/reference/javascript/FB.login/v2.11

const googleLoginOptions: GoogleInitOptions = {
  oneTapEnabled: false, // default is true
  scopes: 'https://www.googleapis.com/auth/calendar.readonly'
}; // https://developers.google.com/identity/oauth2/web/guides/use-token-model#initialize_a_token_client

const vkLoginOptions = {
  fields: 'photo_max,contacts', // Profile fields to return, see: https://vk.com/dev/objects/user
  version: '5.131', // https://vk.com/dev/versions
}; // https://vk.com/dev/users.get

let config = [
  {
    id: GoogleLoginProvider.PROVIDER_ID,
    provider: new GoogleLoginProvider("Google-OAuth-Client-Id", googleLoginOptions)
  },
  {
    id: FacebookLoginProvider.PROVIDER_ID,
    provider: new FacebookLoginProvider("Facebook-App-Id", fbLoginOptions)
  },
  {
    id: VKLoginProvider.PROVIDER_ID,
    provider: new VKLoginProvider("VK-App-Id", vkLoginOptions)
  },
];

Specifying custom scopes, fields etc. on login

const fbLoginOptions = {
  scope: 'pages_messaging,pages_messaging_subscriptions,email,pages_show_list,manage_pages'
}; // https://developers.facebook.com/docs/reference/javascript/FB.login/v2.11

this.authService.signIn(FacebookLoginProvider.PROVIDER_ID, fbLoginOptions);

Providers

ProviderDocumentation
MicrosoftLoginProviderLink

Running the demo app

ng build angularx-social-login
ng serve

Error & Loading Handling

To handle errors and loaded events you can use the BaseLoginProvider object from your SocialAuthServiceConfig

// inject the SOCIAL_AUTH_CONFIG into your component
providerConfig: SocialAuthServiceConfig | Promise<SocialAuthServiceConfig> = inject(SOCIAL_AUTH_CONFIG);

...

async SetUpProviderHandling() {
  // get the injected providerConfig as a SocialAuthServiceConfig.
  const providerConfig = await this.providerConfig;
  // get your provider config based on the ProviderId
  const foundProviderConfig = providerConfig.providers.find(x => x.id == MyProvider.PROVIDER_ID);
  if(foundProviderConfig && foundProviderConfig.provider) {
    foundProviderConfig.provider.onHasErrorChange.subscribe(value => {
      // handle errors on change here
    });
    // set the base has error result here.
    const hasError = foundProviderConfig.provider.hasError;

    foundProviderConfig.provider.onIsLoadedChange.subscribe(value => {
      // handle isLoading on change here
    });
    // set the base isLoaded result here.
    const hasError = foundProviderConfig.provider.isLoaded;
  }
}

版本与发布记录

@abacritt/angularx-social-login 共发布 23 个版本,首次发布于 2022/04/15;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.6.0latest2026/03/287 个月前
2.5.12025/09/191.1 年前
2.5.02025/07/251.3 年前
2.4.02025/04/241.5 年前
2.3.02024/06/082 年前
2.2.02023/12/122 年前

关于 @abacritt/angularx-social-login 的常见问题

@abacritt/angularx-social-login 是什么?

Social login and authentication module for Angular 19. Supports authentication with Google, Facebook, Amazon, and VK. Can be extended to other providers also.

如何安装 @abacritt/angularx-social-login?

在项目目录运行 npm install @abacritt/angularx-social-login,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(>=21.2.6)、@angular/core(>=21.2.6)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@abacritt/angularx-social-login 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@abacritt/angularx-social-login 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@abacritt/angularx-social-login 可以商用吗?

@abacritt/angularx-social-login 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@abacritt/angularx-social-login 的下载量有多少?

根据 npm 官方下载统计,@abacritt/angularx-social-login 在 2026/10/05 – 2026/11/03 这 30 天内被下载 362,236 次,日均约 12,075 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@abacritt/angularx-social-login 还在维护吗?

最新版本 2.6.0 发布于 2026/03/28(7 个月前);至今共发布 23 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/04/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。