Add some apis needed for successful login
This commit is contained in:
parent
588eea3a03
commit
59a399b16e
21
src/app/api/users/getUserByAppleId/route.ts
Normal file
21
src/app/api/users/getUserByAppleId/route.ts
Normal file
@ -0,0 +1,21 @@
|
||||
"use server";
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { getUserByAppleId } from "~/server/functions";
|
||||
import { middleware } from "~/middleware";
|
||||
|
||||
export const GET = async (request: NextRequest) => {
|
||||
const middlewareResponse = await middleware(request);
|
||||
if (middlewareResponse) return middlewareResponse;
|
||||
try {
|
||||
const appleId = request.nextUrl.searchParams.get("appleId");
|
||||
if (!appleId)
|
||||
return NextResponse.json({ message: "Missing Apple ID" }, { status: 400 });
|
||||
const user = await getUserByAppleId(appleId);
|
||||
if (user) return NextResponse.json(user);
|
||||
else return NextResponse.json({ message: "User not found" }, { status: 404 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ message: "Error" }, { status: 500 });
|
||||
}
|
||||
};
|
38
src/app/api/users/updatePushTokenByAppleId/route.ts
Normal file
38
src/app/api/users/updatePushTokenByAppleId/route.ts
Normal file
@ -0,0 +1,38 @@
|
||||
"use server";
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { updateUserPushTokenByAppleId } from "~/server/functions";
|
||||
import { middleware } from "~/middleware";
|
||||
|
||||
type UpdatePushTokenRequest = {
|
||||
appleId: string;
|
||||
pushToken: string;
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const middlewareResponse = await middleware(request);
|
||||
if (middlewareResponse) return middlewareResponse;
|
||||
try {
|
||||
const { appleId, pushToken } = await request.json() as UpdatePushTokenRequest;
|
||||
console.log("Received request:", { appleId, pushToken });
|
||||
|
||||
if (!appleId || !pushToken)
|
||||
return NextResponse.json({ message: "Missing required fields" }, { status: 400 });
|
||||
|
||||
console.log("Updating push token for user:", appleId);
|
||||
const result = await updateUserPushTokenByAppleId(appleId, pushToken);
|
||||
|
||||
if (result.success) {
|
||||
console.log("Push token updated successfully");
|
||||
return NextResponse.json({ message: "Push token updated successfully" });
|
||||
} else {
|
||||
throw new Error("Failed to update push token");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error in updatePushToken:", error);
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ message: "Error updating push token" }, { status: 500 });
|
||||
}
|
||||
}
|
@ -25,8 +25,8 @@ export default function CreateUserPage() {
|
||||
appleId,
|
||||
appleEmail,
|
||||
fullName,
|
||||
pfpURL,
|
||||
pushToken
|
||||
pushToken,
|
||||
pfpURL
|
||||
})
|
||||
});
|
||||
const data = await response.json() as {message: string}
|
||||
@ -76,16 +76,6 @@ export default function CreateUserPage() {
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="pfpURL" className="block">Pfp URL:</label>
|
||||
<input
|
||||
type="text"
|
||||
id="pfpURL"
|
||||
value={pfpURL}
|
||||
onChange={(e) => setPfpURL(e.target.value)}
|
||||
className="border p-2 w-full bg-black"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="pushToken" className="block">Push Token:</label>
|
||||
<input
|
||||
@ -96,6 +86,16 @@ export default function CreateUserPage() {
|
||||
className="border p-2 w-full bg-black"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="pfpURL" className="block">Pfp URL:</label>
|
||||
<input
|
||||
type="text"
|
||||
id="pfpURL"
|
||||
value={pfpURL}
|
||||
onChange={(e) => setPfpURL(e.target.value)}
|
||||
className="border p-2 w-full bg-black"
|
||||
/>
|
||||
</div>
|
||||
<button type='submit' className='bg-blue-500 p-2 rounded'>
|
||||
Create User
|
||||
</button>
|
||||
|
@ -2,26 +2,15 @@ import 'server-only';
|
||||
import { db } from '~/server/db';
|
||||
import * as schema from '~/server/db/schema';
|
||||
import { eq, and, or, sql } from 'drizzle-orm';
|
||||
import jwt from 'jsonwebtoken';
|
||||
//import { pgEnum } from 'drizzle-orm/pg-core';
|
||||
//import { inArray } from 'drizzle-orm/sql';
|
||||
|
||||
// --- Helper Functions --- //
|
||||
|
||||
/**
|
||||
* Ensure the user exists based on their userId.
|
||||
* Throws an error if the user doesn't exist.
|
||||
*/
|
||||
export const ensureUserExists = async (userId: number) => {
|
||||
try {
|
||||
const user = await db.select().from(schema.users)
|
||||
.where(eq(schema.users.id, userId));
|
||||
|
||||
if (user.length === 0) {
|
||||
throw new Error('User does not exist');
|
||||
}
|
||||
|
||||
return user[0]; // Return user for further use if needed
|
||||
return (user.length > 0) ? user[0] : null;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(`Error checking user: ${error.message}`);
|
||||
@ -119,13 +108,26 @@ export const getUserById = async (userId: number) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getUserByAppleId = async (appleId: string) => {
|
||||
try {
|
||||
const user = await db.select().from(schema.users)
|
||||
.where(eq(schema.users.appleId, appleId));
|
||||
|
||||
return (user.length > 0) ? user[0] : null;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(`Failed to fetch user by appleId: ${error.message}`);
|
||||
} else {
|
||||
throw new Error("Unknown error occurred while fetching user by appleId");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const getUserByAppleEmail = async (appleEmail: string) => {
|
||||
try {
|
||||
const user = await db.select().from(schema.users)
|
||||
.where(eq(schema.users.appleEmail, appleEmail));
|
||||
|
||||
if (user.length === 0) throw new Error('User not found');
|
||||
return user[0];
|
||||
return (user.length > 0) ? user[0] : null;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(`Failed to fetch user by username: ${error.message}`);
|
||||
@ -137,7 +139,7 @@ export const getUserByAppleEmail = async (appleEmail: string) => {
|
||||
|
||||
export const createUser = async (
|
||||
appleId: string, appleEmail: string,
|
||||
fullName: string, pfpURL = "", pushToken = ""
|
||||
fullName: string, pushToken: string, pfpURL = ""
|
||||
) => {
|
||||
try {
|
||||
if (!appleId || !appleEmail || !fullName || !pushToken) {
|
||||
@ -198,6 +200,22 @@ export const updateUserPushToken = async (userId: number, pushToken: string) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const updateUserPushTokenByAppleId = async (appleId: string, pushToken: string) => {
|
||||
try {
|
||||
await db.update(schema.users)
|
||||
.set({ pushToken })
|
||||
.where(eq(schema.users.appleId, appleId));
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(`Failed to update profile: ${error.message}`);
|
||||
} else {
|
||||
throw new Error("Unknown error occurred while updating profile");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// --- Relationship Management Functions --- //
|
||||
|
||||
export const createRelationshipRequest = async (requestorId: number, requestedId: number) => {
|
||||
|
Loading…
Reference in New Issue
Block a user