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

Update auth ap paths #522

Merged
merged 15 commits into from
Jul 27, 2022
Merged
6 changes: 6 additions & 0 deletions .changeset/plenty-readers-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'demo-parse-server-auth-server': minor
'@moralisweb3/auth': minor
---

Update endpoints for evm Authentication in Moralis.Auth to reflect api changes
1 change: 1 addition & 0 deletions demos/demo-parse-server-auth/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<h1>Demo Auth parse-server</h1>
</header>
<main>
<div id="error"></div>
<button class="btn" id="auth-metamask">Authenticate via Metamask</button>
<div id="user"></div>
</main>
Expand Down
14 changes: 11 additions & 3 deletions demos/demo-parse-server-auth/public/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const AUTH_API_URL = 'http://localhost:1337/api/auth';

const elError = document.getElementById('error');
const elUser = document.getElementById('user');
const elBtnMetamask = document.getElementById('auth-metamask');

const handleApiPost = async (endpoint, params) => {
const result = await axios.post(`${AUTH_API_URL}/${endpoint}`, params, {
headers: {
Expand Down Expand Up @@ -57,12 +61,16 @@ const handleAuth = async () => {
};

const renderUser = (user) => {
document.getElementById('user').innerHTML = user ? JSON.stringify(user, null, 2) : '';
elUser.innerHTML = user ? JSON.stringify(user, null, 2) : '';
};

const renderError = (error) => {
elError.innerHTML = error ? JSON.stringify(error.message, null, 2) : '';
};

function init() {
document.getElementById('auth-metamask').addEventListener('click', async () => {
handleAuth();
elBtnMetamask.addEventListener('click', async () => {
handleAuth().catch((error) => renderError(error));
});
}

Expand Down
7 changes: 7 additions & 0 deletions demos/demo-parse-server-auth/public/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ h1 {
font-size: 1rem;
color: rgb(4, 24, 54);
}

#error {
margin-bottom: 1rem;
line-height: 1.2;
font-size: 1rem;
color: rgb(200, 31, 31);
}
12 changes: 6 additions & 6 deletions packages/auth/src/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*/

export interface paths {
"/challenge": {
post: operations["initializeChallenge"];
"/challenge/request/evm": {
post: operations["Request Challenge (EVM)"];
};
"/challenge/complete": {
post: operations["completeChallenge"];
"/challenge/verify/evm": {
post: operations["Verify Challenge (EVM)"];
};
"/health": {
get: operations["HealthController_check"];
Expand Down Expand Up @@ -175,7 +175,7 @@ export interface components {
}

export interface operations {
initializeChallenge: {
"Request Challenge (EVM)": {
parameters: {};
responses: {
/** The back channel challenge containing the id to store on the api and the message to be signed by the user */
Expand All @@ -191,7 +191,7 @@ export interface operations {
};
};
};
completeChallenge: {
"Verify Challenge (EVM)": {
parameters: {};
responses: {
/** The token to be used to call the third party API from the client */
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/methods/requestMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AuthErrorCode, MoralisAuthError } from '@moralisweb3/core';
import { EvmAddress, EvmAddressish, EvmChain, EvmChainish } from '@moralisweb3/evm-utils';
import { initializeChallengeResolver } from '../resolvers/initializeChallenge';
import { initializeChallengeResolver } from '../resolvers/evmRequestChallenge';

export enum AuthNetwork {
EVM = 'evm',
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/methods/verify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertUnreachable } from '@moralisweb3/core';
import { completeChallengeResolver } from '../resolvers/completeChallenge';
import { completeChallengeResolver } from '../resolvers/evmVerifyChallenge';

export interface VerifyEvmOptions {
message: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { toCamelCase } from '@moralisweb3/core';
import { operations } from '../generated/types';
import { BASE_URL } from '../MoralisAuth';

type name = 'initializeChallenge';
type name = 'Request Challenge (EVM)';
type BodyParams = operations[name]['requestBody']['content']['application/json'];
type ApiParams = BodyParams;
const method = 'post';
Expand Down Expand Up @@ -31,8 +31,8 @@ const apiToResult = (apiData: ApiResult, params: Params) => {
};

export const initializeChallengeResolver = new ApiResolver({
name: 'initializeChallenge',
getUrl: (params: Params) => `${BASE_URL}/challenge/`,
name: 'Request Challenge (EVM)',
getUrl: (params: Params) => `${BASE_URL}/challenge/request/evm`,
apiToResult: apiToResult,
resultToJson: (data) => ({
...data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ApiResolver } from '@moralisweb3/api-utils';
import { operations } from '../generated/types';
import { BASE_URL } from '../MoralisAuth';

type name = 'completeChallenge';
type name = 'Verify Challenge (EVM)';
type BodyParams = operations[name]['requestBody']['content']['application/json'];
type ApiParams = BodyParams;
type Params = ApiParams;
Expand All @@ -14,8 +14,8 @@ const bodyParams = ['message', 'signature'] as const;
type ApiResult = operations[name]['responses']['201']['content']['application/json'];

export const completeChallengeResolver = new ApiResolver({
name: 'completeChallenge',
getUrl: (params: Params) => `${BASE_URL}/challenge/complete`,
name: 'Verify Challenge (EVM)',
getUrl: (params: Params) => `${BASE_URL}/challenge/verify/evm`,
apiToResult: ({ chainId, ...data }: ApiResult) => ({
...data,
// TODO: revisit EVM logic once we know how authentication in other networks work
Expand Down