This commit is contained in:
Skip R. 2023-04-26 20:32:52 -07:00
parent 2e4a05ded3
commit 8d428d90a8
7 changed files with 59 additions and 9 deletions

View File

@ -0,0 +1,8 @@
/*
Warnings:
- A unique constraint covering the columns `[username]` on the table `User` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateIndex
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");

View File

@ -18,7 +18,7 @@ model AuthTicket {
model User { model User {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
username String? username String? @unique
authTicket AuthTicket? authTicket AuthTicket?
discordAuth DiscordAuth? discordAuth DiscordAuth?

View File

@ -226,6 +226,14 @@ export default function AboutMe({ info }: { info: UserInfo }) {
)} )}
</Formik> </Formik>
</PrettyForm> </PrettyForm>
<hr className={styles.divider} />
<h2 className={styles.header}>Connections</h2>
<div>
<p>discord: {info.discordId}</p>
<p>github: {info.githubId}</p>
</div>
<hr className={styles.divider} /> <hr className={styles.divider} />
<input <input
type="button" type="button"

View File

@ -11,6 +11,7 @@ import prisma from "@/prisma";
import { v4 } from "uuid"; import { v4 } from "uuid";
import * as ldap from "@/ldap"; import * as ldap from "@/ldap";
import { getLogger } from "@/logger"; import { getLogger } from "@/logger";
import { AuthState, getAuthState, getUser } from "@/auth";
export async function GET(request: Request) { export async function GET(request: Request) {
const logger = getLogger("/oauth/discord/redirect"); const logger = getLogger("/oauth/discord/redirect");
@ -66,6 +67,13 @@ export async function GET(request: Request) {
return new Response("not permitted to register account", { status: 403 }); return new Response("not permitted to register account", { status: 403 });
} }
let userId = null;
const authState = await getAuthState();
if (authState === AuthState.LoggedIn) {
const currentUser = await getUser();
userId = currentUser?.id;
}
// - create the discord auth data in prisma, which will make the user if it doesn't exist // - create the discord auth data in prisma, which will make the user if it doesn't exist
// - get the user from the discord auth data // - get the user from the discord auth data
// - either create a new auth ticket or invalidate the old one // - either create a new auth ticket or invalidate the old one
@ -80,11 +88,10 @@ export async function GET(request: Request) {
accessToken: tokenBody.access_token, accessToken: tokenBody.access_token,
refreshToken: tokenBody.refresh_token, refreshToken: tokenBody.refresh_token,
expiresAt: new Date(Date.now() + tokenBody.expires_in * 1000), expiresAt: new Date(Date.now() + tokenBody.expires_in * 1000),
user: { user:
create: { userId != null
username: null ? { connect: { id: userId } }
} : { create: { username: null } }
}
}, },
update: { update: {
accessToken: tokenBody.access_token, accessToken: tokenBody.access_token,

View File

@ -9,6 +9,7 @@ import {
import prisma from "@/prisma"; import prisma from "@/prisma";
import * as ldap from "@/ldap"; import * as ldap from "@/ldap";
import { v4 } from "uuid"; import { v4 } from "uuid";
import { AuthState, getAuthState, getUser } from "@/auth";
const logger = getLogger("/oauth/github/redirect"); const logger = getLogger("/oauth/github/redirect");
@ -61,12 +62,22 @@ export async function GET(request: Request) {
return new Response("not permitted to register account", { status: 403 }); return new Response("not permitted to register account", { status: 403 });
} }
let userId = null;
const authState = await getAuthState();
if (authState === AuthState.LoggedIn) {
const currentUser = await getUser();
userId = currentUser?.id;
}
const githubAuth = await prisma.gitHubAuth.upsert({ const githubAuth = await prisma.gitHubAuth.upsert({
where: { id: githubUser.id }, where: { id: githubUser.id },
create: { create: {
id: githubUser.id, id: githubUser.id,
accessToken, accessToken,
user: { create: { username: null } } user:
userId != null
? { connect: { id: userId } }
: { create: { username: null } }
}, },
update: { accessToken } update: { accessToken }
}); });

View File

@ -13,8 +13,8 @@
} }
.avatarChanger button svg { .avatarChanger button svg {
width: 1.5em; width: 1.2em;
height: 1.5em; height: 1.2em;
margin-right: 0.5em; margin-right: 0.5em;
} }

View File

@ -4,6 +4,7 @@ import { gql } from "./__generated__";
import { BerWriter } from "asn1"; import { BerWriter } from "asn1";
import { User } from "@prisma/client"; import { User } from "@prisma/client";
import { ensureJpg } from "@/image"; import { ensureJpg } from "@/image";
import prisma from "./prisma";
export type LLDAPAuthResponse = { export type LLDAPAuthResponse = {
token: string; token: string;
@ -19,6 +20,9 @@ export type UserInfo = {
displayName: string; displayName: string;
email: string; email: string;
avatar?: string; avatar?: string;
discordId?: string;
githubId?: string;
}; };
async function getLdapClient() { async function getLdapClient() {
@ -202,10 +206,22 @@ export async function getUserInfo(user: User) {
? `data:image/jpeg;base64,${mutationAvatar}` ? `data:image/jpeg;base64,${mutationAvatar}`
: undefined; : undefined;
const dbUser = await prisma.user.findFirst({
where: {
username: user.username
},
include: {
discordAuth: true,
githubAuth: true
}
});
const userInfo: UserInfo = { const userInfo: UserInfo = {
username: mutation.data.user.id, username: mutation.data.user.id,
displayName: mutation.data.user.displayName, displayName: mutation.data.user.displayName,
email: mutation.data.user.email, email: mutation.data.user.email,
discordId: dbUser?.discordAuth?.id,
githubId: dbUser?.githubAuth?.id?.toString(),
avatar avatar
}; };