Skip to main content

SocialAuthService

Package: @nauth-toolkit/core Type: Service

Service for managing social authentication accounts and their relationships. Provides account linking/unlinking, password management for social-only users, and account queries.

import { SocialAuthService } from '@nauth-toolkit/nestjs';

Overview

Service for managing social authentication accounts and their relationships. This service provides account linking/unlinking, password management for social-only users, and querying linked accounts.

OAuth Login Flows

For OAuth authentication (login/signup), use SocialRedirectHandler or the frontend SDK's loginWithSocial() method. See the Social Login Guide for details.

Optional Feature

Only available when social auth provider modules are imported (e.g., GoogleSocialAuthModule, AppleSocialAuthModule).

note

Auto-injected by framework. No manual instantiation required.

Methods

canSetPassword()

Check if user can set a password (social-only users can set passwords).

async canSetPassword(dto: CanSetPasswordDTO): Promise<CanSetPasswordResponseDTO>

Parameters

Returns

Example

import { CanSetPasswordDTO } from '@nauth-toolkit/nestjs';

const dto: CanSetPasswordDTO = { userId: user.sub };
const result = await this.socialAuthService.canSetPassword(dto);
if (result.canSetPassword) {
// Show password setup form
}

getLinkedAccounts()

Get linked social accounts for a user.

async getLinkedAccounts(dto: GetLinkedAccountsDTO): Promise<GetLinkedAccountsResponseDTO>

Parameters

Returns

Errors

CodeWhenDetails
NOT_FOUNDUser not found{ userId?: string }

Example

import { GetLinkedAccountsDTO } from '@nauth-toolkit/nestjs';

@Get('social/linked')
async getLinked(@CurrentUser() user: IUser) {
const dto: GetLinkedAccountsDTO = { userId: user.sub };
const accounts = await this.socialAuthService.getLinkedAccounts(dto);
return accounts.accounts;
}

linkSocialAccount()

Link social account to existing authenticated user.

async linkSocialAccount(dto: LinkSocialAccountDTO): Promise<LinkSocialAccountResponseDTO>

Parameters

Returns

Errors

CodeWhenDetails
SOCIAL_ACCOUNT_LINKEDAccount already linked{}
NOT_FOUNDUser not found{ userId?: string }
SOCIAL_CONFIG_MISSINGProvider not configured{}
SOCIAL_TOKEN_INVALIDOAuth callback failed{}

Example

import { LinkSocialAccountDTO } from '@nauth-toolkit/nestjs';

@Post('social/link')
async link(@CurrentUser() user: IUser, @Body() body: { provider: string; code: string; state: string }) {
const dto: LinkSocialAccountDTO = {
userId: user.sub,
...body,
};
return await this.socialAuthService.linkSocialAccount(dto);
}

listAvailableProviders()

List available social auth providers.

listAvailableProviders(): string[]

Parameters

None

Returns

  • string[] - Array of provider names (e.g., ['google', 'apple', 'facebook'])

Example

const providers = this.socialAuthService.listAvailableProviders();
// ['google', 'apple']

setPasswordForSocialUser()

Set password for social-only user.

async setPasswordForSocialUser(dto: SetPasswordForSocialUserDTO): Promise<SetPasswordForSocialUserResponseDTO>

Parameters

Returns

Errors

CodeWhenDetails
NOT_FOUNDUser not found{ userId?: string }
VALIDATION_FAILEDUser already has password{ field: 'password' }
WEAK_PASSWORDPassword policy violation{ errors: string[] }

Example

import { SetPasswordForSocialUserDTO } from '@nauth-toolkit/nestjs';

const dto: SetPasswordForSocialUserDTO = {
userId: user.sub,
password: 'newpassword',
};
await this.socialAuthService.setPasswordForSocialUser(dto);

unlinkSocialAccount()

Unlink social account from user.

async unlinkSocialAccount(dto: UnlinkSocialAccountDTO): Promise<UnlinkSocialAccountResponseDTO>

Parameters

Returns

Errors

CodeWhenDetails
NOT_FOUNDUser not found{ userId?: string }
SOCIAL_ACCOUNT_NOT_FOUNDAccount not linked{}

Example

import { UnlinkSocialAccountDTO } from '@nauth-toolkit/nestjs';

@Post('social/unlink')
async unlink(@CurrentUser() user: IUser, @Body() body: { provider: string }) {
const dto: UnlinkSocialAccountDTO = {
userId: user.sub,
provider: body.provider,
};
await this.socialAuthService.unlinkSocialAccount(dto);
return { success: true };
}

Error Handling

All methods throw NAuthException on failure. Handle errors appropriately for your framework.

try {
await this.socialAuthService.linkSocialAccount(dto);
} catch (error) {
if (error instanceof NAuthException) {
console.log(error.code);
}
}

See Error Handling Guide.


Redirect-first OAuth (SocialRedirectHandler)

For backend-first OAuth flows (redirect to provider, then callback), use SocialRedirectHandler (injected alongside this service). The handler is framework-neutral: it reads delivery and deviceToken from ContextStorage and applies cookies via HTTP_RESPONSE in cookies mode. Consumer controllers pass only provider and DTOs.

  • start(provider, dto) - Returns Promise<StartSocialRedirectResponseDTO>. Use with NestJS @Redirect().
  • callback(provider, dto) - Returns Promise<SocialRedirectCallbackResponseDTO>. Cookies are applied to the response from ContextStorage when delivery is cookies.
  • exchange(exchangeToken) - Returns Promise<AuthResponseDTO> for JSON/hybrid or challenge flows.

See the Social Login Guide for full implementation and DTOs (StartSocialRedirectQueryDTO, SocialCallbackQueryDTO, SocialCallbackFormDTO, StartSocialRedirectResponseDTO, SocialRedirectCallbackResponseDTO).