@katomaran/omni-auth
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@katomaran/omni-auth

A simple and easy-to-use package for handling Google and Apple login authentication in your NestJS applications.

Installation

To install the package, run the following command:

 npm install @katomaran/omni-auth

Dependencies This package requires the following dependencies:

@nestjs/common: For NestJS integration. apple-signin-verify-token: For verifying Apple login tokens. axios: For making HTTP requests to Google API. Usage Importing the Service First, import the SocialLoginService into your NestJS service or controller:

import { SocialLoginService } from '@katomaran/omni-auth';
Using the Google Login Method
To use Google login, call the loginWithGoogle method with the Google ID token:

const socialLoginService = new SocialLoginService();

async function loginWithGoogle() {
  const token = 'your-google-id-token';
  try {
    const user = await socialLoginService.loginWithGoogle(token);
    console.log('User:', user);
  } catch (error) {
    console.error('Login failed:', error);
  }
}

The method returns the Google user data if the login is successful. If the login fails, it throws an UnauthorizedException with the message "Google login failed".

Using the Apple Login Method To use Apple login, call the loginWithApple method with the Apple token:

const socialLoginService = new SocialLoginService();

async function loginWithApple() {
  const token = 'your-apple-token';
  try {
    const user = await socialLoginService.loginWithApple(token);
    console.log('User:', user);
  } catch (error) {
    console.error('Login failed:', error);
  }
}

The method returns an object containing the user's email and first name if the login is successful. If the login fails, it throws an UnauthorizedException with the message "Apple login failed".

Error Handling Both loginWithGoogle and loginWithApple methods throw an UnauthorizedException if login fails. The message will be:

"Google login failed" for Google login failures. "Apple login failed" for Apple login failures. Example of Usage in NestJS Controller

import { Controller, Post, Body } from '@nestjs/common';
import { SocialLoginService } from '@katomaran/omni-auth';

@Controller('auth')
export class AuthController {
  constructor(private readonly socialLoginService: SocialLoginService) {}

  @Post('google')
  async loginWithGoogle(@Body('token') token: string) {
    return await this.socialLoginService.loginWithGoogle(token);
  }

  @Post('apple')
  async loginWithApple(@Body('token') token: string) {
    return await this.socialLoginService.loginWithApple(token);
  }
}

In this example, you can create endpoints for Google and Apple login by calling the loginWithGoogle and loginWithApple methods from your SocialLoginService.

License This package is licensed under the ISC License.

This full code provides installation instructions, usage examples, error handling, and a NestJS controller example in markdown format. Let me know if you need any further modifications!

Readme

Keywords

none

Package Sidebar

Install

npm i @katomaran/omni-auth

Weekly Downloads

6

Version

1.0.1

License

ISC

Unpacked Size

9.65 kB

Total Files

8

Last publish

Collaborators

  • kousallya
  • katomaran-pvt