Skip to content

Commit

Permalink
Merge pull request #5 from Gateway-DAO/feat/Auth
Browse files Browse the repository at this point in the history
Feat/auth
  • Loading branch information
Siddharth9890 authored Nov 29, 2023
2 parents de03231 + 9c076e1 commit 29694e9
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "jest",
"dev": "ts-node ./index.ts",
"dev": "ts-node src/index.ts",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier --write ."
Expand Down
3 changes: 3 additions & 0 deletions src/Gateway.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getMeshSDK, Sdk } from '../.mesh';
import { Organization } from './organization/organization';
import { Auth } from './auth/auth';
import { PDA } from './pda/pda';
import { DataRequestTemplate } from './dataRequestsTemplate/dataRequestsTemplate';

Expand All @@ -8,6 +9,7 @@ export class Gateway {
public pda: PDA;
public dataRequestTemplate: DataRequestTemplate;
public organization: Organization;
public auth: Auth;

constructor({ apiKey, token }: { apiKey: string; token: string }) {
if (!apiKey && !token) throw new Error('No token found');
Expand All @@ -18,5 +20,6 @@ export class Gateway {
this.pda = new PDA(this.sdk);
this.dataRequestTemplate = new DataRequestTemplate(this.sdk);
this.organization = new Organization(this.sdk);
this.auth = new Auth(this.sdk);
}
}
189 changes: 189 additions & 0 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { AddWalletConfirmationInput, Sdk } from '../../.mesh';
import { AuthType, Chain } from '../types';
import { errorHandler } from '../utils/errorHandler';

export class Auth {
private sdk: Sdk;

constructor(sdk: Sdk) {
this.sdk = sdk;
}

/**
* The function checks the availability of a username by making a async query to the SDK.
* @param {string} username - The `username` parameter is a string that represents the username that
* needs to be checked for availability.
* @returns the result of the `checkUsernameAvailability` method, is a boolean
*/
async checkUsernameAvailability(username: string) {
try {
return (await this.sdk.checkUsernameAvailability_query({ username }))
.checkUsernameAvailability;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `addEmail` is an asynchronous function that adds an email to a mutation using the
* SDK.
* @param {string} email - The `email` parameter is a string that represents the email address that
* needs to be added.
* @returns The addEmail function is returning the result code and email
*/
async addEmail(email: string) {
try {
return (await this.sdk.addEmail_mutation({ input: { email } })).addEmail;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `addEmailConfirmation` takes an email and code as input, and calls a mutation to add
* email confirmation.
* @param {string} email: a string representing the email address to be confirmed
* @param {number} code : a 6 digit code representing that was sent
* @returns the result of the `addEmailConfirmation` method call, is the logged in user.
*/
async addEmailConfirmation(email: string, code: number) {
try {
return (
await this.sdk.addEmailConfirmation_mutation({
input: { code, email },
})
).addEmailConfirmation;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `addWallet` takes an wallet and optional chain as input.
* @param {string} wallet: a string representing the wallet
* @param {Chain} chain : a chain optional of type Chain
* @returns the result of the `addWallet` method call, is a message which will be used to confirm wallet.
*/
async addWallet(wallet: string, chain?: Chain) {
try {
return (await this.sdk.addWallet_mutation({ input: { wallet, chain } }))
.addWallet;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `addWalletConfirmation` takes an walletConfirmationInput input, and calls a mutation to add
* wallet confirmation.
* @param {AddWalletConfirmationInput} walletConfirmationInput: walletConfirmationInput of type AddWalletConfirmationInput
* @returns the result of the `addWalletConfirmation` method call, is the logged in user.
*/
async addWalletConfirmation(
walletConfirmationInput: AddWalletConfirmationInput,
) {
try {
return await this.sdk.addWalletConfirmation_mutation({
input: walletConfirmationInput,
});
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `createWalletNounce` takes an wallet and optional chain as input
* @param {string} wallet: a string representing the wallet
* @param {Chain} chain : a chain optional of type Chain
* @returns the result of the `createWalletNounce` method call, is a message which will be used to confirm wallet.
*/
async createWalletNonce(wallet: string, chain?: Chain) {
try {
return (
await this.sdk.createWalletNonce_mutation({ input: { wallet, chain } })
).createWalletNonce;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `deleteAccount` takes an id to soft delete the account
* @param {string} id: a string representing the user id
* @returns the result of the `deleteAccount` method call,returning the boolean | undefined if user not found
*/
async deleteAccount(id: string) {
try {
return (await this.sdk.deleteAccount_mutation({ id })).deleteAccount;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `loginEmail` takes an email and code as input verifies it and gives user details
* @param {string} email: a string representing the email
* @param {number} code: a number representing the verification code
* @returns the result of the `loginEmail` method call,returning the user if code is correct
*/
async loginEmail(email: string, code: number) {
try {
return (await this.sdk.loginEmail_mutation({ input: { email, code } }))
.loginEmail;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `loginWallet` takes an wallet and signature as input verifies it and gives user details
* @param {string} wallet: a string representing the email
* @param {string} signature: a string representing the signature generated
* @returns the result of the `loginWallet` method call,returning the user if signature is correct
*/
async loginWallet(wallet: string, signature: string) {
try {
return (
await this.sdk.loginWallet_mutation({
input: { wallet, signature },
})
).loginWallet;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `refreshToken` takes an existing refresh token to create a new one
* @param {string} existingRefreshToken: a string representing the existing refresh token
* @returns the result of the `refreshToken` method call,returning the new refresh token and user
*/
async refreshToken(existingRefreshToken: string) {
try {
return (
await this.sdk.refreshToken_mutation({
input: { refresh_token: existingRefreshToken },
})
).refreshToken;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function unregisterAuthMethod is an asynchronous function that takes in a JSON object and an
* AuthType, and attempts to unregister an authentication method using the SDK.
* @param {string} data: a string representing either email or wallet
* @param {AuthType} type: a AuthType representing the type like Wallet, Email and so on
* @returns the result of the `this.sdk.unregisterAuthMethod_mutation` method, which is awaited.
*/
async unregisterAuthMethod(data: string, type: AuthType) {
try {
return await this.sdk.unregisterAuthMethod_mutation({
input: { data, type },
});
} catch (error) {
throw new Error(errorHandler(error));
}
}
}
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ export enum OrganizationRole {
Member = 'Member',
Owner = 'Owner',
}

export enum AuthType {
'EMAIL' = 'EMAIL',
'GOOGLE' = 'GOOGLE',
'HOT_WALLET' = 'HOT_WALLET',
'WALLET' = 'WALLET',
}

export enum Chain {
EVM = 'EVM',
SOL = 'SOL',
}
56 changes: 56 additions & 0 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import dotenv from 'dotenv';
import { Gateway } from '../src/Gateway';

dotenv.config();
let api: Gateway;
const DEFAULT_TIMEOUT = 100000;

beforeAll(() => {
api = new Gateway({
apiKey: process.env.API_KEY!,
token: process.env.BEARER_TOKEN!,
});
});

describe('auth test', () => {
it(
'check username availability',
async () => {
const result = await api.auth.checkUsernameAvailability('sid');
expect(result).toEqual(false);
},
DEFAULT_TIMEOUT,
);

it(
'add email',
async () => {
const { email, code } = await api.auth.addEmail('[email protected]');
expect(email).toBe('[email protected]');
expect(code).toBeDefined();
},
DEFAULT_TIMEOUT,
);

it(
'add wallet',
async () => {
const { message } = await api.auth.addWallet(
'0xD73e46DeBD3958F2706903A6D3644C570285EC1F',
);
expect(message.length).toBeDefined();
},
DEFAULT_TIMEOUT,
);

it(
'create wallet nounce',
async () => {
const { message } = await api.auth.createWalletNonce(
'0xCf084430Fc2CfAd8E81716aEdeBBE4458866D239',
);
expect(message.length).toBeDefined();
},
DEFAULT_TIMEOUT,
);
});

0 comments on commit 29694e9

Please sign in to comment.