Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ interface ChatRouteRequest {
}

export async function POST(req: Request) {
const authorized = await checkAuthorizedStatus();
const authorized = await checkAuthorizedStatus(["*", "ai"]);
if (!authorized) {
return new NextResponse("Unauthorized", { status: 401 });
}
Expand Down
19 changes: 14 additions & 5 deletions lib/auth.rsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,26 @@ export async function redirectIfAuthenticated(): Promise<void> {
redirect("/");
}

export async function checkAuthorizedStatus(): Promise<boolean> {
export async function checkAuthorizedStatus(requiredScopes?: string[]): Promise<boolean> {
const token = await getAuthToken();
if (!token) {
return false;
}

const loggedIn = await getAuthStatus(token)
.then(result => result.loggedIn)
.catch(() => false);
const authStatus = await getAuthStatus(token);

if (!authStatus.loggedIn || !authStatus.introspectResult?.active) {
return false;
}

// check if the token has the required scope
if (requiredScopes) {
for (const scope of requiredScopes) {
if (authStatus.introspectResult?.scope.includes(scope)) {
return true;
}
}

if (!loggedIn) {
return false;
}

Expand Down