Skip to content

Commit

Permalink
Merge pull request #16 from ExWeiv/development
Browse files Browse the repository at this point in the history
Helper Functions for Velo APIs and Integration + Docs Updated
  • Loading branch information
loeiks authored Mar 26, 2024
2 parents fa9da5d + fe426ed commit 2f65c50
Show file tree
Hide file tree
Showing 69 changed files with 2,608 additions and 426 deletions.
20 changes: 19 additions & 1 deletion DOCS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# Easy Auth Setup

Currently this documentation is in build.
This package is built for developers and not for beginners instead it's more focusced for advanced users/developers. The goal of this package is give you ability to integrate popular OAuth options with your Wix website via Velo + JS. You'll find ready to use APIs to integrate OAuth apps with full customization or use few extra APIs for direct setup with less customization.

You can read more about APIs in this docs. You can import OAuth options with their names like this:

```js
import { discord } from "@exweiv/easy-auth";
```

You can also import general OAuth helpers that's actually using Velo APIs like this:

```js
import { oauth } from "@exweiv/easy-auth";
```

`oauth` object will give you some functions to integrate oauth options with Wix Members quickly with less customization. If you want full customization you can build your own sing in and sign up process.

---

**Read docs to understand functions and how to use them.**
41 changes: 41 additions & 0 deletions app/lib/Auth/auth-backend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSessionToken = void 0;
const errors_1 = require("../Errors/errors");
const wix_members_backend_1 = require("wix-members-backend");
const providers = __importStar(require("../index"));
async function getSessionToken(provider, options, access_token) {
try {
const response = await providers[provider].authUser(options, undefined, access_token);
const email = response.email;
const sessionToken = await wix_members_backend_1.authentication.generateSessionToken(email);
return sessionToken;
}
catch (err) {
throw Error(`${errors_1.prefix} ${provider} - failed to login member with email.`);
}
}
exports.getSessionToken = getSessionToken;
2 changes: 1 addition & 1 deletion app/lib/GitHub/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const authUser = async (options, client_secret, access_token) => {
headers: {
"Accept": "application/vnd.github+json",
"Authorization": `Bearer ${access_token}`,
"X-GitHub-Api-Version": "2022-11-28"
"X-github-Api-Version": "2022-11-28"
}
});
return githubUserResponse.data;
Expand Down
2 changes: 1 addition & 1 deletion app/lib/Steam/steam.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const authUser = async (options, client_secret) => {
const steamUserResponse = await axios_1.default.get(`https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${!client_secret ? await (0, wix_secret_helpers_1.getSecretValue)("SteamClientSecret") : client_secret}&steamids=${steamId}`);
const result = steamUserResponse.data;
if (!(result && result.response && Array.isArray(result.response.players) && result.response.players.length > 0)) {
throw new Error(`Malformed response while retrieving user's Steam profile information`);
throw new Error(`Malformed response while retrieving user's steam profile information`);
}
return result.response.players[0];
}
Expand Down
3 changes: 2 additions & 1 deletion app/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.steam = exports.github = exports.discord = exports.facebook = exports.google = void 0;
exports.oauth = exports.steam = exports.github = exports.discord = exports.facebook = exports.google = void 0;
exports.google = __importStar(require("./Google/google"));
exports.facebook = __importStar(require("./Facebook/facebook"));
exports.discord = __importStar(require("./Discord/discord"));
exports.github = __importStar(require("./GitHub/github"));
exports.steam = __importStar(require("./Steam/steam"));
exports.oauth = __importStar(require("./Auth/auth-backend"));
4 changes: 2 additions & 2 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exweiv/easy-auth",
"version": "0.1.1",
"version": "0.2.1",
"description": "Integrate popular OAuth apps with your Wix website easily.",
"main": "./lib/index.js",
"scripts": {
Expand Down
18 changes: 18 additions & 0 deletions app/src/Auth/auth-backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path="../../types/wix-members-backend.d.ts" />
/// <reference path="../../types/index.d.ts" />

import { prefix } from '../Errors/errors';
import { authentication } from 'wix-members-backend';
import { oauth } from '@exweiv/easy-auth';
import * as providers from '../index';

export async function getSessionToken(provider: oauth.OAuthProviders, options: oauth.OAuthOptions, access_token: string): Promise<string> {
try {
const response = await providers[provider].authUser(options, undefined, access_token);
const email = response.email;
const sessionToken = await authentication.generateSessionToken(email);
return sessionToken;
} catch (err) {
throw Error(`${prefix} ${provider} - failed to login member with email.`);
}
}
8 changes: 4 additions & 4 deletions app/src/Discord/discord.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="../../types/index.d.ts" />

// Type Imports
import type { Discord, AuthResponse } from '@exweiv/easy-auth';
import type { discord, AuthResponse } from '@exweiv/easy-auth';
// API Imports
import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';
Expand All @@ -10,7 +10,7 @@ import { getSecretValue } from '@exweiv/wix-secret-helpers';
// Internal Imports
import errCodes from '../Errors/errors';

export const redirectURL = (options: Discord.RedirectURLOptions): string => {
export const redirectURL = (options: discord.RedirectURLOptions): string => {
try {
const {
client_id,
Expand Down Expand Up @@ -41,7 +41,7 @@ export const redirectURL = (options: Discord.RedirectURLOptions): string => {
}
}

export const authUser = async (options: Discord.AuthOptions, client_secret?: string, access_token?: string): Promise<AuthResponse> => {
export const authUser = async (options: discord.AuthOptions, client_secret?: string, access_token?: string): Promise<AuthResponse> => {
try {
const {
code,
Expand Down Expand Up @@ -73,7 +73,7 @@ export const authUser = async (options: Discord.AuthOptions, client_secret?: str
}
}

export const getTokens = async (options: Discord.TokensOptions): Promise<Discord.TokensResponse> => {
export const getTokens = async (options: discord.TokensOptions): Promise<discord.TokensResponse> => {
try {
const tokenParams = new URLSearchParams();

Expand Down
8 changes: 4 additions & 4 deletions app/src/Facebook/facebook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="../../types/index.d.ts" />

// Type Imports
import type { Facebook, AuthResponse } from '@exweiv/easy-auth';
import type { facebook, AuthResponse } from '@exweiv/easy-auth';
// API Imports
import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';
Expand All @@ -10,7 +10,7 @@ import { getSecretValue } from '@exweiv/wix-secret-helpers';
// Internal Imports
import errCodes from '../Errors/errors';

export const redirectURL = (options: Facebook.RedirectURLOptions): string => {
export const redirectURL = (options: facebook.RedirectURLOptions): string => {
try {
const {
redirect_uri,
Expand Down Expand Up @@ -39,7 +39,7 @@ export const redirectURL = (options: Facebook.RedirectURLOptions): string => {
}
}

export const authUser = async (options: Facebook.AuthOptions, client_secret?: string, access_token?: string): Promise<AuthResponse> => {
export const authUser = async (options: facebook.AuthOptions, client_secret?: string, access_token?: string): Promise<AuthResponse> => {
try {
const { client_id, redirect_uri, code, fields } = options;

Expand All @@ -64,7 +64,7 @@ export const authUser = async (options: Facebook.AuthOptions, client_secret?: st
}
}

export const getTokens = async (options: Facebook.TokensOptions): Promise<Facebook.TokensResponse> => {
export const getTokens = async (options: facebook.TokensOptions): Promise<facebook.TokensResponse> => {
try {
const {
client_id,
Expand Down
10 changes: 5 additions & 5 deletions app/src/GitHub/github.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="../../types/index.d.ts" />

// Type Imports
import type { GitHub, AuthResponse } from '@exweiv/easy-auth';
import type { github, AuthResponse } from '@exweiv/easy-auth';
// API Imports
import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';
Expand All @@ -10,7 +10,7 @@ import querystring from 'querystring';
// Internal Imports
import errCodes from '../Errors/errors';

export const redirectURL = (options: GitHub.RedirectURLOptions): string => {
export const redirectURL = (options: github.RedirectURLOptions): string => {
try {
const {
client_id,
Expand All @@ -37,7 +37,7 @@ export const redirectURL = (options: GitHub.RedirectURLOptions): string => {
}
}

export const authUser = async (options: GitHub.AuthOptions, client_secret?: string, access_token?: string): Promise<AuthResponse> => {
export const authUser = async (options: github.AuthOptions, client_secret?: string, access_token?: string): Promise<AuthResponse> => {
try {
const {
client_id,
Expand All @@ -61,7 +61,7 @@ export const authUser = async (options: GitHub.AuthOptions, client_secret?: stri
headers: {
"Accept": "application/vnd.github+json",
"Authorization": `Bearer ${access_token}`,
"X-GitHub-Api-Version": "2022-11-28"
"X-github-Api-Version": "2022-11-28"
}
});
return githubUserResponse.data;
Expand All @@ -70,7 +70,7 @@ export const authUser = async (options: GitHub.AuthOptions, client_secret?: stri
}
}

export const getTokens = async (options: GitHub.TokensOptions): Promise<GitHub.TokensResponse> => {
export const getTokens = async (options: github.TokensOptions): Promise<github.TokensResponse> => {
try {
const tokenParams = new URLSearchParams();

Expand Down
8 changes: 4 additions & 4 deletions app/src/Google/google.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="../../types/index.d.ts" />

// Type Imports
import type { Google, AuthResponse } from '@exweiv/easy-auth';
import type { google, AuthResponse } from '@exweiv/easy-auth';
// API Imports
import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';
Expand All @@ -10,7 +10,7 @@ import querystring from 'querystring';
// Internal Imports
import errCodes from '../Errors/errors';

export const redirectURL = (options: Google.RedirectURLOptions): string => {
export const redirectURL = (options: google.RedirectURLOptions): string => {
try {
const {
redirect_uri,
Expand Down Expand Up @@ -43,7 +43,7 @@ export const redirectURL = (options: Google.RedirectURLOptions): string => {
}
}

export const authUser = async (options: Google.AuthOptions, client_secret?: string, access_token?: string): Promise<AuthResponse> => {
export const authUser = async (options: google.AuthOptions, client_secret?: string, access_token?: string): Promise<AuthResponse> => {
try {
const { client_id, redirect_uri, code, grant_type } = options;

Expand All @@ -69,7 +69,7 @@ export const authUser = async (options: Google.AuthOptions, client_secret?: stri
}
}

export const getTokens = async (options: Google.TokensOptions): Promise<Google.TokensResponse> => {
export const getTokens = async (options: google.TokensOptions): Promise<google.TokensResponse> => {
try {
const {
client_id,
Expand Down
8 changes: 4 additions & 4 deletions app/src/Steam/steam.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/// <reference path="../../types/index.d.ts" />

// Type Imports
import type { Steam, AuthResponse } from '@exweiv/easy-auth';
import type { steam, AuthResponse } from '@exweiv/easy-auth';
// API Imports
import axios from 'axios';
import { getSecretValue } from '@exweiv/wix-secret-helpers';
import querystring from 'querystring';
// Internal Imports
import errCodes from '../Errors/errors';

export const redirectURL = (options: Steam.RedirectURLOptions): string => {
export const redirectURL = (options: steam.RedirectURLOptions): string => {
try {
const {
realm,
Expand All @@ -32,15 +32,15 @@ export const redirectURL = (options: Steam.RedirectURLOptions): string => {
}
}

export const authUser = async (options: Steam.AuthOptions, client_secret?: string): Promise<AuthResponse> => {
export const authUser = async (options: steam.AuthOptions, client_secret?: string): Promise<AuthResponse> => {
try {
const { steamId } = options;

const steamUserResponse = await axios.get(`https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${!client_secret ? await getSecretValue("SteamClientSecret") : client_secret}&steamids=${steamId}`)
const result = steamUserResponse.data;

if (!(result && result.response && Array.isArray(result.response.players) && result.response.players.length > 0)) {
throw new Error(`Malformed response while retrieving user's Steam profile information`);
throw new Error(`Malformed response while retrieving user's steam profile information`);
}

return result.response.players[0];
Expand Down
3 changes: 2 additions & 1 deletion app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * as google from './Google/google';
export * as facebook from './Facebook/facebook';
export * as discord from './Discord/discord';
export * as github from './GitHub/github';
export * as steam from './Steam/steam';
export * as steam from './Steam/steam';
export * as oauth from './Auth/auth-backend';
Loading

0 comments on commit 2f65c50

Please sign in to comment.