Add back vc auth temporarily to unblock things
This commit is contained in:
parent
07c2e7d36a
commit
177b2cc210
20 changed files with 350 additions and 40 deletions
|
|
@ -1,18 +0,0 @@
|
|||
import NextAuth, { NextAuthOptions } from "next-auth";
|
||||
import { PrismaAdapter } from "@next-auth/prisma-adapter";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import GoogleProvider from "next-auth/providers/google";
|
||||
|
||||
export const authOptions: NextAuthOptions = {
|
||||
adapter: PrismaAdapter(prisma),
|
||||
providers: [
|
||||
GoogleProvider({
|
||||
clientId: process.env.GOOGLE_CLIENT_ID as string,
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
|
||||
}),
|
||||
],
|
||||
};
|
||||
|
||||
const handler = NextAuth(authOptions);
|
||||
|
||||
export { handler as GET, handler as POST };
|
||||
62
app/api/auth/callback/route.ts
Normal file
62
app/api/auth/callback/route.ts
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { serialize } from 'cookie';
|
||||
import { createSession } from '@/lib/session/create';
|
||||
import { encryptJWECookie } from '@/lib/cookies/encrypt-jwe';
|
||||
import ms from 'ms';
|
||||
import { userSessionCookieName } from '@/lib/session/constants';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
const sharedTokenSecret = process.env.SHARE_TOKEN_ENDPOINT_SECRET;
|
||||
const cookieTTL = ms('1y');
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const { origin, searchParams } = req.nextUrl;
|
||||
const shareableToken = searchParams.get('shareableToken');
|
||||
|
||||
if (!sharedTokenSecret || !shareableToken) {
|
||||
// TODO: Make an error page
|
||||
return NextResponse.json({ error: 'Invalid request' }, { status: 500 });
|
||||
}
|
||||
|
||||
const query = new URLSearchParams({
|
||||
sharedTokenSecret,
|
||||
});
|
||||
|
||||
const response = await fetch(
|
||||
`https://api.vercel.com/user/tokens/shared/${shareableToken}?${query.toString()}`
|
||||
);
|
||||
|
||||
const { token } = (await response.json()) as { token: string };
|
||||
|
||||
const session = await createSession(token);
|
||||
|
||||
const cookieValue = await encryptJWECookie(session, '1y');
|
||||
const next = req.cookies.get('auth-next')?.value;
|
||||
|
||||
return NextResponse.redirect(new URL(next ?? '/', origin), {
|
||||
headers: [
|
||||
[
|
||||
'set-cookie',
|
||||
serialize(userSessionCookieName, cookieValue, {
|
||||
path: '/',
|
||||
maxAge: cookieTTL / 1000,
|
||||
expires: new Date(Date.now() + cookieTTL),
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV !== 'development',
|
||||
}),
|
||||
],
|
||||
// There's currently an issue with adding more than one cookie to a response: https://github.com/vercel/next.js/issues/46579
|
||||
// the `auth-next` cookie will expire in 10 min anyway
|
||||
// [
|
||||
// 'set-cookie',
|
||||
// serialize('auth-next', '', {
|
||||
// path: '/api/auth/callback',
|
||||
// maxAge: 0,
|
||||
// expires: new Date(),
|
||||
// httpOnly: true,
|
||||
// })
|
||||
// ]
|
||||
],
|
||||
});
|
||||
}
|
||||
29
app/api/auth/login/route.ts
Normal file
29
app/api/auth/login/route.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { serialize } from 'cookie';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const { origin } = req.nextUrl;
|
||||
|
||||
const next = req.nextUrl.searchParams.get('next');
|
||||
|
||||
const params = new URLSearchParams({
|
||||
redirectUrl: new URL('/api/auth/callback', origin).toString(),
|
||||
});
|
||||
|
||||
return NextResponse.redirect(
|
||||
`https://vercel.com/api/vercel-auth?${params.toString()}`,
|
||||
{
|
||||
headers: next
|
||||
? {
|
||||
'Set-Cookie': serialize('auth-next', next, {
|
||||
path: '/api/auth/callback',
|
||||
maxAge: 60 * 60 * 10,
|
||||
httpOnly: true,
|
||||
}),
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
);
|
||||
}
|
||||
25
app/api/auth/logout/route.ts
Normal file
25
app/api/auth/logout/route.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { userSessionCookieName } from '@/lib/session/constants';
|
||||
import { serialize } from 'cookie';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const { origin, searchParams } = req.nextUrl;
|
||||
const next = searchParams.get('next');
|
||||
|
||||
return NextResponse.redirect(new URL(next ?? '/', origin), {
|
||||
headers: [
|
||||
[
|
||||
'set-cookie',
|
||||
serialize(userSessionCookieName, '', {
|
||||
path: '/',
|
||||
maxAge: 0,
|
||||
expires: new Date(),
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV !== 'development',
|
||||
}),
|
||||
],
|
||||
],
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue