npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@create-mern-app/auth-service

v1.0.1

Published

`@create-mern-app/auth-service` is a JavaScript package designed to simplify the process of interacting with OAuth2.0 authentication flows. It provides methods for obtaining authorization tokens, refreshing tokens, and retrieving user profiles from an OAu

Downloads

10

Readme

@create-mern-app/auth-service

@create-mern-app/auth-service is a JavaScript package designed to simplify the process of interacting with OAuth2.0 authentication flows. It provides methods for obtaining authorization tokens, refreshing tokens, and retrieving user profiles from an OAuth2.0 server.

Installation

To install @create-mern-app/auth-service, run:

npm install @create-mern-app/auth-service

Usage

1. Import the Package

In your JavaScript file, import the AuthService class:

import AuthService from '@create-mern-app/auth-service';

2. Initialize AuthService

Create an instance of AuthService with your OAuth2.0 client credentials and server base URL:

const authService = new AuthService({
    clientId: 'your_client_id',
    clientSecret: 'your_client_secret',
    redirectUri: 'your_redirect_uri',
    authBaseUrl: 'http://localhost:3000' // Optional: Default is 'http://localhost:3000'
});

3. Generate Authorization URL

Generate the URL to which users should be redirected for authorization:

const authUrl = authService.getAuthUrl();
console.log('Authorization URL:', authUrl);

4. Obtain Access Token

After the user authorizes your application and you receive an authorization code, exchange it for an access token:

const authCode = 'received_auth_code';
authService.getAccessToken(authCode)
    .then(data => {
        console.log('Access Token:', data.accessToken);
        console.log('Refresh Token:', data.refreshToken);
    })
    .catch(error => {
        console.error('Error fetching access token:', error);
    });

5. Refresh Access Token

Use a refresh token to obtain a new access token:

const refreshToken = 'your_refresh_token';
authService.getRefreshToken(refreshToken)
    .then(data => {
        console.log('New Access Token:', data.accessToken);
        console.log('New Refresh Token:', data.refreshToken); // Optional: Check if a new refresh token is issued
    })
    .catch(error => {
        console.error('Error refreshing access token:', error);
    });

6. Retrieve User Profile

Use the access token to fetch the user’s profile information:

const accessToken = 'your_access_token';
authService.getUserProfile(accessToken)
    .then(profile => {
        console.log('User Profile:', profile);
    })
    .catch(error => {
        console.error('Error fetching user profile:', error);
    });

Methods

constructor({ clientId, clientSecret, redirectUri, authBaseUrl })

Creates an instance of AuthService.

  • clientId (string): Your OAuth2.0 client ID.
  • clientSecret (string): Your OAuth2.0 client secret.
  • redirectUri (string): The URI to which users are redirected after authentication.
  • authBaseUrl (string, optional): Base URL of your authentication server. Defaults to 'http://localhost:3000'.

getAuthUrl()

Generates the authorization URL for user authentication.

getAccessToken(authCode)

Exchanges an authorization code for an access token.

  • authCode (string): The authorization code received after user authentication.

Returns an object containing:

  • accessToken (string): The access token.
  • refreshToken (string, optional): The refresh token, if provided.

getRefreshToken(refreshToken)

Exchanges a refresh token for a new access token.

  • refreshToken (string): The refresh token obtained previously.

Returns an object containing:

  • accessToken (string): The new access token.
  • refreshToken (string, optional): The new refresh token, if provided.

getUserProfile(accessToken)

Retrieves the user profile using the access token.

  • accessToken (string): The access token obtained from the OAuth2.0 server.

Error Handling

  • Throws errors if fetching the access token, refreshing the token, or retrieving the user profile fails. Ensure proper error handling in your application.

Example

Here is a complete example showing how to use @create-mern-app/auth-service:

import AuthService from '@create-mern-app/auth-service';

const authService = new AuthService({
    clientId: 'your_client_id',
    clientSecret: 'your_client_secret',
    redirectUri: 'http://localhost:5500/callback'
});

const authUrl = authService.getAuthUrl();
console.log('Authorize URL:', authUrl);

// After redirect and obtaining the auth code
const authCode = 'received_auth_code';

authService.getAccessToken(authCode)
    .then(data => {
        console.log('Access Token:', data.accessToken);
        console.log('Refresh Token:', data.refreshToken);
        
        // Optionally, refresh the access token
        return authService.getRefreshToken(data.refreshToken);
    })
    .then(data => {
        console.log('New Access Token:', data.accessToken);
        console.log('New Refresh Token:', data.refreshToken);
        
        // Retrieve user profile
        return authService.getUserProfile(data.accessToken);
    })
    .then(profile => {
        console.log('User Profile:', profile);
    })
    .catch(error => {
        console.error('Error:', error);
    });

Contributing

Feel free to contribute by forking the repository and submitting a pull request. Please ensure to follow the coding guidelines and include tests for new features.