completed oauth implementation on hub and dispatch
This commit is contained in:
71
apps/dispatch/app/api/auth/[...nextauth]/auth.ts
Normal file
71
apps/dispatch/app/api/auth/[...nextauth]/auth.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import {
|
||||
AuthOptions,
|
||||
getServerSession as getNextAuthServerSession,
|
||||
} from 'next-auth';
|
||||
import { PrismaAdapter } from '@next-auth/prisma-adapter';
|
||||
import Credentials from 'next-auth/providers/credentials';
|
||||
import { PrismaClient } from '@repo/db';
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export const options: AuthOptions = {
|
||||
providers: [
|
||||
Credentials({
|
||||
credentials: {
|
||||
code: { label: 'code', type: 'code' },
|
||||
},
|
||||
async authorize(credentials, req) {
|
||||
try {
|
||||
if (!credentials) throw new Error('No credentials provided');
|
||||
const code = await prisma.oAuthToken.findFirstOrThrow({
|
||||
where: {
|
||||
accessToken: credentials.code,
|
||||
},
|
||||
});
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
id: code.userId,
|
||||
},
|
||||
});
|
||||
console.log(code, user);
|
||||
|
||||
if (!user) return null;
|
||||
|
||||
return user;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
}),
|
||||
],
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
session: {
|
||||
strategy: 'jwt',
|
||||
maxAge: 30 * 24 * 60 * 60,
|
||||
},
|
||||
|
||||
adapter: PrismaAdapter(prisma),
|
||||
callbacks: {
|
||||
jwt: async ({ token, user }) => {
|
||||
if (user && 'firstname' in user) {
|
||||
return {
|
||||
...token,
|
||||
...user,
|
||||
};
|
||||
}
|
||||
return token;
|
||||
},
|
||||
session: async ({ session, user, token }) => {
|
||||
return {
|
||||
...session,
|
||||
user: token,
|
||||
};
|
||||
},
|
||||
},
|
||||
pages: {
|
||||
signIn: '/login',
|
||||
signOut: '/logout',
|
||||
error: '/authError',
|
||||
},
|
||||
} satisfies AuthOptions;
|
||||
|
||||
export const getServerSession = async () => getNextAuthServerSession(options);
|
||||
6
apps/dispatch/app/api/auth/[...nextauth]/route.ts
Normal file
6
apps/dispatch/app/api/auth/[...nextauth]/route.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import NextAuth from 'next-auth';
|
||||
import { options } from './auth';
|
||||
|
||||
const handler = NextAuth(options);
|
||||
|
||||
export { handler as GET, handler as POST };
|
||||
Reference in New Issue
Block a user