Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/auth #5

Merged
merged 23 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2f4f6ee
feat: added all auth related queries and mutations and test cases
Siddharth9890 Nov 6, 2023
a8cfdc2
chore: added JSDocs for auth methods
Siddharth9890 Nov 7, 2023
4ba5414
chore: seems custom fetch problem resolved for now
Siddharth9890 Nov 7, 2023
ab00743
chore: renamed PDA=>pda
Siddharth9890 Nov 8, 2023
7ff9d7f
chore: removed pda class
Siddharth9890 Nov 9, 2023
87c3c49
feat: added error hanlder and fixed tests
Siddharth9890 Nov 10, 2023
8c75037
Merge branch 'main' into feat/Auth
Siddharth9890 Nov 10, 2023
8879430
chore: resolved unused import error
Siddharth9890 Nov 10, 2023
b47949b
chore: removed email nounce method
Siddharth9890 Nov 13, 2023
b94ddbf
chore: fixed linter error
Siddharth9890 Nov 13, 2023
bcea6de
feat: added types for unregister auth method
Siddharth9890 Nov 21, 2023
dfeaebc
chore: fixing linting errors
Siddharth9890 Nov 21, 2023
a1270cf
chore: added parameters instead of objects
Siddharth9890 Nov 21, 2023
85a029a
Merge branch 'main' into feat/Auth
Siddharth9890 Nov 21, 2023
08beff8
chore: merging pda branch
Siddharth9890 Nov 21, 2023
b68ac85
chore: format errors
Siddharth9890 Nov 21, 2023
8dffc20
chore: reorder classes
Siddharth9890 Nov 21, 2023
76ff9b5
chore: testing auth method type for unregister
Siddharth9890 Nov 22, 2023
7398ef3
chore: cleaning up auth methods
Siddharth9890 Nov 29, 2023
4dfdaa1
Merge branch 'main' into feat/Auth
Siddharth9890 Nov 29, 2023
f6ecdc1
chore: merge main changes
Siddharth9890 Nov 29, 2023
d6bc8c9
Merge branch 'main' into feat/Auth
Siddharth9890 Nov 29, 2023
9c076e1
chore: createWalletNonce=>createWalletNounce
Siddharth9890 Nov 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('sid@test.com');
expect(email).toBe('sid@test.com');
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,
);
});
Loading