-
Notifications
You must be signed in to change notification settings - Fork 2
🛂 server: add better auth is an authentication framework #881
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
Open
nfmelendez
wants to merge
3
commits into
main
Choose a base branch
from
better-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "@exactly/server": patch | ||
| --- | ||
|
|
||
| ➕ install better auth | ||
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "@exactly/server": patch | ||
| --- | ||
|
|
||
| 🛂 setup better auth |
This file contains hidden or 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 hidden or 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,143 @@ | ||
| --- | ||
| title: Organizations, authentication and authorization | ||
| sidebar: | ||
| label: Organizations and authentication | ||
| order: 10 | ||
| --- | ||
|
|
||
| Creating organizations is permission-less. Any user can create an organization and will be the owner. | ||
| Then the owner can add members with admin role and those admins will be able to add more members with different roles. | ||
|
|
||
| Better auth client and viem are the recommended libraries to use for authentication and signing using SIWE. | ||
|
|
||
| ## SIWE Authentication | ||
|
|
||
| Example code to authenticate using SIWE, it will create the user if doesn't exist. | ||
| Note: Check viem account to use a private key instead of a mnemonic. | ||
|
|
||
| ```typescript | ||
| import { createAuthClient } from "better-auth/client"; | ||
| import { siweClient, organizationClient } from "better-auth/client/plugins"; | ||
| import { mnemonicToAccount } from "viem/accounts"; | ||
| import { optimismSepolia } from "viem/chains"; | ||
| import { createSiweMessage } from "viem/siwe"; | ||
|
|
||
| const chainId = optimismSepolia.id; | ||
|
|
||
| const authClient = createAuthClient({ | ||
| baseURL: "http://localhost:3000", | ||
| plugins: [siweClient(), organizationClient()], | ||
| }); | ||
|
|
||
| const owner = mnemonicToAccount("test test test test test test test test test test test test"); | ||
nfmelendez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| authClient.siwe | ||
| .nonce({ | ||
| walletAddress: owner.address, | ||
| chainId, | ||
| }) | ||
| .then(async ({ data: nonceResult }) => { | ||
| //can be any statement | ||
| const statement = "i accept exa terms and conditions"; | ||
| const nonce = nonceResult?.nonce ?? ""; | ||
| const message = createSiweMessage({ | ||
| statement, | ||
| resources: ["https://exactly.github.io/exa"], | ||
| nonce, | ||
| uri: "https://localhost", | ||
nfmelendez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| address: owner.address, | ||
| chainId, | ||
| scheme: "https", | ||
| version: "1", | ||
| domain: "localhost", | ||
| }); | ||
| const signature = await owner.signMessage({ message }); | ||
|
|
||
| await authClient.siwe.verify( | ||
| { | ||
| message, | ||
| signature, | ||
| walletAddress: owner.address, | ||
| chainId, | ||
| }, | ||
| { | ||
| onSuccess: async (context) => { | ||
| // authentication successful, session cookie is now set | ||
| }, | ||
nfmelendez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| onError: (context) => { | ||
| console.log("authorization error", context); | ||
| }, | ||
| }, | ||
| ); | ||
| }).catch((error: unknown) => { | ||
| console.error("nonce error", error); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Creating an organization | ||
|
|
||
| owner account will be the owner of the created organization | ||
|
|
||
| ```typescript | ||
| const chainId = optimismSepolia.id; | ||
|
|
||
| const authClient = createAuthClient({ | ||
| baseURL: "http://localhost:3000", | ||
| plugins: [siweClient(), organizationClient()], | ||
| }); | ||
|
|
||
| const owner = mnemonicToAccount("test test test test test test test test test test test siwe"); | ||
|
|
||
| authClient.siwe | ||
| .nonce({ | ||
| walletAddress: owner.address, | ||
| chainId, | ||
| }) | ||
| .then(async ({ data: nonceResult }) => { | ||
| const statement = `i accept exa terms and conditions`; | ||
| const nonce = nonceResult?.nonce ?? ""; | ||
| const message = createSiweMessage({ | ||
| statement, | ||
| resources: ["https://exactly.github.io/exa"], | ||
| nonce, | ||
| uri: `https://localhost`, | ||
| address: owner.address, | ||
| chainId, | ||
| scheme: "https", | ||
| version: "1", | ||
| domain: "localhost", | ||
| }); | ||
| const signature = await owner.signMessage({ message }); | ||
|
|
||
| await authClient.siwe.verify( | ||
| { | ||
| message, | ||
| signature, | ||
| walletAddress: owner.address, | ||
| chainId, | ||
| }, | ||
| { | ||
| onSuccess: async (context) => { | ||
| const headers = new Headers(); | ||
| headers.set("cookie", context.response.headers.get("set-cookie") ?? ""); | ||
| const createOrganizationResult = await authClient.organization.create({ | ||
| fetchOptions: { headers }, | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| name: "Uphold", | ||
| slug: "uphold", | ||
| keepCurrentActiveOrganization: false, | ||
| }); | ||
| if (createOrganizationResult.data) { | ||
| console.log(`organization created id: ${createOrganizationResult.data.id}`); | ||
| } else { | ||
| console.error("Failed to create organization error:", createOrganizationResult.error); | ||
| } | ||
| }, | ||
| onError: (context) => { | ||
| console.log("authorization error", context); | ||
| }, | ||
| }, | ||
| ); | ||
| }).catch((error: unknown) => { | ||
| console.error("nonce error", error); | ||
| }); | ||
nfmelendez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.