File size: 1,775 Bytes
d1a980e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const bcrypt = require('bcryptjs');

async function debugAuth() {
    try {
        const email = 'admin@example.com';
        const password = 'admin';

        console.log(`Checking user: ${email}`);
        const user = await prisma.user.findUnique({
            where: { email }
        });

        if (!user) {
            console.log('User NOT found in DB.');
            console.log('Attempting to create user...');
            const hashedPassword = await bcrypt.hash(password, 10);
            await prisma.user.create({
                data: {
                    email,
                    passwordHash: hashedPassword,
                    name: 'System Admin',
                    username: 'admin',
                    role: 'ADMIN'
                }
            });
            console.log('User created successfully.');
        } else {
            console.log('User FOUND in DB.');
            console.log('Stored Hash:', user.passwordHash);

            const isValid = await bcrypt.compare(password, user.passwordHash);
            console.log(`Password '${password}' is valid?`, isValid);

            if (!isValid) {
                console.log('Updating password to ensure it is correct...');
                const newHash = await bcrypt.hash(password, 10);
                await prisma.user.update({
                    where: { email },
                    data: { passwordHash: newHash }
                });
                console.log('Password updated.');
            }
        }
    } catch (e) {
        console.error('Error:', e);
    } finally {
        await prisma.$disconnect();
    }
}

debugAuth();