Skip to content

Commit

Permalink
Add Google client
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Apr 9, 2021
1 parent 876870e commit afe0780
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 89 deletions.
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,36 @@

Pre-configured OAuth 2.0 clients based on [`simple-oauth2`](http://lelylan.github.io/simple-oauth2/). You can use it as a simpler alternative to Passport.js for implementing login flows via 3rd party login providers such as Google, Facebook, Apple, Twitter, etc.

- **Google** OAuth 2.0 client ([docs](https://developers.google.com/identity/protocols/oauth2/))
- **Facebook** OAuth 2.0 client ([docs](https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow))
- **GitHub** OAuth 2.0 client ([docs](https://docs.github.com/en/developers/apps/authorizing-oauth-apps))
- ... more is coming (create a [PR](https://docs.github.com/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request))

## Getting Started

```bash
$ npm install simple-oauth2-clients
```

```
GITHUB_CLIENT_ID=xxxxx
GITHUB_CLIENT_SECRET=xxxxx
Provide environment variables for the OAuth 2.0 providers that you need, For example:

```bash
# Google OAuth 2.0 credentials
# https://console.cloud.google.com/apis/credentials
GOOGLE_CLIENT_ID=xxxxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=xxxxx

# Facebook App
# https://developers.facebook.com/apps/
FACEBOOK_APP_ID=xxxxx
FACEBOOK_APP_SECRET=xxxxx

# GitHub App
# https://github.com/settings/applications
GITHUB_CLIENT_ID=xxxxx
GITHUB_CLIENT_SECRET=xxxxx

...
```

```js
Expand Down Expand Up @@ -49,8 +67,7 @@ app.get("/auth/github/return", async function (req, res, next) {
});

// Create a new user, or authenticate an existing one.
// const user = await connect(req, tokens);
// req.signIn(user);
// await authorize(req, tokens);
// res.redirect("/");
} catch (err) {
next(err);
Expand Down
6 changes: 2 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ module.exports = function config(api) {

plugins: [["@babel/plugin-proposal-class-properties", { loose: true }]],

ignore:
api.env() === "test"
? []
: ["src/types.ts", "**/__tests__/**", "**/*.test.ts"],
ignore: api.env() === "test" ? [] : ["*.d.ts", "*.test.ts"],

sourceMaps: api.env() === "production",

overrides: [
Expand Down
3 changes: 0 additions & 3 deletions facebook.d.js

This file was deleted.

3 changes: 0 additions & 3 deletions github.d.js

This file was deleted.

19 changes: 19 additions & 0 deletions google.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @copyright 2021-present Kriasoft (https://git.io/JOevo)
*/
import { AuthorizationCode as Base } from "simple-oauth2";
export declare type AuthorizationCodeOptions = {
/**
* Google client ID.
* @default process.env.GOOGLE_CLIENT_ID
*/
id?: string;
/**
* Google client secret.
* @default process.env.GOOGLE_CLIENT_SECRET
*/
secret?: string;
};
export declare class AuthorizationCode extends Base {
constructor(options?: AuthorizationCodeOptions);
}
33 changes: 33 additions & 0 deletions google.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AuthorizationCode = void 0;

var _simpleOauth = require("simple-oauth2");

/**
* @copyright 2021-present Kriasoft (https://git.io/JOevo)
*/
class AuthorizationCode extends _simpleOauth.AuthorizationCode {
constructor(options = {}) {
var _options$id, _options$secret;

super({
client: {
id: (_options$id = options.id) !== null && _options$id !== void 0 ? _options$id : process.env.GOOGLE_CLIENT_ID,
secret: (_options$secret = options.secret) !== null && _options$secret !== void 0 ? _options$secret : process.env.GOOGLE_CLIENT_SECRET
},
auth: {
tokenHost: "https://accounts.google.com",
tokenPath: "/o/oauth2/v2/auth",
authorizeHost: "https://www.googleapis.com",
authorizePath: "/oauth2/v4/token"
}
});
}

}

exports.AuthorizationCode = AuthorizationCode;
36 changes: 36 additions & 0 deletions google.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @copyright 2021-present Kriasoft (https://git.io/JOevo)
*/

import { AuthorizationCode as Base } from "simple-oauth2";

export type AuthorizationCodeOptions = {
/**
* Google client ID.
* @default process.env.GOOGLE_CLIENT_ID
*/
id?: string;

/**
* Google client secret.
* @default process.env.GOOGLE_CLIENT_SECRET
*/
secret?: string;
};

export class AuthorizationCode extends Base {
constructor(options: AuthorizationCodeOptions = {}) {
super({
client: {
id: options.id ?? (process.env.GOOGLE_CLIENT_ID as string),
secret: options.secret ?? (process.env.GOOGLE_CLIENT_SECRET as string),
},
auth: {
tokenHost: "https://accounts.google.com",
tokenPath: "/o/oauth2/v2/auth",
authorizeHost: "https://www.googleapis.com",
authorizePath: "/oauth2/v4/token",
},
});
}
}
39 changes: 0 additions & 39 deletions index.d.js

This file was deleted.

1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
*/
export { AuthorizationCode as FacebookAuthorizationCode } from "./facebook";
export { AuthorizationCode as GitHubAuthorizationCode } from "./github";
export * from "./types";
20 changes: 1 addition & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
FacebookAuthorizationCode: true,
GitHubAuthorizationCode: true
};
Object.defineProperty(exports, "FacebookAuthorizationCode", {
enumerable: true,
get: function () {
Expand All @@ -22,18 +18,4 @@ Object.defineProperty(exports, "GitHubAuthorizationCode", {

var _facebook = require("./facebook");

var _github = require("./github");

var _types = require("./types");

Object.keys(_types).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _types[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _types[key];
}
});
});
var _github = require("./github");
1 change: 0 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@

export { AuthorizationCode as FacebookAuthorizationCode } from "./facebook";
export { AuthorizationCode as GitHubAuthorizationCode } from "./github";
export * from "./types";
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
{
"name": "simple-oauth2-clients",
"version": "0.1.0",
"version": "0.2.0",
"description": "OAuth 2.0 client configurations (Google, Facebook, GitHub, etc.)",
"keywords": [
"apple",
"auth",
"authentication",
"client",
"credentials",
"facebook",
"github",
"google",
"http",
"oauth"
"oauth 2.0",
"oauth 2",
"oauth",
"oauth2",
"oauth20",
"token"
],
"license": "MIT",
"author": {
Expand All @@ -27,8 +33,8 @@
"type": "patreon",
"url": "https://patreon.com/koistya"
},
"main": "lib/validate.js",
"types": "lib/validate.d.ts",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"lint": "eslint --report-unused-disable-directives .",
"test": "jest",
Expand Down
1 change: 0 additions & 1 deletion types.d.js

This file was deleted.

4 changes: 0 additions & 4 deletions types.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion types.js

This file was deleted.

4 changes: 0 additions & 4 deletions types.ts

This file was deleted.

0 comments on commit afe0780

Please sign in to comment.