Its been a while

This commit is contained in:
Jason Jordan
2026-07-23 10:38:20 -04:00
parent 77f294c972
commit 8b3bb209b3
50 changed files with 4147 additions and 1452 deletions
+100
View File
@@ -0,0 +1,100 @@
// /app/actions/auth.ts
'use server'
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { SignJWT } from 'jose/jwt/sign';
const SECRET_KEY = new TextEncoder().encode(process.env.JWT_SECRET);
function roleMapping(role_number: number) {
const map = [
"member",
"employer",
"broker",
"provider",
"carrier",
"admin"
]
return map[role_number]
}
export async function loginUser(prevState: any, formData: FormData) {
const email = formData.get('email') as string
const password = formData.get('password') as string
// Simple validation check
if (!email || !password) {
throw new Error('Please fill in all fields.');
}
const response = await fetch(`${process.env.BE_API_SERVER_HOST}:${process.env.BE_API_SERVER_PORT}/api/v1/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
if (!response.ok) {
throw new Error('Invalid credentials');
}
const data = await response.json();
const role = roleMapping(data.keychain.role)
const sessionPayload = {
accessToken: data.token,
user: {
id: data.id,
name: data.name,
email: data.email,
keychain: data.keychain,
role: role,
role_id: data.keychain.role_id
}
};
const encryptedSession = await new SignJWT(sessionPayload)
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('2h')
.sign(SECRET_KEY);
const cookieStore = await cookies();
cookieStore.set('session', encryptedSession, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
});
const initUserResponse = await fetch('https://yourdomain.com', {
headers: { Authorization: `Bearer ${token}` },
});
const userData = await initUserResponse.json();
// return { success: true };
if (role == "member") {
redirect('/dashboard/member')
} else if (role == "employer") {
redirect('/dashboard/employer')
} else if (role == "broker") {
redirect('/dashboard/broker')
} else if (role == "provider") {
redirect('/dashboard/provider')
} else if (role == "carrier") {
redirect('/dashboard/carrier')
} else if (role == "admin") {
redirect('/dashboard/admin')
} else {
return { error: 'Invalid email or password.' }
}
}
+91
View File
@@ -0,0 +1,91 @@
'use server'
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { SignJWT } from 'jose';
const SECRET_KEY = new TextEncoder().encode(process.env.JWT_SECRET_KEY);
function roleMapping(role_number: number) {
const map = [
"member",
"employer",
"broker",
"provider",
"carrier",
"admin"
]
return map[role_number]
}
export async function signInAndFetchUser(prevState: any, formData: FormData) {
const email = formData.get('email') as string
const password = formData.get('password') as string
if (!email || !password) {
throw new Error('Please fill in all fields.');
}
const authResponse = await fetch(`http://${process.env.BE_API_SERVER_HOST}:${process.env.BE_API_SERVER_PORT}/signin`, {
method: 'POST',
body: JSON.stringify({ user: {email, password} }),
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
});
if (!authResponse.ok) {
throw new Error('Invalid credentials');
}
const { user } = await authResponse.json();
const token = authResponse.headers.get('authorization')?.split(' ')[1];
console.log("token: ", token)
// const token = user.jti
const role = user.role
const sessionPayload = {
token: token,
user: {
id: user.id,
name: user.name,
email: user.email,
role: role,
roleId: user.role_id,
keychain: user.keychain
}
};
console.log("sessionPayload: ", sessionPayload)
const encryptedSession = await new SignJWT(sessionPayload)
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('2h')
.sign(SECRET_KEY);
const cookieStore = await cookies();
cookieStore.set('session', encryptedSession, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
});
if (role == "member") {
redirect('/dashboard/member')
} else if (role == "employer") {
redirect('/dashboard/employer')
} else if (role == "broker") {
redirect('/dashboard/broker')
} else if (role == "provider") {
redirect('/dashboard/provider')
} else if (role == "carrier") {
redirect('/dashboard/carrier')
} else if (role == "admin") {
redirect('/dashboard/admin')
} else {
return { error: 'Invalid email or password.' }
}
}
+98
View File
@@ -0,0 +1,98 @@
// app/login/actions.ts
'use server'
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { SignJWT } from 'jose';
const SECRET_KEY = new TextEncoder().encode(process.env.JWT_SECRET_KEY);
function roleMapping(role_number: number) {
const map = [
"member",
"employer",
"broker",
"provider",
"carrier",
"admin"
]
return map[role_number]
}
export async function registerUser(prevState: any, formData: FormData) {
const email = formData.get('email') as string
const password = formData.get('password') as string
const confirm_password = formData.get('confirmPassword') as string
const role = formData.get('role') as string
const role_id = formData.get('roleId') as string
const last_four_ssn = formData.get('lastFourSsn') as string
const date_of_birth = formData.get('dateOfBirth') as string
if (!email || !password || !confirm_password || !role) {
throw new Error('Please fill in all fields.');
}
if (role === "provider" && !role_id) {
throw new Error('Please fill in all fields.');
}
if (role === "member" && (!date_of_birth || !last_four_ssn)) {
throw new Error('Please fill in all fields.');
}
const authResponse = await fetch(`${process.env.BE_API_SERVER_HOST}:${process.env.BE_API_SERVER_PORT}/api/v1/login`, {
method: 'POST',
body: JSON.stringify({ user: {email, password, confirm_password, role, role_id, last_four_ssn, date_of_birth} }),
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
});
if (!authResponse.ok) {
throw new Error('Invalid credentials');
}
const data = await authResponse.json();
const token = data.jti
const sessionPayload = {
token: token,
user: {
id: data.id,
name: data.name,
email: data.email,
role: data.role,
roleId: data.role_id,
keychain: data.keychain
}
};
const encryptedSession = await new SignJWT(sessionPayload)
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('2h')
.sign(SECRET_KEY);
const cookieStore = await cookies();
cookieStore.set('session', encryptedSession, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
});
if (role == "member") {
redirect('/dashboard/member')
} else if (role == "employer") {
redirect('/dashboard/employer')
} else if (role == "broker") {
redirect('/dashboard/broker')
} else if (role == "provider") {
redirect('/dashboard/provider')
} else if (role == "carrier") {
redirect('/dashboard/carrier')
} else if (role == "admin") {
redirect('/dashboard/admin')
} else {
return { error: 'Invalid email or password.' }
}
}