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 {
id Int @id @default(autoincrement())
username String?
username String? @unique
authTicket AuthTicket?
discordAuth DiscordAuth?

View file

@ -226,6 +226,14 @@ export default function AboutMe({ info }: { info: UserInfo }) {
)}
</Formik>
</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} />
<input
type="button"

View file

@ -11,6 +11,7 @@ import prisma from "@/prisma";
import { v4 } from "uuid";
import * as ldap from "@/ldap";
import { getLogger } from "@/logger";
import { AuthState, getAuthState, getUser } from "@/auth";
export async function GET(request: Request) {
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 });
}
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
// - get the user from the discord auth data
// - 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,
refreshToken: tokenBody.refresh_token,
expiresAt: new Date(Date.now() + tokenBody.expires_in * 1000),
user: {
create: {
username: null
}
}
user:
userId != null
? { connect: { id: userId } }
: { create: { username: null } }
},
update: {
accessToken: tokenBody.access_token,

View file

@ -9,6 +9,7 @@ import {
import prisma from "@/prisma";
import * as ldap from "@/ldap";
import { v4 } from "uuid";
import { AuthState, getAuthState, getUser } from "@/auth";
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 });
}
let userId = null;
const authState = await getAuthState();
if (authState === AuthState.LoggedIn) {
const currentUser = await getUser();
userId = currentUser?.id;
}
const githubAuth = await prisma.gitHubAuth.upsert({
where: { id: githubUser.id },
create: {
id: githubUser.id,
accessToken,
user: { create: { username: null } }
user:
userId != null
? { connect: { id: userId } }
: { create: { username: null } }
},
update: { accessToken }
});

View file

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

View file

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