{"_id":"@alphasoftx/google-auth","_rev":"6-0e11e3fcf4af3c6d7028eecdc42b9c46","time":{"created":"2022-06-04T11:10:24.512Z","0.0.0":"2022-06-04T09:26:11.415Z","modified":"2022-06-18T07:47:12.714Z","0.0.1":"2022-06-04T11:10:24.745Z","1.0.0":"2022-06-10T04:38:26.428Z","1.1.0":"2022-06-18T07:47:12.646Z"},"name":"@alphasoftx/google-auth","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.0":{"name":"@alphasoftx/google-auth","version":"1.0.0","description":"Google Auth is a NodeJS library which helps in authentication with google in backend!","main":"index.js","type":"module","keywords":["google-auth","alphasoftx","backend","nodejs","library"],"author":{"name":"AlphaSoftX"},"license":"ISC","dependencies":{"axios":"^0.27.2"},"_id":"@alphasoftx/google-auth@1.0.0","_nodeVersion":"16.14.2","_npmVersion":"8.5.5","dist":{"integrity":"sha512-JUhFcbl04h01NJ1UFY4fFQAXAbsPecgp5hZfEWbrHatoYQj4g2EvjKQkvmrzs0npceQhTK+2n7wey24ynL8iqg==","shasum":"7c56cf6db78d9fe32a47c2f071cfd51c3cb1d87b","tarball":"https://registry.npmjs.org/@alphasoftx/google-auth/-/google-auth-1.0.0.tgz","fileCount":3,"unpackedSize":6937,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCGtORynAuQkQTorO/CynJYBi0DiOOxjZwy6VmY2RuR7gIhAKaG3i0vaKnPHdwqLLOAXN39RZuu+a5vuIQ1boVP7zJG"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiosrCACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrVtw/8CU9EcfeQZG4y0XDDCwBQ4BciE8C3eljTxBEqd/OgRsnIUVOg\r\nlfSeXt6GonXC/X1MPY2lwDsqp5KjC8FrD4NYfmQ+J8S17uIkAG18RxC9tUZN\r\nzO8HJQuF5cjBWo/l2xEJy9Z/4FKa55t8x/XEqD7FQ5KAFCd8zavkBryZ96x+\r\nxXKjUdHVEdtstkQa+7heGP0SYGGBSHQTnMqsWmsau32AMMHEwLDQfcCQfjcw\r\nc7dN+7tAnLE9SGoreZMgGo6XSBBeA+5hSjTMLa0aXPphwEAVuqh+6gB6X3F1\r\n0HFPIuPJa0Gwq9cmd6+j02V5yE/lViVPS+NSFDqL8oZS1/x9HcrgPhXR88Ld\r\nOYIBCevRAx0Wz/xE7w9SXTO/TeYYiczAXyF/p4au1eod3TrZ0IPf3mVsCbcD\r\nzyAiz+JoC3xWoz3WHveVruKYwxmMtN96Y5TTtCcd9l3fA2U9V6SnupnCI1gh\r\npfU5L8jlqBXHoek0EaoRf/H6EWwm8jT2y1+0H8vDCOyhIqP4SSgCLNbfbqzG\r\nMQss18BPhnzxhTTnYad/yJze71hzTL2YCZF7N5Ch4DBWx60dIboANv80Jbkb\r\nu9W2r/cmqXnVVgywvfoetpygOHnGqor13Czpvf8wE62ohWwZ+pi5RF0JMaDy\r\n6r50Na/EzCZKdB3Byr13lR8N/vxr9OUBrcE=\r\n=sKOG\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"alphasoftx","email":"alphasoftx@gmail.com"},"directories":{},"maintainers":[{"name":"alphasoftx","email":"alphasoftx@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/google-auth_1.0.0_1654835906264_0.013319716563367834"},"_hasShrinkwrap":false},"1.1.0":{"name":"@alphasoftx/google-auth","version":"1.1.0","description":"Google Auth is a NodeJS library which helps in authentication with google in backend!","main":"index.js","type":"module","keywords":["google-auth","alphasoftx","backend","nodejs","library"],"author":{"name":"AlphaSoftX"},"license":"ISC","dependencies":{"axios":"^0.27.2"},"_id":"@alphasoftx/google-auth@1.1.0","_nodeVersion":"16.14.2","_npmVersion":"8.5.5","dist":{"integrity":"sha512-ZBj82PD0UrBoV1s+oe/+uEBNcsWBFz8nHWGXuVppBkXlRPJE2DYrSH/8YUAf07h2RlgCQkN2mYwrzB5XApL4gw==","shasum":"5be9e065a8b23fd22aebdce34a3712a7afd2e6d6","tarball":"https://registry.npmjs.org/@alphasoftx/google-auth/-/google-auth-1.1.0.tgz","fileCount":3,"unpackedSize":7128,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDXNgpHpYgIMq64opOYEx3S6s76QBSxxjZ0jheu5J/xJwIhAPrv1Z0iM0N8kyf2dKY08kWaXdkEcVxqguU37H7VBWZE"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJirYMAACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrHaQ//W7GM/U3ET7c8A5wD/yzZdhYpTzVLKRUhBp1LBCzhbE6+eAGX\r\n3SxyYhZreLy5fLpK6vwH1ibrkKURSZUd6n5r+1Yt6QPKpNOwioECg19I7oqT\r\n2wefA3GFT2N6F27HLwtL4b3u/wq5INRPP+B9Xaw0mRstkPEqXwqSobSpgxn2\r\nDTXv/Y74rP80kJUCCe9ulq+Io8KKjAEnTxB/g/A34c6VZ4bcaNJqnVNqnHol\r\nP7EKcFv4Kf7dRwrCubHUP9UKFL/etw07v0CU7Rw7dI/r8tuzrEAICfUcA9M8\r\nkccbyyB5LiJqnQs+vA/LC5gLKIgzwmABgWvSUYUNGnTTPlX/tISMCjPAUckq\r\nVzd/ZBeW5oE3CcQsmo5KWhkehuYgEAwc3ZdET08Milsk2Xxc7ZdNzaUF87hL\r\n0A6irVBFCXVYQ40wrBvbjgdHrWTAn6xXanhL0TvdPSR1+b0dgbDvDef0xAPF\r\nrG8oTzpy/ATHc5qhhig/GWblgx5lWqL3Mt/usNJ5cR98RroH+QTlubAjoX1B\r\nxiq0jUqGTq/Tm+7NEr5zAiQRAk7p2hBrR5I1G2/IXW7wj9QnRqyn2Qk0Wq1p\r\ndWAG7q7VnJhsySYeH5qtFlllwbD58cS5S8m2O2CQj6xctkypZzZ/tUjp6C89\r\nrwlOuMhsbC+1G2DiqcIRKOyI1ou01X04zQg=\r\n=L1D9\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"alphasoftx","email":"alphasoftx@gmail.com"},"directories":{},"maintainers":[{"name":"alphasoftx","email":"alphasoftx@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/google-auth_1.1.0_1655538432552_0.09765547980129408"},"_hasShrinkwrap":false}},"maintainers":[{"name":"alphasoftx","email":"alphasoftx@gmail.com"}],"description":"Google Auth is a NodeJS library which helps in authentication with google in backend!","keywords":["google-auth","alphasoftx","backend","nodejs","library"],"author":{"name":"AlphaSoftX"},"license":"ISC","readme":"# Google Auth\n\nGoogle Auth is a NodeJS library which helps in authentication with google in backend!\n\nThis library simply exports 2 functions, `getGoogleAuthUrl` and `getGoogleUser`, which helps in authentication with google using nodejs backend!\n\n## Install\n\n### NPM\n\n#### Latest version\n\n```bash\nnpm i @alphasoftx/google-auth@latest\n```\n\n#### Specific version\n\n```bash\nnpm i @alphasoftx/google-auth@<version>\n```\n\n### Yarn\n\n#### Latest version\n\n```bash\nyarn add @alphasoftx/google-auth@latest\n```\n\n#### Specific version\n\n```bash\nyarn add @alphasoftx/google-auth@<version>\n```\n\n> Note: Remember to replace &lt;version> with your version name, for example, 1.0.0, 1.1.0, 2.0.1 etc.\n\n## Docs\n\nLet's have a look that how this library works, and to do so, we will create a small project!\n\n### Step 1\n\nCreate a folder named as `google-auth-example`, this is your project folder!\n\n### Step 2\n\nOpen command line interface in your project folder and initialise a nodejs project by executing the following command:- \n\n```bash\nnpm init\n```\n\nOR\n\n```bash\nyarn init\n```\n\nThese commands will ask some questions related to your project, answer them and your project will be initialised!\n\nNow add a field in your package.json file:- \n\n```js\n{ \n  \"type\": \"module\",\n} \n```\n\nThis field defines your project as a nodejs module, this is an important field as `@alphasoftx/google-auth` package doesn't supports `require('')` syntax!\n\n### Step 3\n\nCreate a file named as index.js, this will be your main project file!\n\n> Note: Project file name should be as same as you written in \"main\" field in package.json while initialisation of your project!\n\n### Step 4\n\nExecute the following command to install all of the dependencies of this project which are `express`, `express-session` and `@alphasoftx/google-auth`:- \n\n```bash\nnpm i express express-session @alphasoftx/google-auth\n```\n\nOR\n\n```bash\nyarn add express express-session @alphasoftx/google-auth\n```\n\n[Express](https://www.npmjs.com/package/express) is a backend server module which helps in building server!\n\n[Express session](https://www.npmjs.com/package/express-session) is a backend module which helps in authentication with express without any frontend role, you can use [jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken) instead of this module!\n\n> Note: you can have more dependencies according to your project, this project has three dependencies as this is a small project!\n\n### Step 5\n\nGo to [Google developers console](https://console.developers.google.com) and create a project with OAuth client id, for more details, you can watch this [youtube video](https://youtu.be/tjCxuDh5e9I)!\n\n### Step 6\n\nNow we will start writing code in index.js file:- \n\n```js\n// Importing required modules\nimport express from \"express\";\nimport session from \"express-session\";\nimport { getGoogleAuthUrl, getGoogleUser } from \"@alphasoftx/google-auth\";\n```\n\n```js\n// Variables\nconst app = express();\nconst port = 5000;\n\n// Middlewares\napp.use(express.json());\napp.use(\n  session({\n    secret: /* your express-session secret */,\n    resave: false,\n    saveUninitialized: true,\n  })\n);\n```\n\n```js\n// Endpoints of the server\napp.get('/', (req, res)=>{\n  if(req.session.user){\n    res.send(`\n      Name: ${req.session.user.name}<br>\n      Email: ${req.session.user.email}<br>\n      Image: <img src=\"${req.session.user.picture}\" alt=\"User image\" referrerpolicy=\"no-referrer\"/><br>\n      <a href=\"http://localhost:${port}/logout\">Logout</a>\n    `);\n  }else{\n    res.send(`<a href='${getGoogleAuthUrl({\n      redirectUrl: `http://localhost:${port}/auth/google`,\n      clientId: /* your cliend id */\n    })}'>Google</a>`);\n  }\n});\n\napp.get('/auth/google', async (req, res)=>{\n  let user = await getGoogleUser({\n    ...req.query,\n    clientId: /* your client id */,\n    clientSecret: /* your client secret */,\n    redirectUrl: `http://localhost:${port}/auth/google`\n  });\n  if(user.err === true){\n    return res.status(404).send(\"Url not found!\");\n  }\n  req.session.user = user;\n  req.session.save();\n  res.redirect('/');\n});\n\napp.get('/logout', (req, res)=>{\n  req.session.destroy(err=>{\n    if(err) return console.log(err.message);\n    res.redirect('/');\n  });\n});\n```\n\n```js\n// Starting nodejs server\napp.listen(port, () => {\n  console.log(`Your server is running at http://localhost:${port}`);\n});\n```\n\n### Step 7\n\nNow run your server with following command:- \n\n```bash\nnode index.js\n```\n\n### Summary\n\n`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!\n\n`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!\n\n> 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!\n","readmeFilename":"README.md"}