Commit 2fc0136d authored by wycer's avatar wycer
Browse files

sustechId

parent f254ee11
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
/*
  Warnings:

  - You are about to drop the column `studentId` on the `User` table. All the data in the column will be lost.

*/
-- AlterTable
ALTER TABLE `User` DROP COLUMN `studentId`,
    MODIFY `sustechId` INTEGER NULL;
+40 −18
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import oauth2Client, { credentials } from '$lib/server/auth/sustech-cloud';
import { db } from '$lib/server/db';
import _ from 'lodash';
import { lucia } from '$lib/server/auth';
import type { LuciaUser } from '@prisma/client';

const sustechUserSchema = z.object({
	sub: z.string(),
@@ -40,8 +41,28 @@ export async function GET(event: RequestEvent): Promise<Response> {
		});
		const userData = sustechUserSchema.parse(await userResponse.json())

		const user = await db.user.findFirst({
			where: {
				sustechId: userData.sustech_id
			}
		})

		const luciaUser = await db.luciaUser.upsert({
		let luciaUser: LuciaUser
		if (!_.isNil(user)) {
			luciaUser = await db.luciaUser.upsert({
				where: { id: userData.sub },
				update: {},
				create: {
					id: userData.sub,
					user: {
						connect: {
							id: user.id
						}
					}
				}
			})
		} else {
			luciaUser = await db.luciaUser.upsert({
				where: { id: userData.sub },
				update: {},
				create: {
@@ -56,6 +77,7 @@ export async function GET(event: RequestEvent): Promise<Response> {
					}
				}
			})
		}

		const session = await lucia.createSession(luciaUser.id, {});
		const sessionCookie = lucia.createSessionCookie(session.id);