-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4937aef
commit c0f6eec
Showing
21 changed files
with
802 additions
and
176 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,7 @@ | ||
API_PUBLIC_KEY="<Turnkey API Public Key (that starts with 02 or 03)>" | ||
API_PRIVATE_KEY="<Turnkey API Private Key>" | ||
NEXT_PUBLIC_ORGANIZATION_ID="<Turnkey organization ID>" | ||
NEXT_PUBLIC_BASE_URL="https://api.turnkey.com" | ||
# Can be changed to a localhost iframe if you're modifying the auth flow | ||
# For production, the URL should not be changed and point to the primary Turnkey domain. | ||
NEXT_PUBLIC_AUTH_IFRAME_URL="https://auth.turnkey.com" |
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 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
} |
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,35 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.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,55 @@ | ||
# Example: `email-auth` | ||
|
||
This example shows a complete email auth flow. It contains a NextJS app with: | ||
|
||
- a frontend application | ||
- a backend application | ||
|
||
The overall flow for email auth is outlined below: | ||
![Email auth flow diagram](./email_auth_steps.png) | ||
|
||
This example contains an example auth page as well as a stub API endpoint for "your business" (where the email is resolved into an organization ID). The creation of the hidden iframe is abstracted by our `@turnkey/iframe-stamper` package. For more information on email auth, [check out our documentation](https://docs.turnkey.com/features/email-auth). | ||
|
||
## Getting started | ||
|
||
### 1/ Cloning the example | ||
|
||
Make sure you have `node` installed locally; we recommend using Node v16+. | ||
|
||
```bash | ||
$ git clone https://github.com/tkhq/sdk | ||
$ cd sdk/ | ||
$ corepack enable # Install `pnpm` | ||
$ pnpm install -r # Install dependencies | ||
$ pnpm run build-all # Compile source code | ||
$ cd examples/email-auth/ | ||
``` | ||
|
||
### 2/ Setting up Turnkey | ||
|
||
The first step is to set up your Turnkey organization and account. By following the [Quickstart](https://docs.turnkey.com/getting-started/quickstart) guide, you should have: | ||
|
||
- A public/private API key pair for Turnkey | ||
- An organization ID | ||
|
||
Once you've gathered these values, add them to a new `.env.local` file. Notice that your API private key should be securely managed and **_never_** be committed to git. | ||
|
||
```bash | ||
$ cp .env.local.example .env.local | ||
``` | ||
|
||
Now open `.env.local` and add the missing environment variables: | ||
|
||
- `API_PUBLIC_KEY` | ||
- `API_PRIVATE_KEY` | ||
- `NEXT_PUBLIC_ORGANIZATION_ID` | ||
- `NEXT_PUBLIC_BASE_URL` (the `NEXT_PUBLIC` prefix makes the env variable accessible to the frontend app) | ||
- `NEXT_PUBLIC_AUTH_IFRAME_URL` | ||
|
||
### 3/ Running the app | ||
|
||
```bash | ||
$ pnpm run dev | ||
``` | ||
|
||
This command will run a NextJS app on port 3000. If you navigate to http://localhost:3000 in your browser, you can follow the prompts to start an email auth. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,4 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {}; | ||
|
||
module.exports = nextConfig; |
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,32 @@ | ||
{ | ||
"name": "@turnkey/example-email-auth", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start", | ||
"lint": "next lint", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@turnkey/http": "workspace:*", | ||
"@turnkey/api-key-stamper": "workspace:*", | ||
"@turnkey/iframe-stamper": "workspace:*", | ||
"@types/node": "20.3.1", | ||
"@types/react": "18.2.14", | ||
"@types/react-dom": "18.2.6", | ||
"axios": "^1.4.0", | ||
"encoding": "^0.1.13", | ||
"eslint": "8.43.0", | ||
"eslint-config-next": "13.4.7", | ||
"esm": "^3.2.25", | ||
"install": "^0.13.0", | ||
"next": "^14.1.4", | ||
"npm": "^9.7.2", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0", | ||
"react-hook-form": "^7.45.1", | ||
"typescript": "5.1.3" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added
BIN
+96.6 KB
examples/browser-api-key-stamper/public/fonts/inter/Inter-Regular.woff2
Binary file not shown.
Binary file added
BIN
+103 KB
examples/browser-api-key-stamper/public/fonts/inter/Inter-SemiBold.woff2
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,42 @@ | ||
"use client"; | ||
|
||
import { IframeStamper } from "@turnkey/iframe-stamper"; | ||
import { Dispatch, SetStateAction, useEffect, useState } from "react"; | ||
|
||
interface AuthProps { | ||
iframeUrl: string; | ||
turnkeyBaseUrl: string; | ||
setIframeStamper: Dispatch<SetStateAction<IframeStamper | null>>; | ||
} | ||
|
||
const TurnkeyIframeContainerId = "turnkey-iframe-container-id"; | ||
const TurnkeyIframeElementId = "turnkey-iframe-element-id"; | ||
|
||
export function Auth(props: AuthProps) { | ||
const [iframeStamper, setIframeStamper] = useState<IframeStamper | null>( | ||
null | ||
); | ||
|
||
useEffect(() => { | ||
if (!iframeStamper) { | ||
const iframeStamper = new IframeStamper({ | ||
iframeUrl: props.iframeUrl, | ||
iframeContainer: document.getElementById(TurnkeyIframeContainerId), | ||
iframeElementId: TurnkeyIframeElementId, | ||
}); | ||
iframeStamper.init().then(() => { | ||
setIframeStamper(iframeStamper); | ||
props.setIframeStamper(iframeStamper); | ||
}); | ||
} | ||
|
||
return () => { | ||
if (iframeStamper) { | ||
iframeStamper.clear(); | ||
setIframeStamper(null); | ||
} | ||
}; | ||
}, [props, iframeStamper, setIframeStamper]); | ||
|
||
return <div style={{ display: "none" }} id={TurnkeyIframeContainerId}></div>; | ||
} |
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,19 @@ | ||
import Document, { Html, Head, Main, NextScript } from "next/document"; | ||
|
||
class Example extends Document { | ||
render() { | ||
return ( | ||
<Html> | ||
<Head> | ||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" /> | ||
</Head> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
); | ||
} | ||
} | ||
|
||
export default Example; |
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,83 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { TurnkeyClient, createActivityPoller } from "@turnkey/http"; | ||
import { ApiKeyStamper } from "@turnkey/api-key-stamper"; | ||
|
||
type AuthRequest = { | ||
suborgID: string; | ||
email: string; | ||
targetPublicKey: string; | ||
invalidateExisting: boolean; | ||
}; | ||
|
||
/** | ||
* Returns the user ID and (newly created) api key ID (available in `EMAIL_AUTH` activity result) | ||
* as well as the organization ID | ||
*/ | ||
type AuthResponse = { | ||
userId: string; | ||
apiKeyId: string; | ||
organizationId: string; | ||
}; | ||
|
||
type ErrorMessage = { | ||
message: string; | ||
}; | ||
|
||
export default async function auth( | ||
req: NextApiRequest, | ||
res: NextApiResponse<AuthResponse | ErrorMessage> | ||
) { | ||
try { | ||
const request = req.body as AuthRequest; | ||
const turnkeyClient = new TurnkeyClient( | ||
{ baseUrl: process.env.NEXT_PUBLIC_BASE_URL! }, | ||
new ApiKeyStamper({ | ||
apiPublicKey: process.env.API_PUBLIC_KEY!, | ||
apiPrivateKey: process.env.API_PRIVATE_KEY!, | ||
}) | ||
); | ||
|
||
const activityPoller = createActivityPoller({ | ||
client: turnkeyClient, | ||
requestFn: turnkeyClient.emailAuth, | ||
}); | ||
|
||
const completedActivity = await activityPoller({ | ||
type: "ACTIVITY_TYPE_EMAIL_AUTH_V2", | ||
timestampMs: String(Date.now()), | ||
// This is simple in the case of a single organization. | ||
// If you use sub-organizations for each user, this needs to be replaced by the user's specific sub-organization. | ||
organizationId: | ||
request.suborgID || process.env.NEXT_PUBLIC_ORGANIZATION_ID!, | ||
parameters: { | ||
email: request.email, | ||
targetPublicKey: request.targetPublicKey, | ||
invalidateExisting: request.invalidateExisting, | ||
}, | ||
}); | ||
|
||
const userId = completedActivity.result.emailAuthResult?.userId; | ||
if (!userId) { | ||
throw new Error("Expected a non-null user ID!"); | ||
} | ||
|
||
const apiKeyId = completedActivity.result.emailAuthResult?.apiKeyId; | ||
if (!apiKeyId) { | ||
throw new Error("Expected a non-null API key ID!"); | ||
} | ||
|
||
res.status(200).json({ | ||
userId, | ||
apiKeyId, | ||
// This is simple in the case of a single organization | ||
// If you use sub-organizations for each user, this needs to be replaced by the user's specific sub-organization. | ||
organizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!, | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
|
||
res.status(500).json({ | ||
message: "Something went wrong.", | ||
}); | ||
} | ||
} |
Oops, something went wrong.