-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add client side authentication to gravityforms rest api (#7)
* Move form field to a seperate comp & started to work on textarea * More textarea work & default confirmation text * Fix linting * Moved backend url to props instead of direclty use .env * added .env example * Feat: add client side authentication * Fix linitng * Remove js-sha1
- Loading branch information
Showing
11 changed files
with
164 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
PUBLIC_GF_CONSUMER_KEY= | ||
PUBLIC_GF_CONSUMER_SECRECT= | ||
PUBLIC_GF_API_URL= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export type Props = { | ||
formId?: number; | ||
formId: number | undefined; | ||
backendUrl: string | undefined; | ||
consumerKey: string | undefined; | ||
consumerSecret: string | undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
apps/svelte-gravity-forms/src/lib/internal/helpers/gf-rest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { get, type Writable } from 'svelte/store'; | ||
import type { GFFormObjectProps } from '../types.js'; | ||
|
||
import OAuth from 'oauth-1.0a'; | ||
import CryptoJS from 'crypto-js'; | ||
|
||
export async function sendSubmission( | ||
req: { [x: string]: unknown }, | ||
backendUrl: Writable<string | undefined>, | ||
formId: Writable<number> | ||
) { | ||
const res = await fetch(`${get(backendUrl)}gf/v2/forms/${get(formId)}/submissions`, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json;charset=UTF-8' | ||
} | ||
}); | ||
const data = await res.json(); | ||
return data; | ||
} | ||
|
||
export async function getClientFormObject( | ||
backendUrl: Writable<string | undefined>, | ||
formId: Writable<number>, | ||
consumer_key: Writable<string>, | ||
consumer_secret: Writable<string> | ||
) { | ||
const oauth = new OAuth({ | ||
consumer: { | ||
key: get(consumer_key), | ||
secret: get(consumer_secret) | ||
}, | ||
signature_method: 'HMAC-SHA1', | ||
hash_function(base_string, key) { | ||
return CryptoJS.HmacSHA1(base_string, key).toString(CryptoJS.enc.Base64); | ||
} | ||
}); | ||
|
||
// Generate OAuth 1.0 parameters | ||
const oauthData = oauth.authorize({ | ||
url: `${get(backendUrl)}gf/v2/forms/${get(formId)}`, | ||
method: 'GET' | ||
}); | ||
|
||
// Convert OAuth parameters to URL parameters | ||
const params = new URLSearchParams({ | ||
oauth_consumer_key: oauthData.oauth_consumer_key, | ||
oauth_nonce: oauthData.oauth_nonce, | ||
oauth_signature_method: oauthData.oauth_signature_method, | ||
oauth_timestamp: oauthData.oauth_timestamp.toString(), | ||
oauth_version: oauthData.oauth_version, | ||
oauth_signature: oauthData.oauth_signature | ||
}); | ||
|
||
// Append OAuth parameters to URL | ||
const url = `${get(backendUrl)}gf/v2/forms/${get(formId)}?${params.toString()}`; | ||
|
||
const res = await fetch(url, { method: 'GET' }); | ||
// eslint-disable-next-line no-console | ||
console.log(res); | ||
const data = (await res.json()) as GFFormObjectProps; | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
<script lang="ts"> | ||
import { SvelteGravityForm } from '$lib/index.js'; | ||
import { | ||
PUBLIC_GF_API_URL, | ||
PUBLIC_GF_CONSUMER_KEY, | ||
PUBLIC_GF_CONSUMER_SECRECT | ||
} from '$env/static/public'; | ||
</script> | ||
|
||
<SvelteGravityForm formId={1} /> | ||
<SvelteGravityForm | ||
formId={1} | ||
backendUrl={PUBLIC_GF_API_URL} | ||
consumerKey={PUBLIC_GF_CONSUMER_KEY} | ||
consumerSecret={PUBLIC_GF_CONSUMER_SECRECT} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.