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

@alphasoftx/google-auth

v1.1.0

Published

Google Auth is a NodeJS library which helps in authentication with google in backend!

Downloads

5

Readme

Google Auth

Google Auth is a NodeJS library which helps in authentication with google in backend!

This library simply exports 2 functions, getGoogleAuthUrl and getGoogleUser, which helps in authentication with google using nodejs backend!

Install

NPM

Latest version

npm i @alphasoftx/google-auth@latest

Specific version

npm i @alphasoftx/google-auth@<version>

Yarn

Latest version

yarn add @alphasoftx/google-auth@latest

Specific version

yarn add @alphasoftx/google-auth@<version>

Note: Remember to replace <version> with your version name, for example, 1.0.0, 1.1.0, 2.0.1 etc.

Docs

Let's have a look that how this library works, and to do so, we will create a small project!

Step 1

Create a folder named as google-auth-example, this is your project folder!

Step 2

Open command line interface in your project folder and initialise a nodejs project by executing the following command:-

npm init

OR

yarn init

These commands will ask some questions related to your project, answer them and your project will be initialised!

Now add a field in your package.json file:-

{ 
  "type": "module",
} 

This field defines your project as a nodejs module, this is an important field as @alphasoftx/google-auth package doesn't supports require('') syntax!

Step 3

Create a file named as index.js, this will be your main project file!

Note: Project file name should be as same as you written in "main" field in package.json while initialisation of your project!

Step 4

Execute the following command to install all of the dependencies of this project which are express, express-session and @alphasoftx/google-auth:-

npm i express express-session @alphasoftx/google-auth

OR

yarn add express express-session @alphasoftx/google-auth

Express is a backend server module which helps in building server!

Express session is a backend module which helps in authentication with express without any frontend role, you can use jsonwebtoken instead of this module!

Note: you can have more dependencies according to your project, this project has three dependencies as this is a small project!

Step 5

Go to Google developers console and create a project with OAuth client id, for more details, you can watch this youtube video!

Step 6

Now we will start writing code in index.js file:-

// Importing required modules
import express from "express";
import session from "express-session";
import { getGoogleAuthUrl, getGoogleUser } from "@alphasoftx/google-auth";
// Variables
const app = express();
const port = 5000;

// Middlewares
app.use(express.json());
app.use(
  session({
    secret: /* your express-session secret */,
    resave: false,
    saveUninitialized: true,
  })
);
// Endpoints of the server
app.get('/', (req, res)=>{
  if(req.session.user){
    res.send(`
      Name: ${req.session.user.name}<br>
      Email: ${req.session.user.email}<br>
      Image: <img src="${req.session.user.picture}" alt="User image" referrerpolicy="no-referrer"/><br>
      <a href="http://localhost:${port}/logout">Logout</a>
    `);
  }else{
    res.send(`<a href='${getGoogleAuthUrl({
      redirectUrl: `http://localhost:${port}/auth/google`,
      clientId: /* your cliend id */
    })}'>Google</a>`);
  }
});

app.get('/auth/google', async (req, res)=>{
  let user = await getGoogleUser({
    ...req.query,
    clientId: /* your client id */,
    clientSecret: /* your client secret */,
    redirectUrl: `http://localhost:${port}/auth/google`
  });
  if(user.err === true){
    return res.status(404).send("Url not found!");
  }
  req.session.user = user;
  req.session.save();
  res.redirect('/');
});

app.get('/logout', (req, res)=>{
  req.session.destroy(err=>{
    if(err) return console.log(err.message);
    res.redirect('/');
  });
});
// Starting nodejs server
app.listen(port, () => {
  console.log(`Your server is running at http://localhost:${port}`);
});

Step 7

Now run your server with following command:-

node index.js

Summary

getGoogleAuthUrl function needs an object with redirectUrl and clientId and it returns a url of google authentication, and you can also pass an optional field named scopes to define google oauth scopes!

getGoogleUser function is used on redirect url to get and save user, this function needs an object with clientId, clientSecret, redirectUrl and req.query and returns an object of user or error message!

Note: Remember to add referrerpolicy="no-referrer" attribute to img tag as sometimes you may get an error that client don't have permission to access the image!