how to get access token to access discord api

currently i got an issued with accessToken with auth discord can you help me ?
15 Replies
YUU 🦖❤
YUU 🦖❤OP2mo ago
hello brother @Ping !
Ping
Ping2mo ago
Hey, do you know what specifically is going wrong? @𝐊𝐘𝐔𝐔 🐣💞
YUU 🦖❤
YUU 🦖❤OP2mo ago
i don't know sorry i tried to config with 3h but still doesn't work now i got rollback to use with directly auth
Ping
Ping2mo ago
Do you have an error message or something that indicates to you it's not working?
YUU 🦖❤
YUU 🦖❤OP2mo ago
i want to know the way to get accessToken from the user table
Ping
Ping2mo ago
const tokens = await authClient.getAccessToken({
providerId: account.provider,
});
const tokens = await authClient.getAccessToken({
providerId: account.provider,
});
YUU 🦖❤
YUU 🦖❤OP2mo ago
the account.provider we call from db or session ?
Ping
Ping2mo ago
If you're on the front-end, you can call authClient.listAccounts() and that will return an array containing all of the accounts which represent the user. From there you'll need to get the one specific for discord and then call the getAccessToken endpoint with that provider id I recommend console.logging the responses just to test and know what it returns
YUU 🦖❤
YUU 🦖❤OP2mo ago
import { authClient } from "@/lib/auth-client";
import { createApi } from "@reduxjs/toolkit/query/react";
import axios from "axios";

const axiosBaseQuery =
() =>
async ({ url, method, body, params }) => {
const { data: session } = await authClient.getSession();

const getListAccount = await authClient.listAccounts();

console.log(getListAccount);

const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${account.accessToken}`,
};

try {
const result = await axios({
headers,
url,
method,
data: body,
params,
});
return { data: result.data };
} catch (axiosError) {
const err = axiosError;
return {
error: {
status: err.response?.status,
message: err.response?.data?.message || err.message,
data: err.response?.data || err.message,
},
};
}
};

export const emptySplitApi = createApi({
reducerPath: "api",
baseQuery: axiosBaseQuery({ baseUrl: "" }),
tagTypes: dynamicTagTypes,
refetchOnFocus: true,
refetchOnReconnect: true,
keepUnusedDataFor: 5,
refetchOnMountOrArgChange: 5,
endpoints: () => ({}),
});
import { authClient } from "@/lib/auth-client";
import { createApi } from "@reduxjs/toolkit/query/react";
import axios from "axios";

const axiosBaseQuery =
() =>
async ({ url, method, body, params }) => {
const { data: session } = await authClient.getSession();

const getListAccount = await authClient.listAccounts();

console.log(getListAccount);

const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${account.accessToken}`,
};

try {
const result = await axios({
headers,
url,
method,
data: body,
params,
});
return { data: result.data };
} catch (axiosError) {
const err = axiosError;
return {
error: {
status: err.response?.status,
message: err.response?.data?.message || err.message,
data: err.response?.data || err.message,
},
};
}
};

export const emptySplitApi = createApi({
reducerPath: "api",
baseQuery: axiosBaseQuery({ baseUrl: "" }),
tagTypes: dynamicTagTypes,
refetchOnFocus: true,
refetchOnReconnect: true,
keepUnusedDataFor: 5,
refetchOnMountOrArgChange: 5,
endpoints: () => ({}),
});
i still don't understand, if i have many account so how can i get the specifically user account ?
YUU 🦖❤
YUU 🦖❤OP2mo ago
No description
YUU 🦖❤
YUU 🦖❤OP2mo ago
async ({ url, method, body, params }) => {
const { data: getListAccount } = await authClient.listAccounts();

const { data: account } = await authClient.getAccessToken({
providerId: getListAccount[0].provider,
});

const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${account.accessToken}`,
};

try {
const result = await axios({
headers,
url,
method,
data: body,
params,
});
return { data: result.data };
} catch (axiosError) {
const err = axiosError;
return {
error: {
status: err.response?.status,
message: err.response?.data?.message || err.message,
data: err.response?.data || err.message,
},
};
}
};
async ({ url, method, body, params }) => {
const { data: getListAccount } = await authClient.listAccounts();

const { data: account } = await authClient.getAccessToken({
providerId: getListAccount[0].provider,
});

const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${account.accessToken}`,
};

try {
const result = await axios({
headers,
url,
method,
data: body,
params,
});
return { data: result.data };
} catch (axiosError) {
const err = axiosError;
return {
error: {
status: err.response?.status,
message: err.response?.data?.message || err.message,
data: err.response?.data || err.message,
},
};
}
};
it's work Thanks brother @Ping ! do you have any recommend about it ?
YUU 🦖❤
YUU 🦖❤OP2mo ago
No description
YUU 🦖❤
YUU 🦖❤OP2mo ago
how can we handle page when user click cancel with Discord Auth ?
nikatune
nikatune2mo ago
export const auth = betterAuth({
onAPIError: {
errorURL: "/error"
}
});
export const auth = betterAuth({
onAPIError: {
errorURL: "/error"
}
});
YUU 🦖❤
YUU 🦖❤OP2mo ago
Thanks

Did you find this page helpful?