switch to rails
This commit is contained in:
parent
83bc981152
commit
68c64808c5
71 changed files with 0 additions and 21335 deletions
|
@ -1,65 +0,0 @@
|
|||
|
||||
import { prisma } from "~/db.server";
|
||||
import type {User} from "@prisma/client";
|
||||
import { AccessTokenResponse, DiscordUser } from "~/discord/index";
|
||||
export type {User, Border} from "@prisma/client";
|
||||
|
||||
export async function getUserByDiscordId(discord_id: User["discord_id"]) {
|
||||
return prisma.user.findUnique({ where: { discord_id: discord_id || undefined }});
|
||||
}
|
||||
|
||||
export async function getUserById(id: User["id"]) {
|
||||
return prisma.user.findUnique({ where: { id } });
|
||||
}
|
||||
|
||||
/// SHOULD ONLY BE USED WITH A CORRESPONDING OAUTH TOKEN
|
||||
export async function createUser(discord_id: User["discord_id"]) {
|
||||
|
||||
return prisma.user.create({
|
||||
data: {
|
||||
discord_id
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createDiscordLogin(discord_id: DiscordUser["id"], token_response: AccessTokenResponse) {
|
||||
return prisma.discordUser.create({
|
||||
data: {
|
||||
discord_id,
|
||||
access_token: token_response.access_token,
|
||||
refresh_token: token_response.refresh_token,
|
||||
expires: new Date(token_response.expires)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export type SessionInformation = {
|
||||
bearer_token: string,
|
||||
refresh_token: string
|
||||
};
|
||||
|
||||
export async function discordIdentify(bearer_token: string, refresh_token: string): Promise<DiscordUser | SessionInformation | undefined> {
|
||||
let user_info = await (
|
||||
await fetch("https://discord.com/api/users/@me", {
|
||||
headers: {
|
||||
Authorization: `Bearer ${bearer_token}`
|
||||
}
|
||||
})
|
||||
).json();
|
||||
|
||||
if (!user_info["id"]) {
|
||||
const form_data = new FormData();
|
||||
form_data.append("client_id", process.env.DISCORD_CLIENT_ID || "");
|
||||
form_data.append("client_secret", process.env.DISCORD_CLIENT_SECRET || "");
|
||||
form_data.append("grant_type", "refresh_token");
|
||||
form_data.append("refresh_token", refresh_token);
|
||||
let refresh_info = await (await fetch("https://discord.com/api/oauth2/token", {
|
||||
method: "POST",
|
||||
body: form_data
|
||||
})).json();
|
||||
|
||||
return refresh_info;
|
||||
}
|
||||
|
||||
return user_info;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue