Access Token
To perform actions or queries using Bettermode JavaScript client, you need to first generate an access token.
You can generate an access token using one of the following methods:
App Access Token
When you create a new client witht he method described here, the Global Client generate the access token and there is no need for an extra steps.
import { GlobalClient, TribeClient } from '@tribeplatform/gql-client'
const globalClient = new GlobalClient({
clientId: '{CLIENT_ID}',
clientSecret: '{CLIENT_SECRET}',
graphqlUrl: 'https://api.bettermode.com/global',
onError: (errors, client, error) => {
console.error(error, errors, client)
},
})
const client = bettermodeClient.getTribeClient({ networkId: '{networkId}' })
const accessToken = client.accessToken
Member Token
In some cases, you may want to generate an access token based on your members username/email and password. An example is when you want to use Bettermode member management as your main member management system and you want to create your own UI and login page that is connected to Bettermode platform.
To do so, we need to first generate a guest access token using getTokens
method and then log the member in using the auth.login
method as followed:
import { TribeClient } from "@tribeplatform/gql-client"
const client = new TribeClient({
graphqlUrl: "https://api.bettermode.com",
})
const getAccessToken = async (usernameOrEmail, password) => {
const guestTokens = await client.getTokens(
{ networkDomain: "{yourcommunityaddress.bettermode.io}" },
"basic"
)
client.setToken(guestTokens.accessToken)
const { accessToken, refreshToken } = await client.mutation({
name: 'loginNetwork',
args: {
variables: {
input: {
usernameOrEmail: '',
password: '',
},
},
fields: 'basic',
},
})
console.log({ accessToken, refreshToken })
}
getAccessToken("{usernameOrEmail}", "{password}")
If your community is hosted on a region other than the US region (us-east-1), you should use a different GraphQL URL as stated here under the GraphQL Endpoint section.
You should replace {yourcommunityaddress.bettermode.io}
with your community subdomain or custom domain. {usernameOrEmail}
and {password}
should be replaced by member's username/email and password.