https://github.com/pitops/passgate
Simple and unobtrusive authentication for third-party OAuth2 providers
https://github.com/pitops/passgate
auth express-middleware google-oauth third-party-authentication
Last synced: about 1 year ago
JSON representation
Simple and unobtrusive authentication for third-party OAuth2 providers
- Host: GitHub
- URL: https://github.com/pitops/passgate
- Owner: pitops
- Created: 2019-06-05T14:47:06.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-10T18:16:59.000Z (over 3 years ago)
- Last Synced: 2025-03-18T21:09:32.015Z (over 1 year ago)
- Topics: auth, express-middleware, google-oauth, third-party-authentication
- Language: JavaScript
- Homepage:
- Size: 669 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Passgate
[]()
[](https://github.com/pitops/passgate/issues)
[](https://github.com/pitops/passgate/pulls)
[](/LICENSE)
##
💡 Easy third-party authentication middleware for express
## 📝 Table of Contents
- [About](#about)
- [Getting Started](#getting_started)
- [TODO](../TODO.md)
- [Authors](#authors)
Passgate is yet another OAuth2 library heavily inspired from passport with the subtle difference of having everything configured in one place.
The purpose of passgate is to provide a simple and easy unobtrusive way of authenticating with third party OAUTH2 providers. **Right now, it only works with Google OAuth2** but it will be expanded in the near future.
In general, Passgate is a middleware that gets passed a config object and thats it. It will do the dirty work for you.
### Step 1
First install the npm package
```bash
npm i passgate
```
### Step 2
Create a file named `google.js` and import the following.
```javascript
const GoogleStrategy = {
google: {
clientID: 'GOOGLE_CLIENT_ID',
clientSecret: 'GOOGLE_CLIENT_SECRET',
authPath: '/auth/google',
callbackPath: '/auth/google/callback',
revokePath: '/auth/google/revoke',
callbackURL: `${LOCALHOST_URL}/auth/google/callback`,
scope: ['https://www.googleapis.com/auth/youtube'],
access_type: 'offline',
successRedirect: '/',
failureRedirect: '/login',
eventCallbacks: {
onAuthSuccess,
onTokensRefresh,
onAccessRevoke
}
}
}
function onAuthSuccess(
{accessToken, refreshToken, tokenExpiryDate, profile},
done
) {
console.log({accessToken, refreshToken, tokenExpiryDate, profile})
// do DB stuff
done() // this must be called after you finish with DB stuff
}
function onTokensRefresh({access_token, refresh_token, expiry_date}) {
console.log({access_token, refresh_token, expiry_date})
}
function onAccessRevoke(done) {
// do database level stuff
done('INSERT REFRESH TOKEN') // here pass the actual refreshToken from your db
}
module.exports = GoogleStrategy
```
The above code can be called a Strategy. In this case for Google. As you can see everything is in once place for authenticating and de-authenticating with Google.
**Make sure you get your CLIENT_ID and CLIENT_SECRET** from [Google dev console](https://console.developers.google.com/)
# Step 3
```javascript
// server.js
const express = require('express')
const passgate = require('passgate')
const GoogleStrategy = require('./google')
const app = express()
const port = process.env.PORT || 3000
passgate.init(GoogleStrategy)
app.use(passgate)
app.get('/', async (req, res) => {
res.send('Hello world')
})
app.listen(port, () => console.log(`App running on ${port}!`))
```
### Prerequisites
Right now Passgate only works with **Express** so make sure your project supports that.
## TODO
- [x] Add Google OAUTH
- [ ] Add Facebook OAUTH
- [ ] Add Github OAUTH
- [@pitops](https://github.com/pitops) - Idea & Initial work
## Acknowledgements
