1.1.0 • Published 4 years ago

rest-oauth2 v1.1.0

Weekly downloads
13
License
ISC
Repository
github
Last release
4 years ago

Rest OAuth2

No more passport, just use simple REST API for OAuth2.

To use client side is not recommended.

Typescript supported.

Contents

Supported providers

  • Google
  • Facebook
  • PayPal

Other providers will be updated soon.

Installing

Using npm:

$ npm install rest-oauth2

Using yarn:

$ yarn add rest-oauth2

Usages

This example uses express

import express from 'express'
import { GoogleOAuth2 } from 'rest-oauth2'

const app = express()
const googleOAuth2 = new GoogleOAuth2({
  client_id: 'CLIENT_ID',
  client_secret: 'CLIENT_SECRET',
  redirect_uri: 'http://api.example.com/login/google/return',
})

app.get('/login/google', (req, res) => {
  const url = googleOAuth2.GenerateUrl()
  res.redirect(url)
})
app.get('/login/google/return', async (req, res) => {
  const { code } = req.query

  const { access_token } = await googleOAuth2.GetAccessToken(code)

  const userinfo = await googleOAuth2.GetProfile(access_token)

  // simpliy get user information!
  const email = userinfo.email
  ...
})

API

import { GoogleOAuth2, FacebookOAuth2, PaypalOAuth2 } from 'rest-oauth2'

GoogleOAuth2

const googleOAuth2 = new GoogleOAuth2({
  client_id: '',
  client_secret: '',
  redirect_uri: '',
})

GenerateUrl()

const url = googleOAuth2.GenerateUrl()

GetAccessToken()

const data = await googleOAuth2.GetAccessToken(code)
{
  "access_token": "",
  "scope": "",
  "id_token": "",
  "token_type": "Bearer",
  "expires_in": 3938
}

GetProfile()

const userinfo = await googleOAuth2.GetProfile(access_token)
{
  "id": "",
  "email": "",
  "name": "",
  "given_name": "",
  "family_name": "",
  "picture": "",
  "locale": "en",
  "verified_email": true
}

FacebookOAuth2

const facebookOAuth2 = new FacebookOAuth2({
  client_id: '',
  client_secret: '',
  redirect_uri: '',
})

GenerateUrl()

const url = facebookOAuth2.GenerateUrl()

GetAccessToken()

const data = await facebookOAuth2.GetAccessToken(code)
{
  "access_token": "",
  "token_type": "Bearer",
  "expires_in": 39312
}

GetProfile()

const userinfo = await facebookOAuth2.GetProfile(access_token)
{
  "id": "",
  "name": "",
  "first_name": "",
  "last_name": "",
  "email": ""
}

PaypalOAuth2

Paypal Rest API supports production and sandbox environments.

You should set different values by the environments.

const paypalOAuth2 = new PaypalOAuth2({
  client_id: '',
  client_secret: '',
  redirect_uri: '',
  production: true,
})

GenerateUrl()

const url = paypalOAuth2.GenerateUrl()

GetAccessToken()

const data = await paypalOAuth2.GetAccessToken(code)
{
  "access_token": "",
  "refresh_token": "",
  "token_type": "Bearer",
  "nonce": "",
  "expires_in": 43123
}

GetProfile()

const userinfo = await paypalOAuth2.GetProfile(access_token)
{
  "user_id": "",
  "payer_id": "",
  "name": "",
  "locality": {
    "locality": "",
    "region": "",
    "country": ""
  },
  "verified_account": true,
  "emails": [
    {
      "value": "",
      "primary": true,
      "confirmed": true
    },
    {
      "value": "",
      "primary": false,
      "confirmed": true
    }
    // ...
  ]
}
1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago