Removed next-auth and refactored authentication
This commit is contained in:
+4
-31
@@ -1,35 +1,8 @@
|
||||
'use server';
|
||||
|
||||
import { signIn, signOut } from '@/auth';
|
||||
import { AuthError } from 'next-auth';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
export async function authenticate(_prevState: string | undefined, formData: FormData): Promise<string> {
|
||||
try {
|
||||
const result = await signIn('credentials', {
|
||||
redirect: false,
|
||||
token: formData.get('token'),
|
||||
});
|
||||
|
||||
if (result?.error) {
|
||||
return 'Invalid token';
|
||||
}
|
||||
|
||||
redirect('/admin');
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
switch (error.type) {
|
||||
case 'CredentialsSignin':
|
||||
return 'Invalid token';
|
||||
default:
|
||||
return 'Something went wrong';
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function performLogout() {
|
||||
await signOut({ redirectTo: '/', redirect: true });
|
||||
redirect('/');
|
||||
export async function deleteSessionCookie() {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.delete('session');
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
'use server';
|
||||
|
||||
import { getToken } from 'next-auth/jwt';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
export async function validateAdminToken(token: string): Promise<boolean> {
|
||||
return token === process.env.ADMIN_TOKEN;
|
||||
}
|
||||
|
||||
export async function getAuthStatus() {
|
||||
const { auth } = await import('@/auth');
|
||||
const session = await auth();
|
||||
|
||||
return {
|
||||
isAuthenticated: !!session?.user,
|
||||
user: session?.user,
|
||||
};
|
||||
}
|
||||
|
||||
export async function isAuthenticated(request: NextRequest): Promise<boolean> {
|
||||
console.log('isAuthenticated');
|
||||
const token = await getToken({ req: request, secret: process.env.AUTH_SECRET });
|
||||
console.log('token', token);
|
||||
if (!token || (token.exp && Date.now() / 1000 > token.exp)) {
|
||||
console.log('false');
|
||||
return false;
|
||||
}
|
||||
console.log('true');
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { getIronSession, SessionOptions } from 'iron-session';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
export interface SessionData {
|
||||
isAuthenticated: boolean;
|
||||
}
|
||||
|
||||
export const sessionOptions: SessionOptions = {
|
||||
password: process.env.AUTH_SECRET!,
|
||||
cookieName: 'session',
|
||||
cookieOptions: {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
maxAge: 60 * 60 * 24 * 7,
|
||||
},
|
||||
};
|
||||
|
||||
export async function getSession() {
|
||||
const session = await getIronSession<SessionData>(await cookies(), sessionOptions);
|
||||
if (!session.isAuthenticated) {
|
||||
session.isAuthenticated = false;
|
||||
}
|
||||
return session;
|
||||
}
|
||||
Reference in New Issue
Block a user